目录
若字段名和实体类中的属性名不一致,则可以通过resultMap属性及标签设置自定义映射
一、基本使用
resultMap:设置自定义映射
属性:
id:表示自定义映射的唯一标识
type:查询的数据要映射的实体类的类型
子标签:
id:设置主键的映射关系
result:设置普通字段的映射关系
association:设置多对一的映射关系
collection:设置一对多的映射关系
子标签属性:
property:映射关系中实体类中的属性名
column:映射关系中表中的字段名
将字段名与属性名进行映射:
List<Emp> getAllEmp();
<resultMap id="empResultMap" type="Emp">
<id property="eid" column="eid"/>
<result property="empName" column="emp_name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
</resultMap>
<select id="getAllEmp" resultMap="empResultMap">
select *from t_emp
</select>
二、多对一查询(如员工->部门)
2.1 使用association标签
association 标签的属性:
property-映射关系中实体类中的属性名
javaType-类似于type属性
子标签的形式与resultmap标签一样
<resultMap id="empDeptMap" 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>
<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>
2.2 基于association的分步查询
association 标签的属性:
property-映射关系中实体类中的属性名
select:设置分步查询,查询某个属性的值的sql的标识(namespace.sqlId)
column:将sql以及查询结果中的某个字段设置为分步查询的条件
Emp getEmpByStep(@Param("eid") int eid);
Dept getEmpDeptByStep(@Param("did") int did);
<!--Dept getEmpDeptByStep(@Param("did") int did);-->
<select id="getEmpDeptByStep" resultType="Dept">
select * from t_dept where did = #{did}
</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>
<association property="dept"
select="mappers.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>
查询分为两步:
首先根据员工id查出员工信息,
然后根据员工信息中的部门id再查询部门的信息。
三、一对多查询(如部门->员工)
3.1 使用collection标签
collection中的属性:
property-映射关系中实体类中的属性名
ofType:设置collection标签所处理的集合属性中存储数据的类型子标签的形式与resultmap标签一样
<resultMap id="deptEmpMap" type="Dept">
<id property="did" column="did"></id>
<result property="dname" column="dname"></result>
<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>
3.2 基于collection的分步查询
collection标签的属性:
property-映射关系中实体类中的属性名
select:设置分步查询,查询某个属性的值的sql的标识(namespace.sqlId)
column:将sql以及查询结果中的某个字段设置为分步查询的条件
Dept getDeptByStep(@Param("did") int did);
List<Emp> getEmpListByDid(@Param("did") int did);
<!--List<Emp> getEmpListByDid(@Param("did") int did);-->
<select id="getEmpListByDid" resultType="Emp">
select * from t_emp where did = #{did}
</select>
<resultMap id="deptEmpStep" type="Dept">
<id property="did" column="did"></id>
<result property="dname" column="dname"></result>
<collection property="emps"
select="mappers.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>
三、关于分步查询
分步查询的优点:可以实现延迟加载,但是必须在核心配置文件中设置全局配置信息: lazyLoadingEnabled:延迟加载的全局开关。
当开启时,所有关联对象都会延迟加载,此时可通过association和 collection中的fetchType属性设置当前的分步查询是否使用延迟加载,
fetchType="lazy(延迟加载) | eager(立即加载)"
举例来说,假如我们的分步查询要用到员工表和部门表,开启了延迟加载。接下来我只用到了结果中员工信息,则此时对应部门表的那段SQL语句就不执行了。