{"id":161713,"date":"2020-09-19T14:39:37","date_gmt":"2020-09-19T06:39:37","guid":{"rendered":"http:\/\/4563.org\/?p=161713"},"modified":"2020-09-19T14:39:37","modified_gmt":"2020-09-19T06:39:37","slug":"mybatis-%e7%9a%84-collection-%e6%a0%87%e7%ad%be%e5%8f%af%e4%bb%a5%e8%b5%b7%e5%88%b0-group-by-%e7%9a%84%e4%bd%9c%e7%94%a8%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=161713","title":{"rendered":"Mybatis \u7684 collection \u6807\u7b7e\u53ef\u4ee5\u8d77\u5230 group by \u7684\u4f5c\u7528\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Mybatis \u7684 collection \u6807\u7b7e\u53ef\u4ee5\u8d77\u5230 group by \u7684\u4f5c\u7528\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6709\u4e00\u4e2a java \u7c7b\uff1b<\/p>\n<pre><code>@Data @Alias(\"Customer\") public class Customer {     private String custName;     private String address;     private List&lt;Order&gt; orderList; } <\/code><\/pre>\n<pre><code>    &lt;select id=\"getCustomer\" resultMap=\"CustomerAndOrder\"&gt;         select * from test.orders a, test.customers b where a.cust_id = b.cust_id     &lt;\/select&gt;          &lt;resultMap id=\"CustomerAndOrder\" type=\"Customer\"&gt;         &lt;result property=\"custName\" column=\"cust_name\"\/&gt;         &lt;result property=\"address\" column=\"cust_address\"\/&gt;         &lt;collection property=\"orderList\" ofType=\"Order\"&gt;             &lt;result property=\"orderNum\" column=\"order_num\"\/&gt;             &lt;result property=\"orderDate\" column=\"order_date\"\/&gt;         &lt;\/collection&gt;     &lt;\/resultMap&gt; <\/code><\/pre>\n<p>getCustomer \u65b9\u6cd5\u662f\u4e3a\u4e86\u67e5\u8be2\u6bcf\u4e2a\u987e\u5ba2\uff0c\u90fd\u6709\u54ea\u4e9b\u8ba2\u5355\u3002<\/p>\n<p>\u6267\u884c getCustomer \u65b9\u6cd5\u540e\uff0c\u53ef\u4ee5\u770b\u5230\u8fd9\u4e2a\u94fe\u8868\u67e5\u8be2\u8d77\u5230\u4e86\u7c7b\u4f3c group by \u7684\u4f5c\u7528\uff0c\u4ee5 cust_name \u548c cust_address \u5206\u7ec4\uff0c\u5c06\u6bcf\u4e2a\u7ec4\u5185\u7684\u8ba2\u5355\u4fe1\u606f\u5f04\u8fdb\u4e86\u4e00\u4e2a\u96c6\u5408\u5185\u3002\u7c7b\u4f3c\u4e8e\uff1a<\/p>\n<pre><code>select  cust_name,  cust_address, (select \u7ec4\u5185\u5404\u884c\u7684 order_num,order_date into a List) from test.orders a, test.customers b where a.cust_id = b.cust_id group by cust_name, cust_address <\/code><\/pre>\n<p>\u5927\u6982\u662f\u4e0a\u9762\u8fd9\u4e2a\u610f\u601d\uff0c\u4f46\u5b9e\u9645\u4e0a mysql \u65e0\u6cd5\u505a\u5230\u8fd9\u79cd\u8bed\u53e5\u3002<\/p>\n<p> <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/10\/20200930_5f74befb2184b.png\" alt=\"Mybatis \u7684 collection \u6807\u7b7e\u53ef\u4ee5\u8d77\u5230 group by \u7684\u4f5c\u7528\uff1f\" \/> <\/p>\n<p>\u7136\u540e\u6211\u60f3\u95ee\u7684\u662f\uff1a<\/p>\n<ul>\n<li>\u5047\u8bbe(select \u7ec4\u5185\u5404\u884c\u7684 order_num,order_date into a List)\u8fd9\u79cd\u884c\u4e3a\u652f\u6301\u7684\u8bdd\uff0c\u4e0a\u9762\u8fd9\u79cd\u67e5\u8be2\u662f\u5426\u7b49\u4ef7\u4e8e\u4e0a\u9762\u8fd9 sql \u8bed\u53e5\uff1f<\/li>\n<li>\u5982\u679c\u7b49\u4ef7\uff0c\u51fa\u73b0\u5728&lt;result property=&#8221;xxx&#8221; column=&#8221;yyy&#8221;\/&gt;\u7684 yyy \u4e00\u5b9a\u662f group by \u7684\u6761\u4ef6\u5457\uff1f<\/li>\n<\/ul>\n<p>\u53e6\u5916\uff0cmybatis \u5b98\u65b9\u6587\u6863\u7684\u8fd9\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre><code>&lt;!-- \u975e\u5e38\u590d\u6742\u7684\u7ed3\u679c\u6620\u5c04 --&gt; &lt;resultMap id=\"detailedBlogResultMap\" type=\"Blog\"&gt;  &lt;constructor&gt;    &lt;idArg column=\"blog_id\" javaType=\"int\"\/&gt;  &lt;\/constructor&gt;  &lt;result property=\"title\" column=\"blog_title\"\/&gt;  &lt;association property=\"author\" javaType=\"Author\"&gt;    &lt;id property=\"id\" column=\"author_id\"\/&gt;    &lt;result property=\"username\" column=\"author_username\"\/&gt;    &lt;result property=\"password\" column=\"author_password\"\/&gt;    &lt;result property=\"email\" column=\"author_email\"\/&gt;    &lt;result property=\"bio\" column=\"author_bio\"\/&gt;    &lt;result property=\"favouriteSection\" column=\"author_favourite_section\"\/&gt;  &lt;\/association&gt;  &lt;collection property=\"posts\" ofType=\"Post\"&gt;    &lt;id property=\"id\" column=\"post_id\"\/&gt;    &lt;result property=\"subject\" column=\"post_subject\"\/&gt;    &lt;association property=\"author\" javaType=\"Author\"\/&gt;    &lt;collection property=\"comments\" ofType=\"Comment\"&gt;      &lt;id property=\"id\" column=\"comment_id\"\/&gt;    &lt;\/collection&gt;    &lt;collection property=\"tags\" ofType=\"Tag\" &gt;      &lt;id property=\"id\" column=\"tag_id\"\/&gt;    &lt;\/collection&gt;    &lt;discriminator javaType=\"int\" column=\"draft\"&gt;      &lt;case value=\"1\" resultType=\"DraftPost\"\/&gt;    &lt;\/discriminator&gt;  &lt;\/collection&gt; &lt;\/resultMap&gt; <\/code><\/pre>\n<p>\u662f\u5426\u53ef\u4ee5\u8ba4\u4e3a\u5176\u7b49\u4ef7 sql \u8bed\u53e5\u662f group by blog_title, author_id, author_username, author_password, author_email, author_bio, author_favourite_section?<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3499446\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u662f\u6211\u8fd9\u4e2a\u95ee\u9898\u592a\u4f4e\u7aef\u4e86\u5417\uff0c\u8fd8\u662f\u5927\u5bb6\u90fd\u4e0d\u559c\u6b22\u7528 mybaits \u5417\uff08\u521a\u770b\u4e86\u9694\u58c1 JPA \u548c mybatis \u5bf9\u6bd4\u8d34\u5b50~\uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3499447\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : shaoyijiong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5176\u5b9e\u662f\u592a\u957f\u4e86\u6ca1\u4eba\u60f3\u770b\u5b8c \u5305\u62ec\u6211                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3499448\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @shaoyijiong <br \/>\u54c8\u54c8\u54c8\uff0c\u597d\u5427\u3002\u665a\u4e0a\u6211\u81ea\u5df1\u518d\u8bd5\u8bd5\u5427\u3002\u5ffd\u7565\u6700\u540e\u7684\u90a3\u4e2a\u4f8b\u5b50\uff0c\u5176\u5b9e\u5e16\u5b50\u5f88\u77ed\u554a[\u624b\u52a8\u7b11\u54ed]                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Mybatis \u7684 collect&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/161713"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=161713"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/161713\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=161713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=161713"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=161713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}