一、介绍
resultType :
作用:将查询结果按照sql列名pojo属性名一致性映射到pojo中。
场合:
常见一些明细记录的展示,比如用户购买商品明细,将关联查询信息全部展示在页面时,此时可直接使用 resultType 将每一条记录映射到 pojo 中,在前端页面遍历 list ( list 中是 pojo )即可。
resultMap :
使用 association 和 collection 完成一对一和一对多高级映射(对结果有特殊的映射要求)。
association :
作用:将关联查询信息映射到一个pojo对象中。(可将多个属性封装到一个java对象中,在结果集对象添加该java对象属性)
场合:
为了方便查询关联信息可以使用 association 将关联订单信息映射为用户对象的 pojo 属性中,比如:查询订单及关联用户信息。
使用 resultType 无法将查询结果映射到 pojo 对象的 pojo 属性中,根据对结果集查询遍历的需要选择使用 resultType 还是 resultMap 。
collection :
作用:将关联查询信息映射到一个list集合中。(如订单信息详情,封装到一个list中,在结果集对象中添加某java对象的List属性)
场合:
为了方便查询遍历关联信息可以使用 collection 将关联信息映射到 list 集合中,比如:查询用户权限范围模块及模块下的菜单,可使用 collection 将模块映射到模块 list 中,将菜单列表映射到模块对象的菜单 list 属性中,这样的作的目的也是方便对查询结果集进行遍历查询。
如果使用 resultType 无法将查询结果映射到 list 集合中。
二、一对一查询
resultType 和 resultMap 实现一对一查询小结:
a.resultType :使用 resultType 实现较为简单,如果 pojo 中没有包括查询出来的列名,需要增加列名对应的属性,即可完成映射。
b.如果没有查询结果的特殊要求建议使用 resultType 。
c.resultMap :需要单独定义 resultMap ,实现有点麻烦,如果对查询结果有特殊的要求,使用 resultMap 可以完成将关联查询映射 pojo 的属性中。
d.resultMap 可以实现延迟加载, resultType 无法实现延迟加载。
2.1.需求:查询订单信息,关联查询用户信息;
2.2 resultType实现
public class Orders {
/** 主键订单Id */
private Integer id;
/** 下单用户id */
private Integer userid;
/** 订单号 */
private String number;
/** 创建订单时间 */
private Date createTime;
/** 备注 */
private String note;
private User user;
private List<OrderDetail> orderdetails;
}
public class OrdersCustom extends Orders {
private String username;
private String sex;
private String address;
}
public interface OrdersCustomMapper {
/** 查询订单,关联查询用户信息 */
public List<OrdersCustom> findOrdersUser();
}
- 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
- 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
<mapper namespace="com.mybatis.mapper.OrdersCustomMapper">
<select id="findOrdersUser" resultType="com.mybatis.entity.OrdersCustom">
SELECT t1.*,
t2.username,
t2.sex,
t2.address
FROM
orders t1,
t_user t2
WHERE t1.user_id=t2.id
</select>
</mapper>
2.3 resultMap实现
<resultMap type="com.mybatis.entity.Orders" id="OrdersUserResultMap">
<id column="id" property="id"/>
<result column="user_id" property="userid"/>
<result column="number" property="number"/>
<result column="createtime" property="createTime"/>
<result column="note" property="note"/>
<association property="user" javaType="com.mybatis.entity.User">
<id column="user_id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<result column="address" property="address"/>
</association>
</resultMap>
- 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
- 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
<select id="findOrdersUserResultMap" resultMap="OrdersUserResultMap">
SELECT t1.*,
t2.username,
t2.sex,
t2.address
FROM
orders t1,
t_user t2
WHERE t1.user_id=t2.id
</select>
三、一对多查询
mybatis 使用 resultMap 的 collection 对关联查询的多条记录映射到一个 list 集合属性中。
使用 resultType 实现:将订单明细映射到orders中的orderdetails中,需要自己处理,使用双重循环遍历,去掉重复记录,将订单明细放在orderdetails中。
3.1 需求:查询订单(关联用户)及订单明细;
3.2 映射思路
3.2. 在 orders.java 类中添加 List orderDetails 属性(上面实体已添加) 。
最终会将订单信息映射到 orders 中,订单所对应的订单明细映射到 orders 中的 orderDetails 属性中.
3.3 核心代码
<select id="findOrdersAndOrderDetailResultMap" resultMap="ordersAndOrderDetailResultMap">
SELECT
t1.*,
t2.username,
t2.sex,
t2.address,
t3.id orderdetail_id,
t3.items_id,
t3.items_num,
t3.orders_id
FROM
orders t1,
t_user t2,
orderdetail t3
WHERE t1.user_id = t2.id AND t3.orders_id=t1.id
</select>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
<resultMap type="com.mybatis.entity.Orders" id="ordersAndOrderDetailResultMap" extends="OrdersUserResultMap">
=======================================================================================================
<collection property="orderdetails" ofType="com.mybatis.entity.OrderDetail">
<id column="orderdetail_id" property="id"/>
<result column="items_id" property="itemsId"/>
<result column="items_num" property="itemsNum"/>
<result column="orders_id" property="ordersId"/>
</collection>
=========================================================================================================
</resultMap>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
四、多对多查询
4.1.需求:查询用户以及用户购买的商品信息
4.2.映射思路
将用户信息映射到 user 中。
在 user 类中添加订单列表属性 List orderslist ,将用户创建的订单映射到 orderslist;
在 Orders 中添加订单明细列表属性 Listorderdetials ,将订单的明细映射到 orderdetials;
在 OrderDetail 中添加 Items 属性,将订单明细所对应的商品映射到 Item;
4.3 核心代码
<resultMap type="com.mybatis.entity.User" id="userAndItemsResultMap">
<id column="user_id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<result column="address" property="address"/>
<collection property="ordersList" ofType="com.mybatis.entity.Orders">
<id column="id" property="id"/>
<result column="user_id" property="userid"/>
<result column="number" property="number"/>
<result column="createtime" property="createTime"/>
<result column="note" property="note"/>
<collection property="orderdetails" ofType="com.mybatis.entity.OrderDetail">
<id column="orderdetail_id" property="id"/>
<result column="items_id" property="itemsId"/>
<result column="items_num" property="itemsNum"/>
<result column="orders_id" property="ordersId"/>
<association property="items" javaType="com.mybatis.entity.Items">
<id column="items_id" property="id"/>
<result column="items_name" property="itemsName"/>
<result column="items_detail" property="detail"/>
<result column="items_price" property="price"/>
</association>
</collection>
</collection>
</resultMap>
<select id="findUserAndItemsResultMap" resultMap="userAndItemsResultMap">
SELECT
t1.*,
t2.username,
t2.sex,
t2.address,
t3.id orderdetail_id,
t3.items_id,
t3.items_num,
t3.orders_id,
t4.itemsname items_name,
t4.detail items_detail,
t4.price items_price
FROM
orders t1,
t_user t2,
orderdetail t3,
items t4
WHERE t1.user_id = t2.id AND t3.orders_id=t1.id AND t3.items_id = t4.id
</select>
- 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
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 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
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 5