{"id":200705,"date":"2025-05-06T12:55:26","date_gmt":"2025-05-06T04:55:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200705\/"},"modified":"2025-05-06T12:55:26","modified_gmt":"2025-05-06T04:55:26","slug":"%e5%9c%a8-spring-boot-%e4%b8%ad%e9%93%be%e6%8e%a5-mysql-%e6%97%b6%ef%bc%8c-mybatis-%e6%96%b9%e6%b3%95%e4%b8%ad%e7%94%a8%e7%a1%ac%e7%bc%96%e7%a0%81%e8%bf%98%e6%98%af%e5%8f%82%e6%95%b0%e4%bc%a0%e9%80%92","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200705\/","title":{"rendered":"\u5728 Spring Boot \u4e2d\u94fe\u63a5 MySQL \u65f6\uff0c MyBatis \u65b9\u6cd5\u4e2d\u7528\u786c\u7f16\u7801\u8fd8\u662f\u53c2\u6570\u4f20\u9012\u66f4\u5408\u9002\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5728 Spring Boot \u4e2d\u94fe\u63a5 MySQL \u65f6\uff0c MyBatis \u65b9\u6cd5\u4e2d\u7528\u786c\u7f16\u7801\u8fd8\u662f\u53c2\u6570\u4f20\u9012\u66f4\u5408\u9002\uff1f<\/h1>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u4e3b\u673a\u5b9d\u8d1d\u5c31\u6574\u7406\u5206\u4eab\u300a\u5728 Spring Boot \u4e2d\u94fe\u63a5 MySQL \u65f6\uff0c MyBatis \u65b9\u6cd5\u4e2d\u7528\u786c\u7f16\u7801\u8fd8\u662f\u53c2\u6570\u4f20\u9012\u66f4\u5408\u9002\uff1f\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6570\u636e\u5e93\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u4e3b\u673a\u5b9d\u8d1d\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241112\/17313852866732d7c6c9d7a.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5728 Spring Boot \u4e2d\u94fe\u63a5 MySQL \u65f6\uff0c\u9700\u8981\u6ce8\u610f\u7684 MyBatis \u95ee\u9898<\/strong><\/p>\n<p>\u4e00\u3001\u80cc\u666f<\/p>\n<p>\u5047\u8bbe\u5b58\u5728\u4e00\u4e2a\u540d\u4e3a &#8220;animal&#8221; \u7684 SQL \u8868\uff0c\u5176\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u5217\u540d<\/th>\n<th>\u7c7b\u578b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>id<\/td>\n<td>\u6574\u6570<\/td>\n<\/tr>\n<tr>\n<td>name<\/td>\n<td>\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>type<\/td>\n<td>\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>age<\/td>\n<td>\u6574\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8868\u5305\u542b\u4ee5\u4e0b\u6570\u636e\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>id<\/th>\n<th>name<\/th>\n<th>type<\/th>\n<th>age<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>\u767d\u80d6<\/td>\n<td>cat<\/td>\n<td>12<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>\u6cf0\u54e5<\/td>\n<td>cat<\/td>\n<td>10<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>\u5927G<\/td>\n<td>dog<\/td>\n<td>9<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>\u58a8\u5b9d<\/td>\n<td>sheep<\/td>\n<td>6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e8c\u3001\u95ee\u9898<\/p>\n<p>\u7f16\u5199\u4e00\u4e2a MyBatis \u65b9\u6cd5 queryCatFromXXX()\uff0c\u4ece &#8220;animal&#8221; \u8868\u4e2d\u83b7\u53d6\u6240\u6709\u7c7b\u578b\u4e3a &#8220;cat&#8221; \u7684\u6570\u636e\u3002\u5728 SQL \u8bed\u53e5\u4e2d\uff0c\u6761\u4ef6 &#8220;type=cat&#8221; \u53ef\u4ee5\uff1a<\/p>\n<ul>\n<li><strong>\u786c\u7f16\u7801\u5728 SQL \u4e2d<\/strong>\uff1aselect * from animal where type=&#8221;cat&#8221;<\/li>\n<li><strong>\u901a\u8fc7\u53c2\u6570\u4f20\u9012<\/strong>\uff1aselect * from animal where type=#{type}<\/li>\n<\/ul>\n<p>\u90a3\u4e48\uff0c\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\uff0c\u54ea\u79cd\u505a\u6cd5\u66f4\u5408\u9002\uff1a<\/p>\n<ul>\n<li>\u65b9\u6cd5\u660e\u786e\u7528\u4e8e\u67e5\u8be2 &#8220;cat&#8221; \u76f8\u5173\u6570\u636e<\/li>\n<li>\u5b58\u5728\u66f4\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u67e5\u8be2\u4e0d\u540c\u7c7b\u578b\u7684\u52a8\u7269\u6570\u636e<\/li>\n<\/ul>\n<p>\u4e09\u3001\u7b54\u6848<\/p>\n<p>\u63a8\u8350\u5728 MyBatis \u65b9\u6cd5\u4e2d\u901a\u8fc7 <strong>\u4f20\u9012 type \u53c2\u6570\u6765\u83b7\u53d6\u6570\u636e<\/strong>\u3002<\/p>\n<p>\u5728 <strong>Service \u5c42<\/strong>\u8003\u8651\u662f\u5426\u9700\u8981\u66f4\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u6bd4\u5982\u6839\u636e\u7c7b\u578b\u679a\u4e3e\u83b7\u53d6\u6570\u636e\u3002\u5982\u679c\u9700\u8981\uff1a<\/p>\n<ul>\n<li>\u5728 Service \u5c42\u521b\u5efa\u4e00\u4e2a\u65b9\u6cd5\uff0c\u4f20\u5165\u7c7b\u578b\u679a\u4e3e\u53c2\u6570\u3002<\/li>\n<li>\u5728\u65b9\u6cd5\u5185\u90e8\uff0c\u6839\u636e\u7c7b\u578b\u679a\u4e3e\u6784\u5efa\u67e5\u8be2\u6761\u4ef6\u3002<\/li>\n<li>Controller \u76f4\u63a5\u8c03\u7528\u65b9\u6cd5\uff0c\u4f20\u5165 &#8220;cat&#8221; \u503c\u679a\u4e3e\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u4e0d\u9700\u8981\u66f4\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728 Service \u5c42\u521b\u5efa\u7279\u5b9a\u65b9\u6cd5\uff0c\u5982\u83b7\u53d6\u8f6c\u79fb\u52a8\u7269\u6570\u636e\u3002\u8be5\u65b9\u6cd5\u5185\u90e8\u5b9e\u73b0\u6761\u4ef6\u7b5b\u9009\uff0c\u8c03\u7528\u8005\u76f4\u63a5\u8c03\u7528\u65b9\u6cd5\u5373\u53ef\u83b7\u53d6\u9700\u8981\u8f6c\u79fb\u52a8\u7269\u7684\u6570\u636e\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5728 Spring Boot \u4e2d\u94fe\u63a5 MySQL \u65f6\uff0c MyBatis \u65b9\u6cd5\u4e2d\u7528\u786c\u7f16\u7801\u8fd8\u662f\u53c2\u6570\u4f20\u9012\u66f4\u5408\u9002\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring Boot \u4e2d\u94fe&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200705","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200705"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200705\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}