自定义映射resultMap
resultMap处理字段和属性的映射关系
若字段名和实体类中的属性名不一致,则可以通过resultMap设置自定义映射
- 为查询的的字段名设置别名,和属性名保持一致
- 当字段符合mysql的要求使用_,而属性符合java的要求使用驼峰
此时可以在MyBatis的核心配置文件中设置一个全局配置,可以自动将下划线映射为驼峰
emp_id:empId… - 使用resultMap自定义映射处理
EmpMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bijing.mybatis.mapper.EmpMapper">
<!-- 字段名和属性名不一致的时候如何处理映射关系
1.为查询的的字段名设置别名,和属性名保持一致
2.当字段符合mysql的要求使用_,而属性符合java的要求使用驼峰
此时可以在MyBatis的核心配置文件中设置一个全局配置,可以自动将下划线映射为驼峰
emp_id:empId....
3.使用resultMap自定义映射处理
resultMap标签:设置自定义映射关系
id属性:唯一标识
type属性:处理映射关系的实体类的类型
常用标签:
id:处理主键和实体类中属性的映射关系
result:处理普通字段和实体类中属性的映射关系
column:设置映射关系中的字段名,必须是sql查询出的某个字段
property:设置映射关系中的属性的属性名,必须是处理实体类中的属性名
-->
<!-- Emp getEmpByEmpId(@Param("empId") Integer empId);-->
<resultMap id="empResultMap" type="Emp">
<id column="emp_id" property="empId"></id>
<result column="emp_name" property="empName"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
</resultMap>
<select id="getEmpByEmpId" resultMap="empResultMap">
<!-- select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{empId};-->
select * from t_emp where emp_id =#{empId};
</select>
<!-- <select id="getEmpByEmpId" resultType="Emp">-->
<!-- <!– select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{empId};–>-->
<!-- select * from t_emp where emp_id =#{empId};-->
<!-- </select>-->
</mapper>
多对一的映射处理
一个类中有个属性是对象类型
级联方式处理映射关系
<resultMap id="empAndDeptResultMapOne" type="Emp">
<id column="emp_id" property="empId"></id>
<result column="emp_name" property="empName"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
<result column="dept_id" property="dept.deptId"></result>
<result column="dept_name" property="dept.deptName"></result>
</resultMap>
<!-- Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);-->
<select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMapOne">
SELECT *
FROM t_emp
LEFT JOIN t_dept ON t_emp.dept_id = t_dept.dept_id
WHERE t_emp.emp_id = #{empId};
使用 association处理映射关系
<resultMap id="empAndDeptResultMapTwo" type="Emp">
<id column="emp_id" property="empId"></id>
<result column="emp_name" property="empName"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
<!-- association:处理多对一对映射关系(处理实体类类型的属性)-->
<!-- property:设置需要处理映射关系的属性的属性名-->
<!-- javaType:设置要处理的属性的类型-->
<!-- Emp中的dept属性是Dept类型,然后就是dept中的映射了-->
<association property="dept" javaType="Dept">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
</association>
</resultMap>
分步查询
注意:最好第二步另外创建一个DeptMapper接口和DeptMapper.xml
第一步:查询员工信息
EmpMapper.java
/**
* 通过分步查询员工以及所对应的部门的信息的第一步
*
* @param empId
* @return
*/
Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);
<resultMap id="empAndDeptByStepResultMap" type="Emp">
<id column="emp_id" property="empId"></id>
<result column="emp_name" property="empName"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
<!-- property:设置需要处理映射关系的属性的属性名-->
<!-- select:设置下一步分布查询的sql的唯一标识-->
<!-- column:将查询出来的某个字段作为分步查询的sql的条件-->
<association property="dept"
select="com.bijing.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
column="dept_id">
</association>
</resultMap>
<!-- Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);-->
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
SELECT*
FROM t_emp
WHERE emp_id = #{empId}
</select>
第二步:根据员工所对应的部门id查询部门信息
public interface DeptMapper {
/**
* 通过分步查询员工以及所对应的部门的信息的第二步
*
* @param deptId
* @return
*/
Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);
}
<mapper namespace="com.bijing.mybatis.mapper.DeptMapper">
<!-- Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);-->
<select id="getEmpAndDeptByStepTwo" resultType="Dept">
SELECT*
FROM t_dept
WHERE dept_id = #{deptId};
</select>
</mapper>
分步查询的优点
- 可以实现延迟加载
但是必须在核心配置文件中设置全局配置信息:
lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载
aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性,否则,每个属性按需加载
此时就可以实现按需加载,获取的数据是什么,就会只执行相应的sql。此时可以通过association和collection中的fetchType属性设置当前分步查询是否使用延迟加载。fetchType=“lazy(延迟加载)|eager(立即加载)”
mybatis-config.xml
<settings>
<!-- 将下划线映射为驼峰-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 开启延时加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 按需加载-->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
EmpMapper.xml
<resultMap id="empAndDeptByStepResultMap" type="Emp">
<id column="emp_id" property="empId"></id>
<result column="emp_name" property="empName"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
<!-- property:设置需要处理映射关系的属性的属性名-->
<!-- select:设置下一步分布查询的sql的唯一标识-->
<!-- column:将查询出来的某个字段作为分步查询的sql的条件-->
<!-- fetchType:在开起了延迟加载的环境中,通过该属性设置当前的分步查询是否使用延迟加载-->
<!-- fetchType="eager(立即加载)|lazy(延迟加载)"-->
<association property="dept" fetchType="eager"
select="com.bijing.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
column="dept_id">
</association>
</resultMap>
一对多映射处理
一个类中有个属性是集合
collection
<resultMap id="deptAndEmpResultMap" type="Dept">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
<!-- collection:处理一对多的映射关系(处理集合类型的属性)-->
<!-- ofType:设置集合类型的属性中存储的数据类型-->
<collection property="emps" ofType="Emp">
<id column="emp_id" property="empId"></id>
<result column="emp_name" property="empName"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
</collection>
</resultMap>
分步查询
第一步:根据部门id查询部门信息
DeoptMapper.java
/**
* 通过分步查询查询部门以及部门中的员工信息的第一步
*
* @param deptId
* @return
*/
Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);
<resultMap id="deptAndEmpResultMapByStep" type="Dept">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
<collection property="emps"
select="com.bijing.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
column="dept_id"></collection>
</resultMap>
<!-- Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);-->
<select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpResultMapByStep">
SELECT *
FROM t_dept
WHERE dept_id = #{deptId};
</select>
第二步:查询部门以及部门中的员工信息
EmpMapper.java
/**
* 通过分步查询查询部门以及部门中的员工信息的第二步
*
* @param deptId
* @return
*/
List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);
<!-- List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);-->
<!-- 如果在mybatis的核心配置文件中没有设置下划线和驼峰的映射,下面的reslutType不能用,要用resultMap -->
<select id="getDeptAndEmpByStepTwo" resultType="Emp">
SELECT *
FROM t_emp
WHERE dept_id = #{deptId};
</select>