{"id":436,"date":"2026-04-08T14:43:20","date_gmt":"2026-04-08T06:43:20","guid":{"rendered":"https:\/\/hhh3m.top\/?p=436"},"modified":"2026-05-08T16:35:11","modified_gmt":"2026-05-08T08:35:11","slug":"%e5%9f%ba%e7%a1%80crud","status":"publish","type":"post","link":"https:\/\/hhh3m.top\/index.php\/2026\/04\/08\/%e5%9f%ba%e7%a1%80crud\/","title":{"rendered":"\u57fa\u7840crud\u5b66\u4e60"},"content":{"rendered":"\n<p>\u53c2\u8003\u89c6\u9891<a href=\"https:\/\/www.bilibili.com\/video\/BV1gm411m7i6?vd_source=97feb73c43f1bbbaba6431e9f6ebde87\">\u3010\u4e00\u5c0f\u65f6\u5e26\u4f60\u4ece0\u52301\u5b9e\u73b0\u4e00\u4e2aSpringBoot\u9879\u76ee\u5f00\u53d1\u3011<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u57fa\u672c\u7ed3\u6784<\/h3>\n\n\n\n<div class=\"wp-block-media-text is-stacked-on-mobile\" style=\"grid-template-columns:46% auto\"><figure class=\"wp-block-media-text__media\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/hhh3m.top\/wp-content\/uploads\/2026\/04\/image-1.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"606\" height=\"657\" data-original=\"https:\/\/hhh3m.top\/wp-content\/uploads\/2026\/04\/image-1.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-442 size-full\"  sizes=\"auto, (max-width: 606px) 100vw, 606px\" \/><\/div><\/figure><div class=\"wp-block-media-text__content\">\n<p>Controller\u5373api\u5c42\uff0c\u5b9a\u4e49api\u63a5\u53e3\u8def\u5f84\u4ee5\u53ca\u8c03\u7528\u5bf9\u5e94\u7684service\u65b9\u6cd5&#8211;&gt;<\/p>\n\n\n\n<p>Service \u8c03\u7528DAO\u5c42 &#8211;&gt;<\/p>\n\n\n\n<p>DAO \u901a\u8fc7 ORM \u6620\u5c04\u5b9e\u73b0\u6570\u636e\u5e93\u4e2d\u6570\u636e\u5230java\u5b9e\u4f53\u7684\u8f6c\u6362\uff0c\u5176\u4e2drepository\u7c7b\u91cc\u81ea\u5e26\u589e\u5220\u67e5\u6539\u65b9\u6cd5<\/p>\n<\/div><\/div>\n\n\n\n<p>\u53ef\u5728<a href=\"https:\/\/start.spring.io\">start.spring.io<\/a>\u4e2d\u81ea\u52a8\u751f\u6210\u4ee3\u7801demo\uff0c\u4e86\u89e3springboot\u57fa\u7840\u67b6\u6784<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">controller\u5c42<\/h3>\n\n\n\n<p>\u4e3b\u76ee\u5f55\u65b0\u5efacontroller\u7c7b\uff0c\u5728\u5176\u4e2d\u901a\u8fc7GetMapping\u6ce8\u89e3\u53ef\u5b9e\u73b0api\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.demo;\n\nimport org.springframework.web.bind.annotation.RestController;\nimport org.springframework.web.bind.annotation.GetMapping;\n\n@RestController\/\/Rest API \u6ce8\u89e3\npublic class TestController {\n    \n    @GetMapping(\"\/hello\")\/\/\u610f\u601d\u662f\u901a\u8fc7get\u8bf7\u6c42\/hello\u8def\u5f84\n    public String hello() {\n        return \"hello world\";\n    }\n\n}<\/code><\/pre>\n\n\n\n<p>\u6b64\u65f6\u542f\u52a8\u7a0b\u5e8f\uff0c\u8bbf\u95ee127.0.0.1:8080\/hello\u8c03\u7528api\u5373\u4f1a\u8fd4\u56dehallo world<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6570\u636e\u5e93<\/h3>\n\n\n\n<p>\u8fd9\u91cc\u76f4\u63a5\u5728\u8fd9\u53f0\u4e91\u670d\u52a1\u5668\u521b\u5efa\u6570\u636e\u5e93demo\uff0c\u65b0\u5efastudent\u8868<\/p>\n\n\n\n<p>CREATE TABLE student(<br>    id INT AUTO_INCREMENT PRIMARY KEY,<br>    name VARCHAR(50) NOT NULL,<br>    email VARCHAR(100) NOT NULL,<br>    age INT<br>);<\/p>\n\n\n\n<p>AUTO_INCREMENT\uff1a\u81ea\u52a8\u9012\u589e          PRIMARY KEY\uff1a\u4e3b\u952e<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">DAO\u5c42<\/h3>\n\n\n\n<p>\u8981\u64cd\u4f5c\u8fd9\u4e2a\u8868\uff0c\u5728\u4e3b\u5305\u4e0b\u65b0\u5efadao\u5305\u8868\u793aData Access\u5c42\uff0c\u4e0b\u9762\u65b0\u5efaStudentRepostory\u63a5\u53e3\u4ee5\u53caStudent\u7c7b\u6765\u63a5\u53d7\u6570\u636e\u5e93\u6620\u5c04\u7684\u6570\u636e\uff0cstudent\u7c7b\u91cc\u5199\u4e0a\u8868\u5bf9\u5e94\u7684\u5b57\u6bb5\u4ee5\u53caget\uff0cset\u65b9\u6cd5\uff0crepository\u91cc\u5c31\u81ea\u52a8\u751f\u6210\u65b9\u6cd5\u6bd4\u5982getById\uff08\uff09\u76f4\u63a5\u53ef\u4ee5\u7528\u6765\u64cd\u4f5c\u6570\u636e\u5e93\uff0c\u4e5f\u53ef\u81ea\u5b9a\u4e49sql\u8bed\u53e5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Service\u5c42<\/strong><\/h3>\n\n\n\n<p>seivice\u8c03\u7528repository\u7684\u65b9\u6cd5\u64cd\u4f5c\u6570\u636e\u5e93\uff0c\u628a\u8fd4\u56de\u503c\u6309\u9700\u8981\u5904\u7406\u4e00\u4e0b\u8fd4\u56de\u7ed9controller<\/p>\n\n\n\n<p>\u8fd8\u6709\u4e00\u4e9b\u8fd4\u56de\u683c\u5f0f\u89c4\u8303\u7b49<\/p>\n\n\n\n<div style=\"border: 3px solid #000; border-radius: 8px; padding: 20px; margin: 20px 0;\">\n    <h1>\u5b66\u751f\u7ba1\u7406crud<\/h1>\n\n    <button onclick=\"hello()\">hello<\/button>\n    <button onclick=\"loadAll()\">\u67e5\u8be2\u6240\u6709<\/button>\n    <br><br>\n    <input type=\"text\" id=\"name\" placeholder=\"\u59d3\u540d\">\n    <input type=\"text\" id=\"email\" placeholder=\"\u90ae\u7bb1\">\n    <input type=\"number\" id=\"age\" placeholder=\"\u5e74\u9f84\">\n    <button onclick=\"add()\">\u589e\u52a0<\/button>\n    <br><br>\n    <input type=\"number\" id=\"delId\" placeholder=\"\u5220\u9664ID\">\n    <button onclick=\"del()\">\u5220\u9664<\/button>\n    <br><br>\n    <input type=\"number\" id=\"updId\" placeholder=\"\u4fee\u6539ID\">\n    <input type=\"text\" id=\"updName\" placeholder=\"\u59d3\u540d\">\n    <input type=\"text\" id=\"updEmail\" placeholder=\"\u90ae\u7bb1\">\n    <input type=\"number\" id=\"updAge\" placeholder=\"\u5e74\u9f84\">\n    <button onclick=\"upd()\">\u4fee\u6539<\/button>\n\n    <h2>\u5b66\u751f\u5217\u8868<\/h2>\n    <table border=\"1\" id=\"table\">\n        <thead>\n            <tr><th>ID<\/th><th>\u59d3\u540d<\/th><th>\u90ae\u7bb1<\/th><th>\u5e74\u9f84<\/th><\/tr>\n        <\/thead>\n        <tbody id=\"tbody\"><\/tbody>\n    <\/table>\n<\/div>\n\n<script>\n    const base = 'https:\/\/hhh3m.top\/api';\n    window.onload = loadAll;\n\n    function hello() {\n        fetch(base + '\/hello')\n            .then(r => r.text())\n            .then(data => {\n                alert(data);\n                loadAll();\n            });\n    }\n\n    function loadAll() {\n        fetch(base + '\/students')\n            .then(r => r.json())\n            .then(res => {\n                const tbody = document.getElementById('tbody');\n                tbody.innerHTML = '';\n                res.data.forEach(s => {\n                    tbody.innerHTML += `<tr><td>${s.id}<\/td><td>${s.name}<\/td><td>${s.email}<\/td><td>${s.age}<\/td><\/tr>`;\n                });\n            });\n    }\n\n    function add() {\n        const name = document.getElementById('name').value;\n        const email = document.getElementById('email').value;\n        const age = document.getElementById('age').value;\n        fetch(base + '\/student', {\n            method: 'POST',\n            headers: {'Content-Type': 'application\/json'},\n            body: JSON.stringify({name, email, age: parseInt(age)})\n        }).then(r => r.json()).then(() => {\n            alert('\u6dfb\u52a0\u6210\u529f');\n            loadAll();\n        });\n    }\n\n    function del() {\n        const id = document.getElementById('delId').value;\n        fetch(base + '\/delete\/' + id)\n            .then(r => r.json())\n            .then(() => {\n                alert('\u5220\u9664\u6210\u529f');\n                loadAll();\n            });\n    }\n\n    function upd() {\n        const id = document.getElementById('updId').value;\n        const name = document.getElementById('updName').value;\n        const email = document.getElementById('updEmail').value;\n        const age = document.getElementById('updAge').value;\n        fetch(base + '\/update\/' + id, {\n            method: 'POST',\n            headers: {'Content-Type': 'application\/json'},\n            body: JSON.stringify({name, email, age: parseInt(age)})\n        }).then(r => r.json()).then(() => {\n            alert('\u4fee\u6539\u6210\u529f');\n            loadAll();\n        });\n    }\n<\/script>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\u89c6\u9891\u3010\u4e00\u5c0f\u65f6\u5e26\u4f60\u4ece0\u52301\u5b9e\u73b0\u4e00\u4e2aSpringBoot\u9879\u76ee\u5f00\u53d1\u3011 \u57fa\u672c\u7ed3\u6784 Controller\u5373api\u5c42\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[12,22,10],"class_list":["post-436","post","type-post","status-publish","format-standard","hentry","category-java_study","tag-java","tag-spring-boot","tag-10"],"_links":{"self":[{"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/posts\/436","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/comments?post=436"}],"version-history":[{"count":27,"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/posts\/436\/revisions"}],"predecessor-version":[{"id":565,"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/posts\/436\/revisions\/565"}],"wp:attachment":[{"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/media?parent=436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/categories?post=436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hhh3m.top\/index.php\/wp-json\/wp\/v2\/tags?post=436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}