{"id":1648,"date":"2025-08-29T09:01:18","date_gmt":"2025-08-29T01:01:18","guid":{"rendered":"https:\/\/zero-zl.cn\/?p=1648"},"modified":"2025-08-29T09:02:05","modified_gmt":"2025-08-29T01:02:05","slug":"spring-mvc%e4%b8%89%e5%b1%82%e6%9e%b6%e6%9e%84","status":"publish","type":"post","link":"https:\/\/zero-zl.cn\/index.php\/2025\/08\/29\/spring-mvc%e4%b8%89%e5%b1%82%e6%9e%b6%e6%9e%84\/","title":{"rendered":"Spring MVC\u4e09\u5c42\u67b6\u6784"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large is-resized\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/zero-zl.cn\/wp-content\/uploads\/2025\/08\/image-109-1024x261.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"261\" data-original=\"https:\/\/zero-zl.cn\/wp-content\/uploads\/2025\/08\/image-109-1024x261.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-1649\" style=\"width:722px;height:auto\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">1. Controller \u5c42\uff08\u6700\u5de6\u4fa7\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u804c\u8d23\uff1a\u63a5\u6536 HTTP \u8bf7\u6c42 \u2192 \u7ec4\u88c5\u53c2\u6570 \u2192 \u8c03\u7528 Service \u2192 \u628a\u7ed3\u679c\u5c01\u88c5\u6210\u7edf\u4e00\u54cd\u5e94\u8fd4\u56de\u524d\u7aef\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong class=\"\">1. \u63a5\u6536\u5e76\u5c01\u88c5\u53c2\u6570<\/strong><br>\u628a\u524d\u7aef\u4f20\u8fdb\u6765\u7684 JSON\/\u8868\u5355\u91cc\u7684 username\u3001password \u53d6\u51fa\u6765\uff0c\u5c01\u88c5\u6210\u4e00\u4e2a DTO\uff08\u5982 <code>LoginDTO<\/code>\uff09\u3002<br>\u793a\u4f8b\u4ee3\u7801\uff08Spring MVC\uff09\uff1ajava\u590d\u5236<code>@PostMapping(\"\/login\") public Result login(@RequestBody LoginDTO dto) { Employee emp = employeeService.login(dto.getUsername(), dto.getPassword()); return Result.success(emp); }<\/code><\/li>\n\n\n\n<li><strong class=\"\">2. \u8c03\u7528 Service \u65b9\u6cd5\u67e5\u8be2\u6570\u636e\u5e93<\/strong><br>\u628a username\u3001password \u5f80\u4e0b\u4f20\uff0c\u4ea4\u7ed9 Service \u5c42\u53bb\u505a\u771f\u6b63\u7684\u4e1a\u52a1\u903b\u8f91\u3002<\/li>\n\n\n\n<li><strong class=\"\">3. \u5c01\u88c5\u7ed3\u679c\u5e76\u54cd\u5e94<\/strong><br>Service \u628a\u6821\u9a8c\u540e\u7684 Employee \u5bf9\u8c61\u8fd4\u56de\u4e0a\u6765\uff0cController \u628a\u5b83\u518d\u5305\u4e00\u5c42\u7edf\u4e00\u8fd4\u56de\u683c\u5f0f\uff08\u5982 <code>Result&lt;T><\/code>\uff09\uff0c\u7136\u540e return \u7ed9\u524d\u7aef\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Service \u5c42\uff08\u4e2d\u95f4\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u804c\u8d23\uff1a\u4e1a\u52a1\u7f16\u6392\u3001\u4e8b\u52a1\u63a7\u5236\u3001\u5bc6\u7801\u6821\u9a8c\u3001\u629b\u4e1a\u52a1\u5f02\u5e38\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong class=\"\">1. \u8c03\u7528 Mapper \u67e5\u8be2\u6570\u636e\u5e93<\/strong><br>\u62ff\u5230 username \u540e\uff0c\u7acb\u523b\u8c03 Mapper \u5c42\u53bb\u6570\u636e\u5e93\u67e5\u6574\u6761\u8bb0\u5f55\u3002<br>\u793a\u4f8b\uff1ajava\u590d\u5236<code>Employee emp = employeeMapper.findByUsername(username);<\/code><\/li>\n\n\n\n<li><strong class=\"\">2. \u5bc6\u7801\u6bd4\u5bf9<\/strong><br>Mapper \u8fd4\u56de\u7684\u662f\u6570\u636e\u5e93\u91cc\u5b58\u7684\u52a0\u5bc6\u540e\u7684\u5bc6\u7801\u3002Service \u5c42\u628a\u524d\u7aef\u4f20\u6765\u7684\u660e\u6587\u5bc6\u7801\u7528\u540c\u6837\u7684\u7b97\u6cd5\uff08\u5982 BCrypt\uff09\u52a0\u5bc6\uff0c\u518d\u4e0e\u6570\u636e\u5e93\u503c\u6bd4\u5bf9\uff1ajava\u590d\u5236<code>if (!passwordEncoder.matches(rawPassword, emp.getPassword())) { throw new BizException(\"\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef\"); }<\/code><\/li>\n\n\n\n<li><strong class=\"\">3. \u8fd4\u56de\u7ed3\u679c<\/strong><br>\u6bd4\u5bf9\u6210\u529f\u540e\u628a Employee \u5bf9\u8c61\u539f\u6837\u8fd4\u56de\u7ed9 Controller\uff1b\u5931\u8d25\u5219\u629b\u5f02\u5e38\u6216\u8fd4\u56de null\/\u9519\u8bef\u7801\uff0c\u7531 Controller \u7edf\u4e00\u5904\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. Mapper \u5c42\uff08\u6700\u53f3\u4fa7\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u804c\u8d23\uff1a\u7eaf\u7cb9\u7684 SQL \u4ea4\u4e92\uff0c\u53ea\u8d1f\u8d23\u628a\u6570\u636e\u62ff\u51fa\u6765\uff0c\u4e0d\u505a\u4efb\u4f55\u4e1a\u52a1\u5224\u65ad\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong class=\"\">1. <code>select * from employee<\/code> \u2026<\/strong><br>Mapper \u63a5\u53e3\u548c XML\/\u6ce8\u89e3\u91cc\u53ea\u5199\u4e00\u6761 SQL\uff1asql\u590d\u5236<code>SELECT * FROM employee WHERE username = <em>#{username}<\/em><\/code>MyBatis \u4f1a\u628a\u7ed3\u679c\u6620\u5c04\u6210 <code>Employee<\/code> \u5b9e\u4f53\u3002<\/li>\n\n\n\n<li><strong class=\"\">2. \u6570\u636e\u5e93<\/strong><br>\u6307\u771f\u6b63\u7684 RDBMS\uff08MySQL\u3001PostgreSQL \u2026\uff09\u3002SQL \u53d1\u5230\u5e93\u540e\u8fd4\u56de\u4e00\u884c\u8bb0\u5f55\uff08\u6216\u7a7a\uff09\u3002<\/li>\n\n\n\n<li><strong>3. \u8fd4\u56de\u7ed3\u679c<\/strong><br>Mapper \u628a\u7ed3\u679c\u585e\u5230\u5b9e\u4f53\u7c7b\u91cc\u56de\u4f20\u7ed9 Service\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u7eb5\u5411\u770b\u4e09\u6761\u201c1-2-3\u201d\u7ebf<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c\u4e00\u6761 1-2-3\uff1aController \u7684\u5168\u90e8\u751f\u547d\u5468\u671f<\/li>\n\n\n\n<li>\u7b2c\u4e8c\u6761 1-2-3\uff1aService \u7684\u5168\u90e8\u751f\u547d\u5468\u671f<\/li>\n\n\n\n<li>\u7b2c\u4e09\u6761 1-2-3\uff1aMapper \u7684\u5168\u90e8\u751f\u547d\u5468\u671f<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837\u5c31\u628a\u4e00\u6b21 HTTP \u8bf7\u6c42\u5728 Java \u540e\u7aef\u7684\u4e09\u5c42\u7ed3\u6784\u4e2d\u5b8c\u6574\u5730\u4e32\u4e86\u8d77\u6765\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Controller \u5c42\uff08\u6700\u5de6\u4fa7\uff09 \u804c\u8d23\uff1a\u63a5\u6536 HTTP \u8bf7\u6c42 \u2192 \u7ec4\u88c5\u53c2\u6570 \u2192 \u8c03\u7528 Service  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1600,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[108,107],"tags":[],"class_list":["post-1648","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring","category-ssm"],"_links":{"self":[{"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/posts\/1648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1648"}],"version-history":[{"count":1,"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/posts\/1648\/revisions"}],"predecessor-version":[{"id":1650,"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/posts\/1648\/revisions\/1650"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/media\/1600"}],"wp:attachment":[{"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}