自定义映射resultMap (多对一映射处理 (多对一查询))



1. 级联方式处理映射关系:
//(实体类添加) 多对一,创建一所对应的对象(dept表示当前员工所对应部门对象)
private Dept dept;
/**
* 查询员工以及员工所对应的部门信息(某一个员工)
*/
Emp getEmpAndDept(
<!--
resultType不能用:因为emp中有dept对象,
然后查询的字段最多只能查询员工所对应的did,和dept name,所以对应不了dept
-->
<!-- 处理的类型是emp:
因为因为sql语句查询出来的就是员工信息,
只不过员工里面一个属性跟当前所查询出来的字段无法映射,
所以要把dept和查询出来的字段创建映射关系(自定义映射关系)
-->
<!-- 级联属性赋值 处理多对一映射关系方式1-->
<resultMap id="empAndDeptResultMapOne" type="Emp">
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
<result property="dept.did" column="did"></result>
<result property="dept.deptName" column="dept_name"></result>
</resultMap>
<!-- Emp getEmpAndDept(
2. 使用association处理映射关系:
第二种方法必须得在核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,
可自动将_类型的字段名转换为驼峰
/**
* 查询员工以及员工所对应的部门信息(某一个员工)
*/
Emp getEmpAndDept(
<!-- association 处理多对一映射关系方式2-->
<resultMap id="empAndDeptResultMapTwo" type="Emp">
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
<!--
association: 处理多对一的映射关系
property:需要处理多对一的映射关系的属性名
javaType:该属性的类型
-->
<association property="dept" javaType="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
</association>
</resultMap>
<select id="getEmpAndDept" resultMap="empAndDeptResultMapTwo">
select * from t_emp left join t_dept on t_emp.did=t_dept.did where t_emp.eid=#{eid}
</select>
3. 分步查询:
EmpMapper接口:
/**
* 通过分步查询工以及员工所对应的部门信息
* 分步查询第一步:查询员工信息
*/
Emp getEmpAndDeptByStepOne(
EmpMapper的映射文件 : (EmpMapper.xml)
<resultMap id="empAndDeptByStepResultMap" type="Emp">
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
<!--
select:设置分步查询的sql的唯一标识(namespace.SQLId或mapper接口的全类名.方法名)
column:设置分步查询的条件
property;处理的实体类的属性 ,多对一的属性
-->
<association property="dept"
select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
column="did">
</association>
</resultMap>
<!-- Emp getEmpAndDeptByStepOne(
DempMapper接口:
/**
* 通过分步查询工以及员工所对应的部门信息
* 分步查询第二步:通过did查询员工所对应的部门信息
*/
Dept getEmpAndDeptByStepTwo(
<!-- Dept getEmpAndDeptByStepTwo(
/**
* 处理多对一的关系:
* ①级联属性赋值
* Emp{eid=1, empName='张三', age=23, sex='男', email='123@qq.com', dept=Dept{did=1, deptName='A'}}
* ②association
* ③分步查询
*/
//Dept实体类
//多对一 对应的对象,对多对应的集合
private List<Emp> emps;
/**
* 获取部门以及部门中所有的员工信息
*/
Dept getDeptAndEmp(
<!--
Dept里面有emps属性处理不了,所以用到resultMap
-->
<resultMap id="deptAndEmpResultMap" type="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
<!--
collection:处理一对多的映射关系(专门处理集合类型的)
ofType:表示该属性所对应的集合中存储数据的类型
-->
<collection property="emps" ofType="Emp">
<!-- 设置完成,下面设置字段和属性的关系-->
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
</collection>
</resultMap>
<!-- Dept getDeptAndEmp(
第二种方法:分步查询
//Dept实体类
//多对一 对应的对象,对多对应的集合
private List<Emp> emps;
/**
* 通过分步查询查询部门以及部门中所有的员工信息
* 分步查询第一步:查询部门信息
*/
Dept getDeptAndEmpByStepOne(
<resultMap id="deptAndEmpByStepResultMap" type="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
<!-- 查询完部门后就要根据部门id去查询它所对应的所有员工,
所以要以当前部门的did作为条件,然后去查询did所对应的员工信息
-->
<collection property="emps"
select="com.atguigu.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwd"
column="did"></collection>
</resultMap>
<!-- Dept getDeptAndEmpByStepOne(
/**
* 通过分步查询查询部门以及部门中所有的员工信息
* 分步查询第二步:根据did查询员工信息
*/
List<Emp> getDeptAndEmpByStepTwd(
<!-- List<Emp> getDeptAndEmpByStepTwd(
测试方法:
/**
* 处理一对多的映射关系:
* ①collection
* ②分步查询
*/
浙公网安备 33010602011771号