{"id":924,"date":"2023-03-11T17:20:02","date_gmt":"2023-03-11T09:20:02","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=924"},"modified":"2023-04-29T15:47:43","modified_gmt":"2023-04-29T07:47:43","slug":"mybatis-query-list-results-in-batch-based-on-list","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/mybatis-query-list-results-in-batch-based-on-list\/","title":{"rendered":"MyBatis\u6839\u636eList\u6279\u91cf\u67e5\u8be2List\u7ed3\u679c"},"content":{"rendered":"<h2>\u57fa\u672c\u4f7f\u7528<\/h2>\n<p>\uff081\uff09 Mapper\u63a5\u53e3<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">\/**\n * \u6839\u636e\u7528\u6237id list\u6279\u91cf\u67e5\u8be2\u7528\u6237\n *\/\npublic List&lt;User&gt; selectByIds(@Param(&quot;userIds&quot;) List&lt;Long&gt; userIds);<\/code><\/pre>\n<p>\uff082\uff09 mapper.xml\u6587\u4ef6<\/p>\n<pre><code class=\"language-xml\">&lt;!-- \u6839\u636e\u7528\u6237id list\u6279\u91cf\u67e5\u8be2\u7528\u6237 --&gt;\n&lt;select id=&quot;selectByIds&quot; resultMap=&quot;UserResultMap&quot;&gt;\n    select * from user where user_id in \n    &lt;foreach collection=&quot;userIds&quot; item=&quot;userId&quot; open=&quot;(&quot; close=&quot;)&quot; separator=&quot;,&quot;&gt;\n    #{userId}\n   &lt;\/foreach&gt;\n&lt;\/select&gt;<\/code><\/pre>\n<p>foreach\u5143\u7d20\u7684\u5c5e\u6027\u4e3b\u8981\u6709<code>item<\/code>\uff0c<code>index<\/code>\uff0c<code>collection<\/code>\uff0c<code>open<\/code>\uff0c<code>separator<\/code>\uff0c<code>close<\/code><\/p>\n<p><code>item<\/code>\u8868\u793a\u96c6\u5408\u4e2d\u6bcf\u4e00\u4e2a\u5143\u7d20\u8fdb\u884c\u8fed\u4ee3\u65f6\u7684\u522b\u540d<br \/>\n<code>index<\/code>\u6307\u5b9a\u4e00\u4e2a\u540d\u5b57\uff0c\u7528\u4e8e\u8868\u793a\u5728\u8fed\u4ee3\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u6b21\u8fed\u4ee3\u5230\u7684\u4f4d\u7f6e<br \/>\n<code>open<\/code>\u8868\u793a\u8be5\u8bed\u53e5\u4ee5\u4ec0\u4e48\u5f00\u59cb<br \/>\n<code>separator<\/code>\u8868\u793a\u5728\u6bcf\u6b21\u8fdb\u884c\u8fed\u4ee3\u4e4b\u95f4\u4ee5\u4ec0\u4e48\u7b26\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26<br \/>\n<code>close<\/code>\u8868\u793a\u4ee5\u4ec0\u4e48\u7ed3\u675f<\/p>\n<p>\u5728\u4f7f\u7528foreach\u7684\u65f6\u5019\u6700\u5173\u952e\u7684\u4e5f\u662f\u6700\u5bb9\u6613\u51fa\u9519\u7684\u5c31\u662f<code>collection<\/code>\u5c5e\u6027\uff0c\u8be5\u5c5e\u6027\u662f\u5fc5\u987b\u6307\u5b9a\u7684\uff0c\u4f46\u662f\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\uff0c\u8be5\u5c5e\u6027\u7684\u503c\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b3\u79cd\u60c5\u51b5\uff1a\u00a0<\/p>\n<p>\uff081\uff09\u5982\u679c\u4f20\u5165\u7684\u662f\u5355\u53c2\u6570\u4e14\u53c2\u6570\u7c7b\u578b\u662f\u4e00\u4e2aList\u65f6\uff0ccollection\u5c5e\u6027\u7684\u9ed8\u8ba4\u503c\u4e3alist\uff08<code>@Param<\/code>\u672a\u6307\u5b9a\u65f6\u7684\u503c\uff09<br \/>\n\uff082\uff09\u5982\u679c\u4f20\u5165\u7684\u662f\u5355\u53c2\u6570\u4e14\u53c2\u6570\u7c7b\u578b\u662f\u4e00\u4e2aarray\u6570\u7ec4\u65f6\uff0ccollection\u7684\u5c5e\u7684\u9ed8\u8ba4\u503c\u4e3aarray\uff08<code>@Param<\/code>\u672a\u6307\u5b9a\u65f6\u7684\u503c\uff09<br \/>\n\uff083\uff09\u5982\u679c\u4f20\u5165\u7684\u53c2\u6570\u662f\u591a\u4e2a\u65f6\uff0c\u5c31\u9700\u8981\u628a\u5b83\u4eec\u5c01\u88c5\u6210\u4e00\u4e2aMap\uff0c\u5f53\u7136\u5355\u53c2\u6570\u4e5f\u53ef\u4ee5\u5c01\u88c5\u6210map\uff0c\u5b9e\u9645\u4e0a\u5728\u4f20\u5165\u53c2\u6570\u7684\u65f6\u5019\uff0c\u5728MyBatis\u91cc\u9762\u4e5f\u662f\u4f1a\u628a\u5b83\u5c01\u88c5\u6210\u4e00\u4e2aMap\u7684\uff0cmap\u7684key\u5c31\u662f\u53c2\u6570\u540d\uff0c\u6240\u4ee5\u8fd9\u4e2a\u65f6\u5019collection\u5c5e\u6027\u503c\u5c31\u662f\u4f20\u5165\u7684List\u6216array\u5bf9\u8c61\u5728\u81ea\u5df1\u5c01\u88c5\u7684map\u91cc\u9762\u7684key<\/p>\n<h2>\u6269\u5c55\u4f7f\u7528<\/h2>\n<pre><code class=\"language-java\">List&lt;PaymentOrder&gt; selectPaymentOrderList(@Param(&quot;payOrderNoList&quot;) List&lt;Long&gt; payOrderNoList);<\/code><\/pre>\n<pre><code class=\"language-xml\">&lt;!-- \u81ea\u5b9a\u4e49Mapper --&gt;\n&lt;select id=&quot;selectPaymentOrderList&quot; parameterType=&quot;java.util.List&quot; resultMap=&quot;BaseResultMap&quot;&gt;\n  select\n  &lt;include refid=&quot;Base_Column_List&quot;\/&gt;\n  from payment_order o\n  where\n  o.pay_order_no in\n  &lt;foreach item=&quot;payOrderNo&quot; index=&quot;index&quot; collection=&quot;payOrderNoList&quot;\n            open=&quot;(&quot; separator=&quot;,&quot; close=&quot;)&quot;&gt;\n    #{payOrderNo}\n  &lt;\/foreach&gt;\n&lt;\/select&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u672c\u4f7f\u7528 \uff081\uff09 Mapper\u63a5\u53e3 \/** * \u6839\u636e\u7528\u6237id list\u6279\u91cf\u67e5\u8be2\u7528\u6237 *\/ public Lis [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[111],"class_list":["post-924","post","type-post","status-publish","format-standard","hentry","category-mybatis","tag-mybatis"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=924"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/924\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}