{"id":91,"date":"2026-01-07T09:31:24","date_gmt":"2026-01-07T01:31:24","guid":{"rendered":"http:\/\/wp.buildyou.top\/?p=91"},"modified":"2026-01-07T09:31:26","modified_gmt":"2026-01-07T01:31:26","slug":"mybatis%e6%89%b9%e9%87%8f%e6%9f%a5%e8%af%a2%e5%a4%9a%e6%9d%a1%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/wp.buildyou.top\/index.php\/2026\/01\/07\/mybatis%e6%89%b9%e9%87%8f%e6%9f%a5%e8%af%a2%e5%a4%9a%e6%9d%a1%e4%bb%b6\/","title":{"rendered":"mybatis\u6279\u91cf\u67e5\u8be2\u591a\u6761\u4ef6"},"content":{"rendered":"\n<p>\u5728 MyBatis \u4e2d\u7f16\u5199 <code>IN<\/code> \u5b50\u53e5\uff0c\u6700\u6838\u5fc3\u7684\u5de5\u5177\u662f <strong><code>&lt;foreach&gt;<\/code><\/strong> \u6807\u7b7e\u3002\u6839\u636e\u60a8\u7684\u9700\u6c42\uff0c\u901a\u5e38\u5206\u4e3a\u4e24\u79cd\u573a\u666f\uff1a<strong>\u5355\u5b57\u6bb5 <code>IN<\/code><\/strong>\uff08\u5982 <code>id IN (1,2,3)<\/code>\uff09\u548c <strong>\u591a\u5b57\u6bb5\u7ec4\u5408 <code>IN<\/code><\/strong>\uff08\u5982 <code>(id, name) IN ((1,'\u5f20\u4e09'), (2,'\u674e\u56db'))<\/code>\uff09\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5355\u5b57\u6bb5 IN\uff08\u6700\u5e38\u89c1\uff09<\/h3>\n\n\n\n<p>\u5982\u679c\u60a8\u53ea\u662f\u4f20\u5165\u4e00\u4e2a List \u6216\u6570\u7ec4\uff0c\u67e5\u8be2\u67d0\u4e2a\u5b57\u6bb5\u662f\u5426\u5728\u96c6\u5408\u4e2d\uff1a<\/p>\n\n\n\n<p><strong>Java \u63a5\u53e3\uff1a<\/strong><\/p>\n\n\n\n<p>Java<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;User&gt; selectByIds(@Param(\"idList\") List&lt;Long&gt; idList);\n<\/code><\/pre>\n\n\n\n<p><strong>XML \u6620\u5c04\u6587\u4ef6\uff1a<\/strong><\/p>\n\n\n\n<p>XML<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;select id=\"selectByIds\" resultType=\"User\"&gt;\n    SELECT * FROM sys_user\n    WHERE id IN\n    &lt;foreach collection=\"idList\" item=\"id\" open=\"(\" separator=\",\" close=\")\"&gt;\n        #{id}\n    &lt;\/foreach&gt;\n&lt;\/select&gt;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u591a\u5b57\u6bb5\u7ec4\u5408 IN (Tuple IN)<\/h3>\n\n\n\n<p>\u5982\u679c\u60a8\u9700\u8981\u540c\u65f6\u5339\u914d\u591a\u4e2a\u5b57\u6bb5\uff08\u4f8b\u5982\uff1a\u6839\u636e\u201c\u7f16\u7801\u201d\u548c\u201c\u7c7b\u578b\u201d\u4e24\u4e2a\u7ef4\u5ea6\u9501\u5b9a\u4e00\u7ec4\u6570\u636e\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528 SQL \u7684\u5143\u7ec4\u8bed\u6cd5\u3002<\/p>\n\n\n\n<p><strong>Java \u63a5\u53e3\uff1a<\/strong><\/p>\n\n\n\n<p>Java<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5047\u8bbe ProductDTO \u5305\u542b id \u548c category \u5c5e\u6027\nList&lt;Product&gt; selectByCompositeKeys(@Param(\"productList\") List&lt;ProductDTO&gt; productList);\n<\/code><\/pre>\n\n\n\n<p><strong>XML \u6620\u5c04\u6587\u4ef6\uff1a<\/strong><\/p>\n\n\n\n<p>XML<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;select id=\"selectByCompositeKeys\" resultType=\"Product\"&gt;\n    SELECT * FROM product\n    WHERE (id, category) IN\n    &lt;foreach collection=\"productList\" item=\"item\" open=\"(\" separator=\",\" close=\")\"&gt;\n        (#{item.id}, #{item.category})\n    &lt;\/foreach&gt;\n&lt;\/select&gt;\n<\/code><\/pre>\n\n\n\n<p><em>\u6ce8\uff1a\u8fd9\u79cd\u8bed\u6cd5\u5728 MySQL\u3001PostgreSQL\u3001Oracle \u4e2d\u901a\u5e38\u652f\u6301\u826f\u597d\uff0c\u4f46\u5728 SQL Server \u4e2d\u53ef\u80fd\u4e0d\u652f\u6301\uff0c\u9700\u8981\u6539\u5199\u4e3a\u591a\u7ec4 <code>OR<\/code> \u8fde\u63a5\u3002<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <code>&lt;foreach&gt;<\/code> \u6807\u7b7e\u53c2\u6570\u8be6\u89e3<\/h3>\n\n\n\n<p>\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u4e0d\u51fa\u9519\uff0c\u7406\u89e3\u8fd9\u51e0\u4e2a\u5c5e\u6027\u975e\u5e38\u91cd\u8981\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table class=\"has-fixed-layout\"><thead><tr><td><strong>\u5c5e\u6027<\/strong><\/td><td><strong>\u8bf4\u660e<\/strong><\/td><\/tr><\/thead><tbody><tr><td><strong>collection<\/strong><\/td><td>\u5fc5\u987b\u3002\u6307\u5b9a\u4f20\u5165\u7684\u96c6\u5408\u53c2\u6570\u540d\u3002\u82e5\u6ca1\u7528 <code>@Param<\/code>\uff0cList \u9ed8\u8ba4\u4e3a <code>list<\/code>\uff0c\u6570\u7ec4\u4e3a <code>array<\/code>\u3002<\/td><\/tr><tr><td><strong>item<\/strong><\/td><td>\u5fc5\u987b\u3002\u96c6\u5408\u4e2d\u5143\u7d20\u8fed\u4ee3\u65f6\u7684\u522b\u540d\uff0c\u5728 <code>#{}<\/code> \u4e2d\u4f7f\u7528\u3002<\/td><\/tr><tr><td><strong>open<\/strong><\/td><td>\u5faa\u73af\u5f00\u59cb\u65f6\u7684\u5b57\u7b26\u4e32\uff08\u901a\u5e38\u662f <code>(<\/code>\uff09\u3002<\/td><\/tr><tr><td><strong>separator<\/strong><\/td><td>\u6bcf\u6b21\u5faa\u73af\u4e4b\u95f4\u7684\u5206\u9694\u7b26\uff08\u901a\u5e38\u662f <code>,<\/code>\uff09\u3002<\/td><\/tr><tr><td><strong>close<\/strong><\/td><td>\u5faa\u73af\u7ed3\u675f\u65f6\u7684\u5b57\u7b26\u4e32\uff08\u901a\u5e38\u662f <code>)<\/code>\uff09\u3002<\/td><\/tr><tr><td><strong>index<\/strong><\/td><td>\u53ef\u9009\u3002\u5f53\u524d\u8fed\u4ee3\u7684\u7d22\u5f15\u503c\uff08Map \u65f6\u4e3a Key\uff09\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udca1 \u8fdb\u9636\u907f\u5751\u6307\u5357<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1. \u96c6\u5408\u4e3a\u7a7a\u7684\u5224\u7a7a\u5904\u7406<\/strong><\/h4>\n\n\n\n<p>\u5982\u679c idList \u4e3a\u7a7a\uff0c\u751f\u6210\u7684 SQL \u4f1a\u53d8\u6210 WHERE id IN ()\uff0c\u8fd9\u4f1a\u5bfc\u81f4 SQL \u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n\n\n\n<p>\u5efa\u8bae\u5199\u6cd5\uff1a<\/p>\n\n\n\n<p>XML<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;where&gt;\n    &lt;if test=\"idList != null and idList.size() &gt; 0\"&gt;\n        AND id IN\n        &lt;foreach collection=\"idList\" item=\"id\" open=\"(\" separator=\",\" close=\")\"&gt;\n            #{id}\n        &lt;\/foreach&gt;\n    &lt;\/if&gt;\n    &lt;if test=\"idList == null or idList.size() == 0\"&gt;\n        AND 1 = 0  &lt;\/if&gt;\n&lt;\/where&gt;\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2. \u96c6\u5408\u957f\u5ea6\u9650\u5236<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>MySQL:<\/strong> <code>max_allowed_packet<\/code> \u9650\u5236\u4e86 SQL \u7684\u5927\u5c0f\uff0c\u867d\u7136\u53ef\u4ee5\u8bbe\u7f6e\u5f88\u5927\uff0c\u4f46 <code>IN<\/code> \u91cc\u7684\u5143\u7d20\u8fc7\u591a\uff08\u5982\u8d85\u8fc7 1000 \u4e2a\uff09\u4f1a\u5bfc\u81f4\u6027\u80fd\u6025\u5267\u4e0b\u964d\u3002<\/li>\n\n\n\n<li><strong>Oracle:<\/strong> <code>IN<\/code> \u5217\u8868\u4e2d\u7684\u8868\u8fbe\u5f0f\u6570\u91cf\u786c\u9650\u5236\u4e3a <strong>1000<\/strong> \u4e2a\u3002<\/li>\n\n\n\n<li><strong>\u65b9\u6848\uff1a<\/strong> \u5982\u679c\u6570\u636e\u91cf\u6781\u5927\uff0c\u5efa\u8bae\u5206\u6279\u67e5\u8be2\uff0c\u6216\u8005\u5c06\u6570\u636e\u63d2\u5165<strong>\u4e34\u65f6\u8868<\/strong>\u8fdb\u884c <code>JOIN<\/code> \u67e5\u8be2\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3. \u6027\u80fd\u4f18\u5316<\/strong><\/h4>\n\n\n\n<p>\u5982\u679c\u60a8\u53ea\u9700\u8981\u67e5\u8be2\u4e00\u4e2a\u5b57\u6bb5\u4e14\u4e0d\u9700\u8981\u53bb\u91cd\uff0c\u76f4\u63a5\u4f7f\u7528 <code>IN<\/code> \u5373\u53ef\u3002\u4f46\u5982\u679c\u5217\u8868\u975e\u5e38\u957f\uff0c\u8003\u8651\u4f7f\u7528 <strong><code>EXISTS<\/code><\/strong> \u914d\u5408\u5b50\u67e5\u8be2\uff0c\u6709\u65f6\u6027\u80fd\u4f1a\u66f4\u4f18\u3002<\/p>\n\n\n\n<p><strong>\u60a8\u76ee\u524d\u7684\u5217\u8868\u6570\u636e\u91cf\u5927\u6982\u5728\u4ec0\u4e48\u7ea7\u522b\uff1f\u5982\u679c\u8d85\u8fc7 1000 \u6761\uff0c\u6211\u5efa\u8bae\u4e3a\u60a8\u63d0\u4f9b\u5206\u6279\u5904\u7406\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 MyBatis \u4e2d\u7f16\u5199 IN \u5b50\u53e5\uff0c\u6700\u6838\u5fc3\u7684\u5de5\u5177\u662f &lt;foreach&gt; \u6807\u7b7e\u3002\u6839\u636e\u60a8\u7684\u9700\u6c42\uff0c\u901a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[17],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-18","tag-17"],"_links":{"self":[{"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/posts\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/comments?post=91"}],"version-history":[{"count":1,"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":92,"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/posts\/91\/revisions\/92"}],"wp:attachment":[{"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}