八、自定义映射resultMap
1、resultMap处理字段和属性的映射关系
注:若字段名和实体类中的属性名不一致,则可以通过resultMap设置自定义映射
<!--
resultMap:设置自定义映射
属性:
id:表示自定义映射的唯一标识
type:查询的数据要映射的实体类的类型
子标签:
id:设置主键的映射关系
result:设置普通字段的映射关系
association:设置多对一的映射关系
collection:设置一对多的映射关系
属性:
property:设置映射关系中实体类中的属性名
column:设置映射关系中表中的字段名
-->
<resultMap id="userMap" type="User">
<id property="id" column="id"></id>
<result property="userName" column="user_name"></result>
<result property="password" column="password"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
</resultMap>
<!--List<User> testMohu(@Param("mohu") String mohu);-->
<select id="testMohu" resultMap="userMap">
<!--select * from t_user where username like '%${mohu}%'-->
select id,user_name,password,age,sex from t_user where user_name like
concat('%',#{mohu},'%')
</select>
若字段名和实体类中的属性名不一致,但是字段名符合数据库的规则(使用
_
),实体类中的属性
名符合
Java
的规则(使用驼峰)
此时也可通过以下两种方式处理字段名和实体类中的属性的映射关系
a>
可以通过为字段起别名的方式,保证和实体类中的属性名保持一致
b>
可以在
MyBatis
的核心配置文件中设置一个全局配置信息
mapUnderscoreToCamelCase
,可
以在查询表中数据时,自动将
_
类型的字段名转换为驼峰
例如:字段名
user_name
,设置了
mapUnderscoreToCamelCase
,此时字段名就会转换为
userName