MyBatis 关联映射

这段时间在学习MyBatis的基础知识,所以想做个记录以便以后查阅。

MyBatis 关联映射

一对一

在本类中定义对方类型的对象,如A类中定义B类类型的属性b,B类中定义A类类型的属性a。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.xiao.po

public class IdCard {
private Integer id;
private String code;
// 省略getter、setter、toString方法
}
public class Person {
private Integer id;
private String name;
private Integer age;
private String sex;
private IdCard card;
// 省略getter、setter、toString方法
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!--IdCardMapper.xml-->
<mapper namespace="com.xiao.mapper.IdCardMapper">
<select id="findCodeById" parameterType="Integer" resultType="idCard">
select * from tb_idcard where id = #{id}
</select>
</mapper>

<!--PersonMapper.xml-->
<mapper namespace="com.xiao.mapper.PersonMapper">

<!-- 嵌套查询:通过执行另外一个SQL映射语句来返回预期的特殊类型 -->
<select id="findPersonById" parameterType="Integer" resultMap="IdCardWithPersonResult">
select * from tb_person where
id = #{id}
</select>

<resultMap type="person" id="IdCardWithPersonResult">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
<result property="sex" column="sex" />
<!-- 一对一:association使用select属性引入另外一条SQL语句 -->
<association property="card" column="card_id" javaType="idCard"
select="com.xiao.mapper.IdCardMapper.findCodeById" />
</resultMap>

<!-- 嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集-->
<select id="findPersonById2" parameterType="Integer" resultMap="IdCardWithPersonResult2">
select p.*, idcard.code
from tb_person p, tb_idcard idcard
where p.card_id = idcard.id
and p.id = #{id}
</select>

<resultMap type="Person" id="IdCardWithPersonResult2">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
<result property="sex" column="sex" />
<association property="card" javaType="IdCard">
<id property="id" column="card_id"/>
<result property="code" column="code"/>
</association>
</resultMap>

</mapper>

一对多

一个A类类型对应多个B类类型的情况,需要在A类中以集合的方式引入B类类型的对象,在B类中定义A类类型的属性a。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.xiao.po;

public class Orders {
private Integer id;
private String number;
// 省略getter、setter、toString方法
}

public class User {
private Integer id;
private String username;
private String address;
private List<Orders> ordersList;
// 省略getter、setter、toString方法
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!-- UserMapper.xml -->
<mapper namespace="com.xiao.mapper.UserMapper">
<select id="findUserWithOrders" parameterType="Integer" resultMap="UserWithOrdersResult">
select u.*, o.id as orders_id, o.number
from tb_user u, tb_orders o
where u.id = o.user_id
and u.id = #{id}
</select>
<resultMap type="User" id="UserWithOrdersResult">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="address" column="address"/>
<!-- 一对多关联映射:collection
ofType表示属性集合中元素的类型,List<Orders>属性即Orders类 -->
<collection property="ordersList" ofType="Orders">
<id property="id" column="orders_id"/>
<result property="number" column="number"/>
</collection>
</resultMap>
</mapper>

<!-- OrdersMapper.xml -->
<mapper namespace="com.xiao.mapper.OrdersMapper">
<select id="findOrdersWithUser" parameterType="Integer" resultMap="OrdersWithUserResult">
select o.*, u.id as user_id, u.username, u.address
from tb_user u, tb_orders o
where u.id = o.user_id
and o.id = #{id}
</select>
<resultMap type="Orders" id="OrdersWithUserResult">
<id property="id" column="id" />
<result property="number" column="number" />
<association property="user" javaType="User">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="address" column="address" />
</association>
</resultMap>
</mapper>

多对多

在A类中定义B类类型,在B类中定义A类类型的集合。

1
2
3
4
5
6
7
8
9
package com.xiao.po

public class Product {
private Integer id;
private String name;
private Double price;
private List<Orders> orders;
// 省略getter、setter、toString方法
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!-- OrdersMapper.xml -->
<mapper namespace="com.xiao.mapper.OrdersMapper">
<select id="findOrdersWithProduct" parameterType="Integer" resultMap="OrdersWithProductResult">
select o.*, p.id as pid, p.name, p.price
from tb_orders o, tb_product p, tb_ordersitem oi
where oi.orders_id = o.id
and oi.product_id = p.id
and o.id = #{id}
</select>
<resultMap type="Orders" id="OrdersWithProductResult">
<id property="id" column="id"/>
<result property="number" column="number"/>
<!-- 多对多关联映射 -->
<collection property="productList" ofType="Product">
<id property="id" column="pid"/>
<result property="name" column="name"/>
<result property="price" column="price"/>
</collection>
</resultMap>
</mapper>
文章作者: XIAO
文章链接: http://yoursite.com/2019/10/05/MyBatis-关联映射/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 XIAO
微信打赏
支付宝打赏