有一个 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 无法做到这种语句。
然后我想问的是:
另外,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?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.