{"id":7060,"date":"2024-11-14T10:32:17","date_gmt":"2024-11-14T02:32:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7060\/"},"modified":"2024-11-14T10:32:17","modified_gmt":"2024-11-14T02:32:17","slug":"spring-boot-mybatis-%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e4%bc%98%e9%9b%85%e5%9c%b0%e6%9f%a5%e8%af%a2%e4%b8%8d%e5%90%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e7%9a%84%e8%ae%b0%e5%bd%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7060\/","title":{"rendered":"Spring Boot + MyBatis \u4e2d\uff0c\u5982\u4f55\u4f18\u96c5\u5730\u67e5\u8be2\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u8bb0\u5f55\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173037046726691.jpg\" class=\"aligncenter\" title=\"Spring Boot + MyBatis \u4e2d\uff0c\u5982\u4f55\u4f18\u96c5\u5730\u67e5\u8be2\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u8bb0\u5f55\uff1f\u63d2\u56fe\" alt=\"Spring Boot + MyBatis \u4e2d\uff0c\u5982\u4f55\u4f18\u96c5\u5730\u67e5\u8be2\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u8bb0\u5f55\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>spring boot + mybatis \u4e2d\u7684 sql \u4ea4\u4e92\u96be\u9898<\/strong><\/p>\n<p>\u5728 spring boot \u4e0e mybatis \u6846\u67b6\u7ed3\u5408\u7684\u9879\u76ee\u4e2d\uff0c\u53ef\u80fd\u9047\u5230\u8fd9\u6837\u7684\u6311\u6218\uff1a\u5bf9\u4e8e\u540c\u4e00\u5f20\u8868\u4e2d\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982\u732b\u3001\u72d7\u3001\u732a\uff09\uff0c\u9700\u8981\u6839\u636e\u7279\u5b9a\u5b57\u6bb5\uff08\u5982 type\uff09\u8fdb\u884c\u67e5\u8be2\u3002<\/p>\n<p><strong>\u67e5\u8be2\u65b9\u6cd5\u7684\u9009\u62e9<\/strong><\/p>\n<p>\u5bf9\u4e8e\u67e5\u8be2\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\uff0c\u6709\u4e24\u79cd\u65b9\u6848\uff1a<\/p>\n<ol>\n<li>\u6bcf\u4e2a\u7c7b\u578b\u5b9a\u4e49\u4e00\u4e2a\u4e13\u95e8\u7684\u67e5\u8be2\u65b9\u6cd5\uff0c\u5e76\u5728 sql \u8bed\u53e5\u4e2d\u6307\u5b9a type \u503c\u3002<\/li>\n<li>\u6784\u5efa\u4e00\u4e2a\u901a\u7528\u7684\u67e5\u8be2\u65b9\u6cd5\uff0c\u901a\u8fc7\u53c2\u6570\u6307\u5b9a type \u503c\u3002<\/li>\n<\/ol>\n<p>\u7b2c\u4e00\u4e2a\u65b9\u6848\u7b80\u5355\u76f4\u89c2\uff0c\u4f46\u7ef4\u62a4\u6210\u672c\u8f83\u9ad8\uff0c\u9700\u8981\u4e3a\u6bcf\u4e2a\u7c7b\u578b\u521b\u5efa\u5355\u72ec\u7684\u65b9\u6cd5\u3002\u7b2c\u4e8c\u4e2a\u65b9\u6848\u66f4\u7075\u6d3b\uff0c\u4f46\u5bf9 sql \u8bed\u53e5\u7684\u7f16\u5199\u8981\u6c42\u66f4\u9ad8\u3002<\/p>\n<p><strong>\u5408\u5e76\u67e5\u8be2\u65b9\u6cd5<\/strong><\/p>\n<p>\u4e3a\u4e86\u7b80\u5316\u67e5\u8be2\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8003\u8651\u5408\u5e76\u4e09\u4e2a\u67e5\u8be2\u65b9\u6cd5\u4e3a\u4e00\u4e2a\u3002\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 mybatis \u7684\u52a8\u6001 sql \u529f\u80fd\uff0c\u6839\u636e type \u503c\u52a8\u6001\u751f\u6210 sql \u8bed\u53e5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>&lt;select id=\"queryAnimalsByType\" resultType=\"com.example.domain.Animal\"&gt;\n  SELECT * FROM animal WHERE type = #{type}\n&lt;\/select&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6837\uff0c\u65e0\u8bba\u67e5\u8be2\u54ea\u79cd\u7c7b\u578b\u7684\u6570\u636e\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u67e5\u8be2\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u66f4\u4f18\u65b9\u6848<\/strong><\/p>\n<p>\u4e3a\u4e86\u66f4\u4f18\u96c5\u5730\u89e3\u51b3\u95ee\u9898\uff0c\u53ef\u4ee5\u5f15\u5165\u4e00\u4e2a\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u5e38\u91cf\u7684\u679a\u4e3e\u7c7b\u3002\u7136\u540e\uff0c\u4e3a animalservice \u63a5\u53e3\u5b9a\u4e49\u4e09\u4e2a\u5206\u79bb\u7684\u67e5\u8be2\u65b9\u6cd5\uff0c\u6bcf\u4e2a\u65b9\u6cd5\u5bf9\u5e94\u4e00\u4e2a\u7279\u5b9a\u7684\u7c7b\u578b\u3002\u6700\u540e\uff0c\u5728 animalserviceimpl \u4e2d\uff0c\u901a\u8fc7\u5e38\u91cf\u679a\u4e3e\u76f4\u63a5\u8c03\u7528\u901a\u7528\u7684 animalmapper.querybytype \u65b9\u6cd5\u3002\u8fd9\u79cd\u89e3\u51b3\u65b9\u6848\u65e2\u7075\u6d3b\u53c8\u53ef\u7ef4\u62a4\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSpring Boot + MyBatis \u4e2d\uff0c\u5982\u4f55\u4f18\u96c5\u5730\u67e5\u8be2\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u8bb0\u5f55\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>spring boot + mybatis \u4e2d\u7684 sql \u4ea4\u4e92\u96be\u9898 \u5728 spring boot \u4e0e mybatis \u6846\u67b6\u7ed3\u5408\u7684\u9879\u76ee\u4e2d\uff0c\u53ef\u80fd\u9047\u5230\u8fd9\u6837\u7684\u6311\u6218\uff1a\u5bf9\u4e8e\u540c\u4e00\u5f20\u8868\u4e2d\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982\u732b\u3001\u72d7\u3001\u732a\uff09\uff0c\u9700\u8981\u6839\u636e\u7279\u5b9a\u5b57\u6bb5\uff08\u5982 type\uff09\u8fdb\u884c\u67e5\u8be2\u3002 \u67e5\u8be2\u65b9\u6cd5\u7684\u9009\u62e9 \u5bf9\u4e8e\u67e5\u8be2\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\uff0c\u6709\u4e24\u79cd\u65b9\u6848\uff1a \u6bcf\u4e2a\u7c7b\u578b\u5b9a\u4e49\u4e00\u4e2a\u4e13\u95e8\u7684\u67e5\u8be2\u65b9\u6cd5\uff0c\u5e76\u5728 sql \u8bed\u53e5\u4e2d\u6307\u5b9a type \u503c\u3002 \u6784\u5efa\u4e00\u4e2a\u901a\u7528\u7684\u67e5\u8be2\u65b9\u6cd5\uff0c\u901a\u8fc7\u53c2\u6570\u6307\u5b9a type \u503c\u3002 \u7b2c\u4e00\u4e2a\u65b9\u6848\u7b80\u5355\u76f4\u89c2\uff0c\u4f46\u7ef4\u62a4\u6210\u672c\u8f83\u9ad8\uff0c\u9700\u8981\u4e3a\u6bcf\u4e2a\u7c7b\u578b\u521b\u5efa\u5355\u72ec\u7684\u65b9\u6cd5\u3002\u7b2c\u4e8c\u4e2a\u65b9\u6848\u66f4\u7075\u6d3b\uff0c\u4f46\u5bf9 sql \u8bed\u53e5\u7684\u7f16\u5199\u8981\u6c42\u66f4\u9ad8\u3002 \u5408\u5e76\u67e5\u8be2\u65b9\u6cd5 \u4e3a\u4e86\u7b80\u5316\u67e5\u8be2\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8003\u8651\u5408\u5e76\u4e09\u4e2a\u67e5\u8be2\u65b9\u6cd5\u4e3a\u4e00\u4e2a\u3002\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 mybatis \u7684\u52a8\u6001 sql \u529f\u80fd\uff0c\u6839\u636e type \u503c\u52a8\u6001\u751f\u6210 sql \u8bed\u53e5\u3002\u4f8b\u5982\uff1a &lt;select id=&#8221;queryAnimalsByType&#8221; resultType=&#8221;com.example.domain.Animal&#8221;&gt; SELECT * FROM animal WHERE type = #{type} &lt;\/select&gt; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6837\uff0c\u65e0\u8bba\u67e5\u8be2\u54ea\u79cd\u7c7b\u578b\u7684\u6570\u636e\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u67e5\u8be2\u65b9\u6cd5\u3002 \u66f4\u4f18\u65b9\u6848 \u4e3a\u4e86\u66f4\u4f18\u96c5\u5730\u89e3\u51b3\u95ee\u9898\uff0c\u53ef\u4ee5\u5f15\u5165\u4e00\u4e2a\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u5e38\u91cf\u7684\u679a\u4e3e\u7c7b\u3002\u7136\u540e\uff0c\u4e3a animalservice \u63a5\u53e3\u5b9a\u4e49\u4e09\u4e2a\u5206\u79bb\u7684\u67e5\u8be2\u65b9\u6cd5\uff0c\u6bcf\u4e2a\u65b9\u6cd5\u5bf9\u5e94\u4e00\u4e2a\u7279\u5b9a\u7684\u7c7b\u578b\u3002\u6700\u540e\uff0c\u5728 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-7060","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=7060"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7060\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}