代码演示

图:resultType、resultMap  的流程。

图:resultType、resultMap  的流程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	resultType    列名与pojo属性名一致  - 单个对象
resultMap 列名与pojo属性名有不一致
连接关联查询

查询单个对象数据 - 若列名与属性名不一致,不会映射
使用reusltType - 不一致的字段不会自动映射值

如何解决:
1: 给列名不一致列 - 使用 as 列标题 与属性名保持一致

2: resultMap(推荐)
可以通过resultMap将字段名和属性名作一个对应关系,resultMap实质上还需要将查询结果映射到pojo对象中。
resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询

3: 驼峰设置 - 若列名 aa_bb - 对象自动映射为 aaBb
全局中配置 <setting name="mapUnderscoreToCamelCase" value="true"/>
1
2
3
4
5
6
7
8
9
10
11
<resultMap type="customer" id="customerMap">
<id column="cust_id" property="custId"/>
<result column="cust_name" property="custName"/>
</resultMap>

//模糊查询
<select id="queryCustomersResultMap" parameterType="string" resultMap="customerMap">
select *
from cst_customer
where cust_name like '%${value}%'
</select>