Mybatis 的 collection 标签可以起到 group by 的作用? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
amiwrong123
V2EX    程序员

Mybatis 的 collection 标签可以起到 group by 的作用?

  •  
  •   amiwrong123 2020-09-26 16:21:04 +08:00 2056 次点击
    这是一个创建于 1853 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有一个 java 类;

    @Data @Alias("Customer") public class Customer { private String custName; private String address; private List<Order> orderList; } 
     <select id="getCustomer" resultMap="CustomerAndOrder"> select * from test.orders a, test.customers b where a.cust_id = b.cust_id </select> <resultMap id="CustomerAndOrder" type="Customer"> <result property="custName" column="cust_name"/> <result property="address" column="cust_address"/> <collection property="orderList" ofType="Order"> <result property="orderNum" column="order_num"/> <result property="orderDate" column="order_date"/> </collection> </resultMap> 

    getCustomer 方法是为了查询每个顾客,都有哪些订单。

    执行 getCustomer 方法后,可以看到这个链表查询起到了类似 group by 的作用,以 cust_name 和 cust_address 分组,将每个组内的订单信息弄进了一个集合内。类似于:

    select cust_name, cust_address, (select 组内各行的 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 

    大概是上面这个意思,但实际上 mysql 无法做到这种语句。

    1601108551(1)

    然后我想问的是:

    • 假设(select 组内各行的 order_num,order_date into a List)这种行为支持的话,上面这种查询是否等价于上面这 sql 语句?
    • 如果等价,出现在<result property="xxx" column="yyy"/>的 yyy 一定是 group by 的条件呗?

    另外,mybatis 官方文档的这个例子:

    <!-- 非常复杂的结果映射 --> <resultMap id="detailedBlogResultMap" type="Blog"> <constructor> <idArg column="blog_id" javaType="int"/> </constructor> <result property="title" column="blog_title"/> <association property="author" javaType="Author"> <id property="id" column="author_id"/> <result property="username" column="author_username"/> <result property="password" column="author_password"/> <result property="email" column="author_email"/> <result property="bio" column="author_bio"/> <result property="favouriteSection" column="author_favourite_section"/> </association> <collection property="posts" ofType="Post"> <id property="id" column="post_id"/> <result property="subject" column="post_subject"/> <association property="author" javaType="Author"/> <collection property="comments" ofType="Comment"> <id property="id" column="comment_id"/> </collection> <collection property="tags" ofType="Tag" > <id property="id" column="tag_id"/> </collection> <discriminator javaType="int" column="draft"> <case value="1" resultType="DraftPost"/> </discriminator> </collection> </resultMap> 

    是否可以认为其等价 sql 语句是 group by blog_title, author_id, author_username, author_password, author_email, author_bio, author_favourite_section?

    3 条回复    2020-09-27 17:23:14 +08:00
    amiwrong123
        1
    amiwrong123  
    OP
       2020-09-27 11:48:47 +08:00
    是我这个问题太低端了吗,还是大家都不喜欢用 mybaits 吗(刚看了隔壁 JPA 和 mybatis 对比贴子~)
    shaoyijiong
        2
    shaoyijiong  
       2020-09-27 16:58:02 +08:00
    其实是太长了没人想看完 包括我
    amiwrong123
        3
    amiwrong123  
    OP
       2020-09-27 17:23:14 +08:00
    @shaoyijiong
    哈哈哈,好吧。晚上我自己再试试吧。忽略最后的那个例子,其实帖子很短啊[手动笑哭]
    div class="sep20">
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2787 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 13:23 PVG 21:23 LAX 06:23 JFK 09:23
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86