{"id":10006600,"date":"2022-09-26T16:34:17","date_gmt":"2022-09-26T08:34:17","guid":{"rendered":"https:\/\/ityet.com\/?p=10006600"},"modified":"2022-09-26T16:34:17","modified_gmt":"2022-09-26T08:34:17","slug":"mybatis-%e5%a4%9a%e6%9d%a1%e4%bb%b6%e6%9f%a5%e8%af%a2%e3%80%81%e5%8a%a8%e6%80%81sql%e3%80%81%e5%a4%9a%e8%a1%a8%e6%93%8d%e4%bd%9c%e3%80%81%e6%b3%a8%e8%a7%a3%e5%bc%80%e5%8f%91%ef%bc%8c%e5%ba%94%e6%9c%89","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006600","title":{"rendered":"MyBatis \u591a\u6761\u4ef6\u67e5\u8be2\u3001\u52a8\u6001SQL\u3001\u591a\u8868\u64cd\u4f5c\u3001\u6ce8\u89e3\u5f00\u53d1\uff0c\u5e94\u6709\u5c3d\u6709\uff0c\u4e00\u7f51\u6253\u5c3d\uff01"},"content":{"rendered":"<p>\u6765\u6e90\uff1aiyu77.blog.csdn.net\/article\/details\/125761737<\/p>\n<p>MyBatis\u5c01\u88c5\u4e86JDBC\u901a\u8fc7Mapper\u4ee3\u7406\u7684\u65b9\u5f0f\uff0c\u4ee5\u524d\u7e41\u7410\u7684\u64cd\u4f5c\u901a\u8fc7\u201c\u5c5e\u6027\u4e0e\u5b57\u6bb5\u6620\u5c04\u201d\u5c31\u7b80\u5355\u5316\u89e3\uff0cMyBatis\u7684\u52a8\u6001SQL\u5b8c\u7f8e\u5c55\u73b0\u4e86DBMS\u7684\u72ec\u7279\u9b45\u529b<\/p>\n<h2>\u4e00\u3001\u591a\u6761\u4ef6\u67e5\u8be2<\/h2>\n<p>\u57fa\u4e8eMybatis\u7684\u591a\u6761\u4ef6\u67e5\u8be2\uff0c\u662f\u5728Mapper\u4ee3\u7406\u7684\u6620\u5c04\u6587\u4ef6\u4e2d\u5199\u4e0a\u539f\u6709\u7684SQL\uff0c\u7136\u540e\u63a5\u53e3\u4e2d\u5199\u4e00\u4e2a\u5e26\u53c2\u7684\u65b9\u6cd5\u5373\u53ef\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2FTD\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u76f8\u6bd4\u4e8e\u539f\u751f\u7684JDBC\u90a3\u4e00\u5957\uff0c\u901a\u8fc7MyBatis\u786e\u5b9e\u89e3\u51b3\u4e86\u4e0d\u5c11\u786c\u7f16\u7801\u7684\u95ee\u9898<\/p>\n<p>\u4f46\u662f\u7528\u6237\u7684\u67e5\u8be2\u6c38\u8fdc\u662f\u52a8\u6001\u7684\u64cd\u4f5c\uff0c\u4ed6\u53ef\u80fd\u5728\u591a\u4e2a\u6761\u4ef6\u4e2d\u9009\u62e9\u5176\u4e2d\u5c11\u91cf\u6761\u4ef6\u8fdb\u884c\u67e5\u8be2\uff0c\u6211\u4eec\u7684SQL\u662f\u6b7b\u7684\uff0c\u800c\u7528\u6237\u9700\u6c42\u5bf9\u5e94\u7684SQL\u5374\u662f\u6d3b\u7684\uff0c\u8fd9\u6837\u5c31\u4f1a\u9020\u6210\u4e0d\u5339\u914d\u800c\u5f62\u6210\u8bed\u6cd5\u9519\u8bef<\/p>\n<p>\u6bd4\u5982\uff0c\u6839\u636e\u8fd9\u5f20\u8868\uff0c\u82e5\u662f\u8981\u6839\u636e\u90e8\u5206\u5b57\u6bb5\u67e5\u51fa\u6574\u4f53\uff0c\u6211\u4eec\u53ef\u4ee5\u5199\u5bf9\u5e94\u9700\u6c42\u7684SQL\uff0c\u4f46\u662f\u6211\u8981\u662f\u67e5\u8be2\u7684\u6761\u4ef6\u53d8\u5c11\u4e86\u6216\u8005\u53d8\u591a\u4e86\u5462\uff1f\u82e5\u7528\u6237\u53ea\u60f3\u901a\u8fc7\u4e00\u4e2a\u6761\u4ef6\u6765\u67e5\u8be2\uff0c\u90a3\u4e48\u5728\u5176\u4ed6\u5360\u4f4d\u7b26\u7684\u4f4d\u7f6e\u4e0d\u8f93\u5165\u4e8e\u662f\u6210\u4e86null\uff0c\u8fc7\u4e0d\u4e86\u8bed\u6cd5\u81ea\u7136\u67e5\u4e0d\u4e86\uff0c\u8fd8\u5f97\u91cd\u65b0\u5199SQL\uff0c\u591a\u9ebb\u70e6<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2FTE\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8fd9\u4e2a\u65f6\u5019MyBatis\u7684\u7279\u8272\u5c31\u4f53\u73b0\u51fa\u6765\u4e86\u2014\u2014\u52a8\u6001SQL<\/p>\n<h2>\u4e8c\u3001\u52a8\u6001SQL<\/h2>\n<p>SQL\u8bed\u53e5\u4f1a\u968f\u7740\u7528\u6237\u7684\u8f93\u5165\u6216\u8005\u5916\u90e8\u6761\u4ef6\u7684\u53d8\u5316\u800c\u53d8\u5316\uff0c\u5219\u79f0\u4e4b\u4e3a\u52a8\u6001SQL<\/p>\n<h3>1. if-where<\/h3>\n<p>\u56e0\u4e3a\u91c7\u7528\u4e86Mapper\u4ee3\u7406\u5f00\u53d1\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5199xml\u7684\u5f62\u5f0f\u6765\u7f16\u5199\u6211\u4eec\u7684SQL\uff0c\u52a8\u6001SQL\u7684\u7279\u6027\u4e5f\u5c31\u5728\u8fd9\u4e00\u4e3e\u52a8\u4e2d\u6240\u8574\u80b2\uff0c\u5728\u539f\u6709\u7684Mapper\u6587\u4ef6\u91cc\u6211\u4eec\u8fdb\u884c\u5982\u4e0b\u6539\u9020\uff0c\u8ba9\u5e73\u5e73\u65e0\u5947\u7684SQL\u7115\u7136\u4e00\u65b0\uff1a<\/p>\n<pre><code>&lt;select id=&quot;selByCondition&quot; resultMap=&quot;rm&quot;&gt;\n    select *\n    from mybatis\n    &lt;where&gt;\n    &lt;if test=&quot;status !=null&quot;&gt;\n       and STATUS=#{STATUS}\n    &lt;\/if&gt;\n    &lt;if test=&quot;companyName !=null and companyName !=''&quot;&gt;\n    and company_name like #{companyName}\n    &lt;\/if&gt;\n    &lt;if test=&quot;bracdName !=null and bracdName !=''&quot;&gt;\n    and bracd_name like #{bracdName}\n    &lt;\/if&gt;\n    &lt;\/where&gt;\n&lt;\/select&gt;<\/code><\/pre>\n<p>\u201c<code>&lt;where&gt;<\/code>\u6807\u7b7e\u53ef\u4ee5\u81ea\u52a8\u5e2e\u6211\u4eec\u53bb\u6389and\u201d\uff0c\u8fd9\u6837\uff0c\u4e0d\u7ba1\u67e5\u8be2\u7684\u6761\u4ef6\u600e\u4e48\u53d8\uff0c\u6211\u8ddf\u7740\u8fd9\u4e2a\u903b\u8f91\u6d41\u7a0b\u8d70\u5c31\u4e0d\u4f1a\u51fa\u73b0SQL\u8bed\u6cd5\u6bdb\u75c5\u800c\u5bfc\u81f4\u67e5\u8be2\u4e0d\u51fa\u6765\u7684\u6bdb\u75c5\u5566\uff0c\u56e0\u4e3anull\u7684\u60c5\u51b5\u5df2\u7ecf\u88abif\u6240\u8fc7\u6ee4\u6389\u4e86\uff0c\u771f\u662f\u592a\u54c7\u585e\u4e86\uff01<\/p>\n<p>\u63a8\u8350\u4e00\u4e2a\u5f00\u6e90\u514d\u8d39\u7684 Spring Boot \u6700\u5168\u6559\u7a0b\uff1a<\/p>\n<blockquote><p><a href=\"https:\/\/link.segmentfault.com\/?enc=r9yDAH63PGKFvJ8bgFg7Mw%3D%3D.FuCRf3aI%2FxE0rjC1fOHxWwVoczp05E5BCyGZvFQLV1ddYzRKawBWBfxacQ42YmlYeKdodK%2FFu%2FtDYP4FZeQlig%3D%3D\" rel=\"nofollow\">https:\/\/github.com\/javastacks&#8230;<\/a><\/p><\/blockquote>\n<h3>2. choose-when-ortherwise<\/h3>\n<p>\u5bf9\u4e8e\u4ece\u591a\u4e2a\u6761\u4ef6\u4e2d\u9009\u62e9\u4e00\u4e2a\u7684\u5355\u6761\u4ef6\u67e5\u8be2\u7684\u573a\u666f\uff0c\u5229\u7528\u5206\u652f\u5d4c\u5957\u5c31\u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u9009\u62e9\u5355\u6761\u4ef6\uff1a<\/p>\n<p>\u5728MyBatis\u7684Mapper\u4ee3\u7406\u4e2d\uff0c<code>&lt;choose&gt;<\/code>\u76f8\u5f53\u4e8eswitch\uff0c<code>&lt;when&gt;<\/code>\u76f8\u5f53\u4e8ecase<\/p>\n<pre><code>&lt;select id=&quot;selByCondition2&quot; resultMap=&quot;rm&quot;&gt;\n    select *\n    from mybatis where\n    &lt;choose&gt;\n        &lt;when test=&quot;status !=null&quot;&gt;\n            STATUS=#{STATUS}\n        &lt;\/when&gt;\n        &lt;when test=&quot;companyName !=null and companyName !=''&quot;&gt;\n            company_name like #{companyName}\n        &lt;\/when&gt;\n        &lt;when test=&quot;bracdName !=null and bracdName !=''&quot;&gt;\n            bracd_name like #{bracdName}\n        &lt;\/when&gt;\n        &lt;otherwise&gt;1=1&lt;\/otherwise&gt;\n    &lt;\/choose&gt;\n&lt;\/select&gt;<\/code><\/pre>\n<p>\u4e0e\u591a\u6761\u4ef6\u67e5\u8be2\u4e0d\u540c\u7684\u662f\uff0cSQL\u8bed\u53e5\u4e2d\u53ea\u4f1a\u6709\u4e00\u4e2a\u5206\u652f\u751f\u6548<\/p>\n<p>\u5f53\u7528\u6237\u4e00\u4e2a\u6761\u4ef6\u90fd\u4e0d\u9009\u65f6\uff0c\u53ef\u4ee5\u5728<code>&lt;otherwise&gt;<\/code>\u4e2d\u5199\u4e0a<code>1=1<\/code>\u8ba9\u8bed\u6cd5\u6210\u7acb\uff0c\u53cd\u4e4b\uff0c\u82e5\u9009\u62e9\u4e86\u6761\u4ef6\u5219\u4f1a\u8fd4\u56de\u6b63\u5e38\u7ed3\u679c<\/p>\n<h3>3. foreach<\/h3>\n<p>\u5bf9\u4e8e\u6279\u91cf\u5220\u9664\u7684\u573a\u666f\uff0c\u4f20\u7edf\u7684\u65b9\u6cd5\u662f\u901a\u8fc7in\u5173\u952e\u5b57\u7ed3\u5408\u5360\u4f4d\u7b26\u6765\u786e\u5b9a\uff0c\u5c31\u50cf\u8fd9\u6837<\/p>\n<pre><code>where id in (?,?,?)<\/code><\/pre>\n<p>\u4f46\u5bf9\u4e8e\u52a8\u6001\u7684\u573a\u666f\uff0c\u6279\u91cf\u7684\u6570\u91cf\u6c38\u8fdc\u662f\u4e0d\u786e\u5b9a\u7684\uff0c\u8fd9\u5c31\u5bfc\u81f4\u8fd8\u9700\u8981\u53bb\u6539SQL\u91cc\u7684\u5360\u4f4d\u7b26\u6570\u91cf\u554a\uff0c\u53c8\u662f\u4e00\u4ef6\u9ebb\u70e6\u4e8b<\/p>\n<blockquote><p>PS:MyBatis\u4f1a\u5c06\u6570\u7ec4\u53c2\u6570\u5c01\u88c5\u6210\u4e00\u4e2aMap\u96c6\u5408\uff0c\u9ed8\u8ba4\u60c5\u51b5\uff08K-V\uff09array=\u6570\u7ec4<\/p><\/blockquote>\n<p>\u4e0b\u9762\u4f7f\u7528\u4e86<code>@Param<\/code>\u6ce8\u89e3\u6539\u53d8\u4e86map\u96c6\u5408\u4e2d\u9ed8\u8ba4\u7684key<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2FTF\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4e8e\u662fMyBatis\u4e2d\u7684<code>&lt;foreach&gt;<\/code>\u89e3\u51b3\u4e86\u8fd9\u4e00\u9ebb\u70e6\uff1a<\/p>\n<p>\u672c\u8d28\u662f\u901a\u8fc7\u904d\u5386\u7684\u5f62\u5f0f\uff0c\u6279\u91cf\u5220\u9664\u7684\u6570\u636e\u662f\u7531id\u6570\u7ec4\u6216\u8005\u96c6\u5408\u6765\u51b3\u5b9a\uff0c<code>collection<\/code>\u5c5e\u6027\u51b3\u5b9a\u4e86\u8981\u904d\u5386\u54ea\u4e2a\u6570\u7ec4\/\u96c6\u5408\uff0citem\u5c5e\u6027\u5219\u6765\u5b58\u653e\u9009\u51fa\u7684\u5143\u7d20\uff0c\u5e76\u628a\u5b83\u653e\u5728\u5360\u4f4d\u7b26\u91cc\uff0c<code>separator<\/code>\u5c5e\u6027\u8868\u793a\u5206\u9694\u7b26<\/p>\n<pre><code>&lt;delete id=&quot;deleteById&quot;&gt;\n    delete frpm mybatis where id in\n    &lt;foreach collection=&quot;ids&quot; item=&quot;id&quot; separator=&quot;,&quot; open=&quot;(&quot; close=&quot;)&quot;&gt;\n        #{id}\n    &lt;\/foreach&gt;;\n&lt;\/delete&gt;<\/code><\/pre>\n<p>\u6709\u4eba\u4f1a\u95ee\u4e3a\u5565\u8fd9\u91cc\u53ea\u6709\u4e00\u4e2a<code>#{id}<\/code>\uff0c\u6211\u7684\u5c5e\u6027\u5b57\u6bb5\u4e0d\u6b62\u8fd9\u4e00\u4e2a\u5440\uff1f\u6b64id\u975e\u5f7cid\u4ed6\u662f\u4e00\u4e2a\u6570\u7ec4\/\u96c6\u5408<\/p>\n<h2>\u4e09\u3001\u591a\u8868\u64cd\u4f5c<\/h2>\n<p>\u591a\u8868\u4e4b\u95f4\u7684\u5173\u7cfb\u6709\u4e00\u5bf9\u4e00\uff0c\u4e00\u5bf9\u591a\uff0c\u591a\u5bf9\u4e00\uff0c\u591a\u5bf9\u591a\uff0c\u6bcf\u4e00\u79cd\u90fd\u6709\u5efa\u8868\u7684\u539f\u5219,\u4ee5\u7528\u6237-\u8ba2\u5355\u6a21\u578b\u4e3a\u4f8b<\/p>\n<p>\u5229\u7528\u4f20\u7edf\u7684\u65b9\u6cd5\u8fdb\u884c\u591a\u8868\u67e5\u8be2\u65e0\u975e\u662f\u901a\u8fc7id\u6765\u8fde\u63a5\u8868\u7136\u540e\u5c01\u88c5\u8fd4\u56de\u7ed3\u679c\uff0cMyBatis\u4e2d\u4e5f\u662f\u5982\u6b64\uff0c\u6211\u4eec\u5728Mapper\u6587\u4ef6\u4e2d\u5199\u597d\u8868\u5b57\u6bb5\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u5b9a\u4e49\u597d\u7c7b\u578b\u5373\u53ef\uff0c\u53ea\u4e0d\u8fc7\u8fd9\u4e00\u8fc7\u7a0b\u6709\u70b9\u590d\u6742\uff0c\u4f46\u4e00\u6b21\u914d\u597d\u4e4b\u540e\u5373\u53ef\u6781\u5927\u51cf\u5c11\u786c\u7f16\u7801\u95ee\u9898\uff0c\u63d0\u9ad8\u6548\u7387<\/p>\n<h3>1. \u4e00\u5bf9\u4e00<\/h3>\n<p>\u4e00\u4e2a\u7528\u6237\u6709\u4e00\u5f20\u8ba2\u5355<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2FTG\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u9996\u5148\u8fd8\u662f\u90a3\u5957\u8def\uff0c\u5efa\u597d\u5b9e\u4f53\u7c7b\uff0c\u5199\u597d\u63a5\u53e3\u65b9\u6cd5\uff0c\u914d\u7f6eMapper\u6587\u4ef6\uff0c\u800c\u591a\u8868\u64cd\u4f5c\u7684\u9ebb\u70e6\u70b9\u5c31\u5728\u4e8e\u914d\u7f6e\u6587\u4ef6\uff0c\u8fd9\u91cc\u901a\u8fc7\u4f8b\u5b50\u7ec6\u8bf4\u4e00\u4e0b<\/p>\n<h5>1.\u5148\u628a\u8868\u5199\u597d<\/h5>\n<pre><code>CREATE TABLE orders (\nid INT PRIMARY KEY ,\nordertime VARCHAR(20) NOT NULL DEFAULT '',\ntotal DOUBLE,\nuid INT);\nINSERT INTO orders VALUES(1,2020,2000,1);\nINSERT INTO orders VALUES(2,2021,3000,2);\nINSERT INTO orders VALUES(3,2022,4000,3);\nCREATE TABLE USER (\nid INT PRIMARY KEY ,\nusername VARCHAR(50) NOT NULL DEFAULT '',\npasswords VARCHAR(50) NOT NULL DEFAULT '');\nINSERT INTO USER VALUES(1,'lyy',333);\nINSERT INTO USER VALUES(2,'myy',444);\nINSERT INTO USER VALUES(3,'xyy',555);<\/code><\/pre>\n<h5>2.\u5199Mapper\u914d\u7f6e\u6587\u4ef6<\/h5>\n<p>\u5728\u5199\u5b9e\u4f53\u7c7b\u65f6\uff0c\u8981\u628a\u4e00\u4e2a\u5b9e\u4f53\u5199\u5230\u53e6\u4e00\u4e2a\u5b9e\u4f53\u7684\u5c5e\u6027\u91cc\u9762\uff0c\u8fd9\u6837\u624d\u4f53\u73b0\u5173\u8054\u6027\uff0c\u5c31\u6bd4\u5982\u201c\u8ba2\u5355\u662f\u6240\u7528\u6237\u62e5\u6709\u7684\u201d\uff0c\u6b63\u56e0\u4e3a\u8fd9\u79cd\u5173\u7cfb\u6211\u4eec\u624d\u4f1a\u5728\u8ba2\u5355\u5b9e\u4f53\u7c7b\u91cc\u9762\u5199\u4e0a<code>private User user;<\/code>\u8fd9\u4e00\u5c5e\u6027\uff0c\u8fd9\u6837\u6839\u636eid\u8fde\u63a5\u7684\u4e24\u4e2a\u5b9e\u4f53\u624d\u80fd\u5b8c\u7f8e\u5bf9\u63a5\uff01<\/p>\n<p>\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2FTH\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u901a\u8fc7<code>&lt;association&gt;<\/code>\u628a\u4e24\u5f20\u8868\u5bf9\u5e94\u7684\u5b9e\u4f53\u7c7b\u8fde\u63a5\u8d77\u6765,\u53ea\u4e0d\u8fc7\u662f\u4e3b\u952eID\u8981\u7528\u5355\u72ec\u7684\u6807\u7b7e<\/p>\n<ul>\n<li><code>property<\/code>: \u5f53\u524d\u5b9e\u4f53(order)\u4e2d\u7684\u5c5e\u6027\u540d\u79f0(private User user)<\/li>\n<li><code>javaType<\/code>: \u5f53\u524d\u5b9e\u4f53(order)\u4e2d\u7684\u5c5e\u6027\u7684\u7c7b\u578b(User)<\/li>\n<\/ul>\n<p>\u8fd9\u4e24\u4e2auser\u6709\u7740\u672c\u8d28\u4e0a\u7684\u5374\u522b\uff0c\u5c31\u597d\u50cf\u524d\u8005\u662f\u5728\u4e00\u4e2a\u4eba\u7684\u540d\u5b57\uff0c\u540e\u8005\u6b63\u662f\u88ab\u53eb\u7684\u90a3\u4e2a\u4eba\uff0cMyBatis\u597d\u50cf\u5c31\u5229\u7528\u4e86\u8fd9\u4e00\u7279\u6027\uff0c\u901a\u8fc7\u6807\u7b7e\u7684\u5f62\u5f0f\u8fde\u63a5\u4e86\u4e24\u4e2a\u5b9e\u4f53<\/p>\n<pre><code>&lt;select id=&quot;findAll&quot; resultMap=&quot;orderMap&quot;&gt;\n   SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id\n&lt;\/select&gt;<\/code><\/pre>\n<p>SQL\u73af\u8282\u548c\u539f\u6765\u6ca1\u4ec0\u4e48\u533a\u522b\uff0c\u540c\u6837\u4e5f\u662f\u901a\u8fc7<code>resultMap<\/code>\u628a\u5b57\u6bb5\u548c\u5c5e\u6027\u6620\u5c04\u5c01\u88c5\u3002<\/p>\n<h3>2.\u4e00\u5bf9\u591a<\/h3>\n<p>\u4e00\u4e2a\u7528\u6237\u6709\u591a\u5f20\u8ba2\u5355<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2FTI\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u9996\u5148\uff0c\u5728\u539f\u6709\u7684User\u5b9e\u4f53\u4e2d\u5f97\u52a0\u4e0a\u4e00\u4e2a\u8868\u793a\u201c\u7528\u6237\u6709\u54ea\u4e9b\u8ba2\u5355\u7684\u5c5e\u6027\u201d<code>private List&lt;Order&gt; orderList;<\/code>\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u628a\u8ba2\u5355\u7684\u4fe1\u606f\u5c01\u88c5\u5230\u7528\u6237\u7684\u8fd9\u4e2a\u5c5e\u6027\u91cc\uff0c\u5728Mapper\u6587\u4ef6\u4e2d\u4f53\u73b0\uff1a<\/p>\n<pre><code>&lt;collection property=&quot;orderList&quot; ofType=&quot;order&quot;&gt;\n    &lt;!--\u5c01\u88c5order\u7684\u6570\u636e--&gt;\n    &lt;id column=&quot;oid&quot; property=&quot;id&quot;&gt;&lt;\/id&gt;\n    &lt;result column=&quot;ordertime&quot; property=&quot;ordertime&quot;&gt;&lt;\/result&gt;\n    &lt;result column=&quot;total&quot; property=&quot;total&quot;&gt;&lt;\/result&gt;\n&lt;\/collection&gt;<\/code><\/pre>\n<ul>\n<li><code>property<\/code>:\u96c6\u5408\u540d\u79f0\uff0cUser\u5b9e\u4f53\u4e2d\u7684orderlist\u5c5e\u6027<\/li>\n<li><code>ofType<\/code>\uff1a\u5f53\u524d\u96c6\u5408\u4e2d\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5c31\u662forder\u5b9e\u4f53<\/li>\n<\/ul>\n<p>\u7136\u540e\u5c31\u662f\u5199\u4e00\u5bf9\u591a\u7684SQL\uff1a<\/p>\n<pre><code>&lt;select id=&quot;findAll&quot; resultMap=&quot;userMap&quot;&gt;\n   SELECT *,o.id oid FROM USER u,orders o WHERE u.id=o.uid\n&lt;\/select&gt;<\/code><\/pre>\n<p>\u603b\u7ed3\u6765\u770b\uff0c\u4e00\u5bf9\u591a\u76f8\u6bd4\u4e8e\u4e00\u5bf9\u4e00\u5c31\u662f\u5728\u90a3\u4e2a\u201c\u4e00\u201d\u4e2d\u589e\u6dfb\u4e86\u5c01\u88c5\u201c\u591a\u201d\u7684\u5c5e\u6027\u800c\u5df2\uff0c\u7136\u540e\u7a0d\u5fae\u8c03\u6574\u4e00\u4e0bSQL<\/p>\n<h3>3.\u591a\u5bf9\u591a<\/h3>\n<p>\u591a\u7528\u6237\u591a\u89d2\u8272<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2FTJ\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u591a\u5bf9\u591a\u7684\u5efa\u8868\u539f\u5219\u662f\u5f15\u5165\u4e00\u5f20\u4e2d\u95f4\u8868\uff0c\u7528\u4e8e\u7ef4\u62a4\u5916\u952e\uff0c\u5c31\u662f\u4e00\u5f20\u8868\u901a\u8fc7\u4e2d\u95f4\u8868\u627e\u5230\u53e6\u4e00\u5f20\u8868<\/p>\n<p>\u548c\u4e00\u5bf9\u591a\u7684\u6a21\u578b\u7c7b\u4f3c\uff0c\u5148\u5728User\u5b9e\u4f53\u7c7b\u4e2d\u589e\u6dfb\u4e00\u4e2a\u201c\u7528\u6237\u5177\u5907\u54ea\u4e9b\u89d2\u8272\u201d\u7684\u5c5e\u6027private List&lt;Role&gt; roleList;\u5176\u6b21\u914d\u7f6eMapper\u6587\u4ef6\uff1a<\/p>\n<pre><code>&lt;collection property=&quot;roleList&quot; ofType=&quot;role&quot;&gt;\n   &lt;id column=&quot;roleId&quot; property=&quot;id&quot;&gt;&lt;\/id&gt;\n   &lt;result column=&quot;roleName&quot; property=&quot;roleName&quot;&gt;&lt;\/result&gt;\n   &lt;result column=&quot;roleDesc&quot; property=&quot;roleDesc&quot;&gt;&lt;\/result&gt;\n&lt;\/collection&gt;<\/code><\/pre>\n<p>\u591a\u8868\u7684\u8fde\u63a5\u662f\u9760\u4e2d\u95f4\u8868\uff0c\u8fd9\u70b9\u5728Mapper\u6587\u4ef6\u4e2d\u901a\u8fc7\u6620\u5c04\u5b9e\u73b0\uff0c\u5177\u4f53\u662f\u628a\u4e24\u5f20\u5916\u8868\u7684id\uff08userId\u548croleId\uff09\u5728id\u6807\u7b7e\u4e2d\u914d\u7f6e\u6210\u540c\u4e00\u4e2a\u5c5e\u6027\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre><code>&lt;id column=&quot;userId&quot; property=&quot;id&quot;&gt;&lt;\/id&gt;\n&lt;id column=&quot;roleId&quot; property=&quot;id&quot;&gt;&lt;\/id&gt;<\/code><\/pre>\n<p>SQL\u73af\u8282\u5c31\u5f97\u7528\u591a\u5bf9\u591a\u7684\u5957\u8def\u4e86<\/p>\n<pre><code>&lt;select id=&quot;findUserAndRoleAll&quot; resultMap=&quot;userRoleMap&quot;&gt;\n    SELECT * FROM USER u,user-role ur,role r WHERE u.id=ur.userId AND ur.roleId=r.id\n&lt;\/select&gt;<\/code><\/pre>\n<p>\u56de\u60f3\u8fdb\u884c\u591a\u8868\u64cd\u4f5c\u65f6MyBatis\u4e3a\u6211\u4eec\u5e26\u6765\u4e86\u4ec0\u4e48\uff1f\u4ed6\u786e\u5b9e\u51cf\u5c11\u4e86\u5f88\u591a\u786c\u7f16\u7801\uff0c\u6211\u6bcf\u4e00\u6b21\u65b0\u7684SQL\u53ea\u9700\u8981\u5728\u6807\u7b7e\u91cc\u6539\u51e0\u4e2a\u5c5e\u6027\u5c31\u53ef\u4ee5\uff0c\u53ea\u8981\u7406\u6e05\u5b57\u6bb5\u4e0e\u5c5e\u6027\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u5728MyBatis\u4e2d\u8fdb\u884c\u591a\u8868\u64cd\u4f5c\u5c31\u662f\u4e00\u4e2a\u201c\u5bf9\u53f7\u5165\u5ea7\u201d\u3002<\/p>\n<h2>\u56db\u3001\u6ce8\u89e3\u5f00\u53d1<\/h2>\n<p>\u9488\u5bf9\u4e8e\u7b80\u5355\u7684CRUD\u6ce8\u89e3\u5f00\u53d1\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u5347\u6548\u7387\uff0c\u987e\u540d\u601d\u4e49\u5c31\u662f\u628aSQL\u5199\u5728\u6ce8\u89e3\u91cc<\/p>\n<p>\u67e5\u8be2\uff08@Select\uff09\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2FTK\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6dfb\u52a0\uff08@Insert\uff09\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2FTL\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4fee\u6539\uff08@Update\uff09:<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2FTM\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5220\u9664\uff08@Delete\uff09 \uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2FTN\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a href=\"https:\/\/link.segmentfault.com\/?enc=vAxU1xnKLADoIDALhPAU5w%3D%3D.5utJ4BDzT9GdQKEKyfrX2mDAt%2FenIRcsIuXeLn3PRjY%3D\" rel=\"nofollow\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a href=\"https:\/\/link.segmentfault.com\/?enc=rB56k%2FxSalM1WYFhrhOhww%3D%3D.xezcrbrFhkO2bjHd1fGC0ITKaLEuKYPoCCvMmalvi70KAMKcwGAElML9vyseq%2BOA6EeaXFLd%2FD8%2BFFe1GVmV3g%3D%3D\" rel=\"nofollow\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a href=\"https:\/\/link.segmentfault.com\/?enc=80PhxTMbT5L8iHkN6XnqFg%3D%3D.u7YLiAfBtgf9PFTCuXdtOguuuMZk30w6iDp8xoGlMeOehw6uQidtHXxxxdQGbE7c\" rel=\"nofollow\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a href=\"https:\/\/link.segmentfault.com\/?enc=kpCAEUJrVzRQmvNy9RfyjA%3D%3D.VDGZhYAKjJ6PIrjpzRTwY9RQzo2HU0uFeWNLxDntFHQ%2F%2B9eVs1FJnL9318iLwrEfL8zzUaAmQBerhuKjSCc9Yg%3D%3D\" rel=\"nofollow\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a href=\"https:\/\/link.segmentfault.com\/?enc=pZB35PK5CqxSgPgI8uhOKg%3D%3D.qyaGdb%2FqljGWEMK9K6TsOAfnPhj31Ujp0h2SvX1pzFnIODTK0zT99HqPTudQsgOcuAfYJ%2BzvKbjUoCk8w%2BioHv35bkaeSITrPURvRef3Gl4%3D\" rel=\"nofollow\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042544378\">https:\/\/segmentfault.com\/a\/1190000042544378<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6765\u6e90\uff1aiyu77.blog.csdn.net\/article\/details\/125761737 MyBati [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006600","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006600"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006600\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}