文章目录
MyBatis–>自定义映射resultMap
文章目录
前言
MyBatis–resultMap学习笔记
一、resultMap处理字段和属性的映射关系
查询出来的字段名与实体类中的属性名(get方法之后的为属性名)不一致,在JDBC中通过在select语句中起别名来解决,这里使用resultMap来自定义他们的映射关系
<!--
resultMap:设置自定义映射 属性:
id:表示自定义映射的唯一标识
type:查询的数据要映射的实体类的类型 子标签: id:设置主键的映射关系
result:设置普通字段的映射关系
association:设置多对一的映射关系 collection:设置一对多的映射关系 属性: property:设置映射关系中实体类中的属性名 column:设置映射关系中表中的字段名
-->
<resultMap id="userMap" type="User">
<id property="id" column="id"/>
<result property="userName" column="user_name"/>
<!--
列名和属性名已经一致的可以不用管
<result property="password" column="password"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
-->
</resultMap>
当然,可以在mybatis-config.xml中设置全局配置(setting标签)信息mapUnderscoreToCamelCase,可以在查询表中数据时,自动将_类型的字段名转换为驼峰,如user_name->userName
二、对象映射处理
背景:Emp类中包含Dept实例对象作为属性
1. 级联方式
<resultMap id="empDeptMap" type="Emp">
<id column="eid" property="eid"></id>
<result column="did"
<!--级联处理-->
<property="dept.did"></result>
<result column="dname" property="dept.dname"></result>
</resultMap>
<!--Emp getEmpAndDeptByEid(@Param("eid") int eid);-->
<select id="getEmpAndDeptByEid" resultMap="empDeptMap">
select emp.*,dept.* from t_emp emp left join t_dept dept on emp.did = dept.did where emp.eid = #{eid} </select>
2. association处理映射
利用association处理映射关系 property属性表示需要处理的属性,javaType表示其对应的类(全类名,这里是配置了typealiases包别名)
在association标签中在将列映射为属性
<resultMap id="empDeptMap" type="Emp">
<id column="eid" property="eid"></id>
<association property="dept" javaType="Dept">
<id column="did" property="did"></id>
<result column="dname" property="dname"></result>
</association>
</resultMap>
<!--Emp getEmpAndDeptByEid(@Param("eid") int eid);-->
<select id="getEmpAndDeptByEid" resultMap="empDeptMap">
select emp.*,dept.* from t_emp emp left join t_dept dept on emp.did = dept.did where emp.eid = #{eid}
</select>
3.分布查询
将查询拆分为多步,先查询员工Emp信息,再查询Dept信息
利用association + select + column标签
先查询员工信息表,查询到did,再利用did去查询部门信息来对dept实例对象(Emp属性)进行注入,select标签中是部门信息查询方法的全类名。
<resultMap id="empDeptStepMap" type="Emp">
<id column="eid" property="eid"></id>
<result column="ename" property="ename"></result>
<result column="age" property="age"></result>
<result column="sex" property="sex"></result>
<!--select:设置分步查询,查询某个属性的值的sql的标识(namespace.sqlId) column:将sql以及查询结果中的某个字段设置为分步查询的条件 -->
<association property="dept" select="org.example.MyBatis.mapper.DeptMapper.getEmpDeptByStep" column="did"> </association> </resultMap>
<!--Emp getEmpByStep(@Param("eid") int eid);-->
<select id="getEmpByStep" resultMap="empDeptStepMap"> select * from t_emp where eid = #{eid} </select>
三、集合映射处理
背景:例如部门类中有员工列表作为部门员工属性
1. collection标签
collection+ofType
<resultMap id="deptEmpMap" type="Dept">
<id property="did" column="did"></id>
<result property="dname" column="dname"></result>
<!--ofType:设置collection标签所处理的集合属性中存储数据的类型 -->
<collection property="emps" ofType="Emp">
<id property="eid" column="eid"></id>
<result property="ename" column="ename"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
</collection>
</resultMap>
<!--Dept getDeptEmpByDid(@Param("did") int did);-->
<select id="getDeptEmpByDid" resultMap="deptEmpMap"> select dept.*,emp.* from t_dept dept left join t_emp emp on dept.did = emp.did where dept.did = #{did} </select>
2.分布查询
分布查询,先查询部门基本信息,再通过部门id去查询员工信息
- Step1 查询部门基本信息
<resultMap id="deptEmpStep" type="Dept">
<id property="did" column="did"></id>
<result property="dname" column="dname"></result>
<!--分布查询fetchType表示是否延迟加载,即两步是分开还是一次进行.
第二步查询如下
List<Emp> getEmpListByDid(@Param("did") int did);-->
<collection property="emps" fetchType="eager" select="org.example.MyBatis.mapper.EmpMapper.getEmpListByDid" column="did"> </collection>
</resultMap>
<!--Dept getDeptByStep(@Param("did") int did);-->
<select id="getDeptByStep" resultMap="deptEmpStep"> select * from t_dept where did = #{did}
</select>