MyBatis 结果封装与关联查询笔记

一、返回普通数据

用于封装基本类型(如 Long、Integer)或普通对象(如实体类),通过 resultType 配置。

示例

1
2
3
4
5
6
7
8
9
<!-- 封装实体类 -->
<select id="getEmp" resultType="com.atguigu.mybatis.entity.Employee">
select * from `t_emp` where id = #{id}
</select>

<!-- 封装基本类型 -->
<select id="countEmp" resultType="java.lang.Long">
select count(*) from `t_emp`
</select>

小提示

  • 若开启 mapUnderscoreToCamelCase = true(MyBatis 全局配置),可自动将数据库下划线命名(如 emp_name)映射为 Java 驼峰命名(如 empName)。

二、返回 List、Map

1. 返回 List

resultType 配置为集合的元素类型(MyBatis 会自动封装为 List)。

示例:

1
2
3
<select id="getEmpList" resultType="com.atguigu.mybatis.entity.Employee">
select * from `t_emp`
</select>

对应接口方法:List<Employee> getEmpList();

2. 返回 Map

  • 单条记录映射为 Map:resultType 配置为 map
  • 多条记录以某字段为键映射为 Map:使用 @MapKey 注解指定键字段。

示例:

1
2
3
4
5
6
7
8
9
<!-- 单条记录转 Map -->
<select id="getEmpMap" resultType="map">
select * from `t_emp` where id = #{id}
</select>

<!-- 多条记录转 Map(以 id 为键) -->
<select id="getEmpMapList" resultType="com.atguigu.mybatis.entity.Employee">
select * from `t_emp`
</select>

对应接口方法:

1
2
3
4
5
6
// 单条记录转 Map
Map<String, Object> getEmpMap(Long id);

// 多条记录转 Map(@MapKey 指定键为 id)
@MapKey("id")
Map<Long, Employee> getEmpMapList();

三、自定义结果集(resultMap

当数据库字段与实体属性命名不匹配(且未开启驼峰映射),或需处理一对一、一对多关联时,使用 resultMap 自定义映射规则。

基础映射示例(单表字段映射)

1
2
3
4
5
6
7
8
9
10
11
12
<resultMap id="EmpResultMap" type="com.atguigu.mybatis.entity.Employee">
<!-- id 标签:映射主键字段 -->
<id column="emp_id" property="empId"/>
<!-- result 标签:映射普通字段 -->
<result column="emp_name" property="empName"/>
<result column="emp_salary" property="empSalary"/>
</resultMap>

<!-- 使用自定义 resultMap -->
<select id="getEmp" resultMap="EmpResultMap">
select * from `t_emp` where id = #{id}
</select>

关联关系映射(association 与 collection

1. 一对一(association 标签)

用于映射对象类型的关联属性(如员工关联部门)。

  • javaType:指定关联对象的类型。
  • select:分步查询时,指定调用的 Mapper 方法。
  • column:分步查询时,传递的参数列。

示例场景:查询订单时关联查询客户信息

1
2
3
4
5
6
7
8
<resultMap id="OrderResultMap" type="com.atguigu.mybatis.entity.Order">
<id column="order_id" property="orderId"/>
<result column="order_no" property="orderNo"/>
<!-- 一对一关联客户 -->
<association property="customer" javaType="com.atguigu.mybatis.entity.Customer"
select="com.atguigu.mybatis.mapper.CustomerMapper.getCustomerById"
column="customer_id"/>
</resultMap>

2. 一对多(collection 标签)

用于映射集合类型的关联属性(如客户关联多个订单)。

  • ofType:指定集合中元素的类型。
  • select:分步查询时,指定调用的 Mapper 方法。
  • column:分步查询时,传递的参数列。

示例场景:查询客户时关联查询所有订单

1
2
3
4
5
6
7
8
<resultMap id="CustomerResultMap" type="com.atguigu.mybatis.entity.Customer">
<id column="customer_id" property="customerId"/>
<result column="customer_name" property="customerName"/>
<!-- 一对多关联订单 -->
<collection property="orders" ofType="com.atguigu.mybatis.entity.Order"
select="com.atguigu.mybatis.mapper.OrderMapper.getOrdersByCustomerId"
column="customer_id"/>
</resultMap>

四、分步查询与延迟加载

  • 分步查询:将关联查询拆分为多次单表查询,减少一次性数据量(如先查员工,再按需查部门)。
  • 延迟加载:按需加载关联数据(如查询员工时不主动加载部门,当调用部门属性时才加载)。

五、MyBatis 默认别名规则(不重要,最佳实现是写全类名)

MyBatis 对常见 Java 类型提供了默认别名,可简化 resultType 配置,但推荐写全类名以提升可读性

别名 Java 类型 别名 Java 类型 别名 Java 类型
_byte byte long Long object[] Object[]
_char(3.5.10+) char short Short map Map
_character(3.5.10+) char int Integer hashmap HashMap
_long long integer Integer list List
_short short double Double arraylist ArrayList
_int int float Float collection Collection
_integer int boolean Boolean iterator Iterator
_double double date Date
_float float decimal BigDecimal
_boolean boolean bigdecimal BigDecimal
string String biginteger BigInteger
byte Byte object Object
char(3.5.10+) Character date[] Date[]
character(3.5.10+) Character decimal[] BigDecimal[]
bigdecimal[] BigDecimal[]
biginteger[] BigInteger[]