Mybatis返回类型
# Mybatis返回类型
# 返回普通数据
返回基本类型、普通对象 都只需要在 resultType 中声明返回值类型全类名即可
<select id="getUser" resultType="com.joe.entity.Employee">
select * from `user` where id = #{id}
</select>
<select id="countUser" resultType="java.lang.Long">
select count(*) from `user`
</select>
1
2
3
4
5
6
7
2
3
4
5
6
7
# 返回List、Map
List:resultType为集合中的元素类型
Map:resultType为map,配合@MapKey 指定哪一列的值作为Map的key,Map的Value为这一行数据的完整信息 Map<Key,Map>
- 返回单条记录的 Map:resultType="java.util.Map",MyBatis 自动将字段名作为key,字段值作为value
<select id="selectUserMapById" resultType="java.util.Map">
SELECT id, user_name, age,create_time
FROM user WHERE id = #{id}
</select>
1
2
3
4
2
3
4
- 返回多条记录的 Map:需配合
定义 key 字段,或用 @MapKey 注解
@MapKey("id") // 必须指定:用返回结果中的 "id" 字段作为 Map 的 key
Map<Long, User> selectAllUserMap();
<select id="selectAllUserMap" resultMap="UserMapResult">
SELECT id, user_name, age, create_time FROM user
</select>
1
2
3
4
5
6
2
3
4
5
6
# 自定义结果集 - ResultMap
数据库的字段,如果和Bean的属性不能一一对应,有两种办法
- 如果符合驼峰命名,则开启驼峰命名规则
- 编写自定义结果集(ResultMap)进行封装
- id 标签:必须指定主键列映射规则
- result 标签:指定普通列映射规则
<resultMap id="userResultMap" type="com.joe.entity.Employee">
<id column="user_id" property="userId"/>
<result column="user_name" property="userName"/>
<result column="user_salary" property="userSalary"/>
</resultMap>
<select id="getUser" resultMap="userResultMap">
select * from `user` where id = #{id}
</select>
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# association标签
association标签:指定自定义对象封装规则
<!-- 自定义结果集 -->
<resultMap id="OrderRM" type="com.joe.bean.Order">
<id column="id" property="id"></id>
<result column="address" property="address"></result>
<result column="amount" property="amount"></result>
<result column="customer_id" property="customerId"></result>
<!--一对一关联封装 -->
<association property="customer" javaType="com.joe.bean.Customer">
<id column="c_id" property="id"></id>
<result column="customer_name" property="customerName"></result>
<result column="phone" property="phone"></result>
</association>
</resultMap>
<!--查询订单以及订单的客户信息-->
<select id="getOrderByIdWithCustomer" resultMap="OrderRM">
select o.*,
c.id c_id,
c.customer_name,
c.phone
from t_order o
left join t_customer c on o.customer_id = c.id
where o.id = #{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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# association标签 分布查询
<!-- 分步查询:自定义结果集;封装订单的分步查询 -->
<resultMap id="OrderCustomerStepRM" type="com.joe.bean.Order">
<id column="id" property="id"></id>
<result column="address" property="address"></result>
<result column="amount" property="amount"></result>
<result column="customer_id" property="customerId"></result>
<!--
customer属性关联一个对象,启动下一次查询,查询这个客户
property: JaveBean 属性列
column:订单结果集中哪一列作为查询条件查询客户
select:查询客户的sql
-->
<association property="customer"
select="com.joe.mapper.OrderCustomerStepMapper.getCustomerById"
column="customer_id">
</association>
</resultMap>
<!-- 按照id查询客户 -->
<select id="getCustomerById" resultMap="com.joe.entity.Customer">
select * from t_customer where id = #{id}
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# collection标签
collection标签:指定自定义集合封装规则
<resultMap id="CutomerRM" type="com.joe.bean.Customer">
<id column="c_id" property="id"></id>
<result column="customer_name" property="customerName"></result>
<result column="phone" property="phone"></result>
<!--
collection:说明 一对N 的封装规则
ofType: 集合中元素的类型
-->
<collection property="orders" ofType="com.joe.bean.Order">
<id column="id" property="id"></id>
<result column="address" property="address"></result>
<result column="amount" property="amount"></result>
<result column="c_id" property="customerId"></result>
</collection>
</resultMap>
<!--查询客户以及客户的订单信息-->
<select id="getCustomerByIdWithOrders" resultMap="CutomerRM">
select c.id c_id,
c.customer_name,
c.phone,
o.*
from t_customer c
left join t_order o on c.id = o.customer_id
where c.id = #{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
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
# collection标签 分布查询
一对多:查询客户以及客户所有的订单信息
<!-- 分步查询的自定义结果集: -->
<resultMap id="CustomerOrdersStepRM" type="com.joe.entity.Customer">
<id column="id" property="id"></id>
<result column="customer_name" property="customerName"></result>
<result column="phone" property="phone"></result>
<collection property="orders"
select="com.joe.mapper.OrderCustomerStepMapper.getOrdersByCustomerId"
column="id">
</collection>
<!-- 告诉MyBatis,封装 orders 属性的时候,是一个集合,
但是这个集合需要调用另一个 方法 进行查询;select:来指定我们要调用的另一个方法
column:来指定我们要调用方法时,把哪一列的值作为传递的参数,交给这个方法
1)、column="id": 单传参:id传递给方法
2)、column="{cid=id,name=customer_name}":多传参(属性名=列名);
cid=id:cid是属性名,它是id列的值
name=customer_name:name是属性名,它是customer_name列的值
-->
<select id="getCustomerByIdAndOrdersStep" resultMap="CustomerOrdersStepRM">
select * from t_customer where id = #{id}
</select>
<!-- 按照客户id查询他的所有订单-->
<select id="getOrdersByCustomerId" resultType="com.joe.entity.Order">
select * from t_order where customer_id = #{id}
</select>
</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
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
# 延迟加载
分步查询 有时候并不需要立即运行,我们希望在用到的时候再去查询,可以开启延迟加载的功能 全局配置:
mybatis.configuration.lazy-loading-enabled=true
mybatis.configuration.aggressive-lazy-loading=false
1
2
2
Last Updated: 2025/12/02, 11:22:00