{"id":691,"date":"2023-02-26T14:55:46","date_gmt":"2023-02-26T06:55:46","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=691"},"modified":"2023-04-29T17:56:32","modified_gmt":"2023-04-29T09:56:32","slug":"using-mybatis-to-query-data-and-sort-it-in-specific-order","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/using-mybatis-to-query-data-and-sort-it-in-specific-order\/","title":{"rendered":"\u4f7f\u7528MyBatis\u67e5\u8be2\u6570\u636e\uff0c\u6309\u7279\u5b9a\u987a\u5e8f\u6392\u5e8f"},"content":{"rendered":"<p>\u6709\u5982\u4e0b\u8868<code>table_city<\/code><\/p>\n<pre><code>id      city\n1      beijing\n2      shanghai\n3      hanghzou\n4      shenzhen<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u73b0\u5728\u5c06\u67e5\u8be2\u51fa\u7684\u6570\u636e\u6309\u7167id 3\u30014\u30011\u30012\u6392\u5e8f<\/p>\n<p>\u5148\u628aid\u6570\u636e\u6309\u7167\u4e00\u5b9a\u987a\u5e8f\u653e\u5230\u4e00\u4e2aList\u4e2d<\/p>\n<pre><code class=\"language-java\">List&lt;Integer&gt; ids = new ArrayList&lt;Integer&gt;();\nids.add(3);\nids.add(4);\nids.add(1);\nids.add(2);<\/code><\/pre>\n<p>MyBatis mapper sql\u5982\u4e0b<\/p>\n<pre><code class=\"language-xml\">&lt;select id=&quot;findHistory&quot; resultMap=&quot;recentlyBrowse&quot;&gt;\n    select * from\n    &lt;foreach collection=&quot;list&quot; item=&quot;id&quot; index=&quot;index&quot;\n        open=&quot;(&quot; close=&quot;)&quot; separator=&quot;union all&quot;&gt;\n        select * from table_city where id=#{id,jdbcType=DECIMAL}\n    &lt;\/foreach&gt; as city\n&lt;\/select&gt;<\/code><\/pre>\n<p>\u628aids\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u67e5\u8be2\u7684dao\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">@Autowired\nSqlSessionTemplate sql;\n\npublic List&lt;People&gt; findHistory(List&lt;Integer&gt; ids) {\n    return sql.selectList(NAMESPACE + &quot;findHistory&quot;, ids);\n}<\/code><\/pre>\n<p>\u5bf9\u4e8eMapper\u63a5\u53e3\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">public interface TableCityMapper {\n    List&lt;City&gt; findHistory(@Param(&quot;list&quot;) List&lt;Integer&gt; ids);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u5982\u4e0b\u8868table_city id city 1 beijing 2 shanghai 3 hanghzou  [&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-691","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\/691","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=691"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/691\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}