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

 多对一映射处理

 

 1. 级联方式处理映射关系:

//(实体类添加) 多对一,创建一所对应的对象(dept表示当前员工所对应部门对象)
   private Dept dept;

 /**
    * 查询员工以及员工所对应的部门信息(某一个员工)
    */
   Emp getEmpAndDept(@Param("eid") Integer eid);

<!--    
   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(@Param("eid") Integer eid);-->
   <select id="getEmpAndDept" resultMap="empAndDeptResultMapOne">
     select * from t_emp left join t_dept on t_emp.did=t_dept.did where t_emp.eid=#{eid}
   </select>

 2. 使用association处理映射关系:

第二种方法必须得在核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,

可自动将_类型的字段名转换为驼峰

 /**
    * 查询员工以及员工所对应的部门信息(某一个员工)
    */
   Emp getEmpAndDept(@Param("eid") Integer eid);
 <!--    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(@Param("eid") Integer eid);

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(@Param("eid") Integer eid);-->
   <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
       select * from t_emp where eid=#{eid}
   </select>

DempMapper接口:

 /**
    * 通过分步查询工以及员工所对应的部门信息
    * 分步查询第二步:通过did查询员工所对应的部门信息
    */
   Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);

DempMapper的映射文件:(DempMapper.xml)

<!--    Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);-->
   <select id="getEmpAndDeptByStepTwo" resultType="Dept">
       select * from t_dept where did=#{did}
   </select>


测试方法:
  /**
    * 处理多对一的关系:
    * ①级联属性赋值
    * Emp{eid=1, empName='张三', age=23, sex='男', email='123@qq.com', dept=Dept{did=1, deptName='A'}}
    * ②association
    * ③分步查询
    */
   @Test
   public void testGetEmpAndDept(){
       SqlSession sqlSession = SqlSessionUtils.getSqlSession();
       EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
       Emp empAndDept = mapper.getEmpAndDept(1);
       System.out.println(empAndDept);
  }
   @Test
   public void testGetEmpAndDeptByStep(){
       SqlSession sqlSession = SqlSessionUtils.getSqlSession();
       EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
       Emp empAndDept = mapper.getEmpAndDeptByStepOne(1);
       System.out.println(empAndDept);
  }
 

  一对多映射处理

  第一种方法:collection

//Dept实体类
//多对一 对应的对象,对多对应的集合
private List<Emp> emps;

    /**
    * 获取部门以及部门中所有的员工信息
    */
   Dept getDeptAndEmp(@Param("did") Integer did);

<!--
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(@Param("did") Integer did);-->
   <select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">
       select * from t_dept left join t_emp on t_dept.did=t_emp.did where t_dept.did=#{did}
   </select>

  第二种方法:分步查询

//Dept实体类
//多对一 对应的对象,对多对应的集合
private List<Emp> emps;

 /**
    * 通过分步查询查询部门以及部门中所有的员工信息
    * 分步查询第一步:查询部门信息
    */
   Dept getDeptAndEmpByStepOne(@Param("did") Integer did);

 <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(@Param("did") Integer did);-->
   <select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpByStepResultMap">
       select * from t_dept where did=#{did}
   </select>
 /**
    * 通过分步查询查询部门以及部门中所有的员工信息
    * 分步查询第二步:根据did查询员工信息
    */
   List<Emp> getDeptAndEmpByStepTwd(@Param("did") Integer did);
<!--        List<Emp> getDeptAndEmpByStepTwd(@Param("did") Integer did);-->
   <select id="getDeptAndEmpByStepTwd" resultType="Emp">
       select * from t_emp where did=#{did}
   </select>

 

 测试方法:

/**
    * 处理一对多的映射关系:
    * ①collection
    * ②分步查询
    */

   @Test
   public void testGetDepAndEmp() {
       SqlSession sqlSession = SqlSessionUtils.getSqlSession();
       DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
       Dept deptAndEmp = mapper.getDeptAndEmp(1);
       System.out.println(deptAndEmp);
       /**
        * Dept{did=1, deptName='A', emps=[Emp{eid=1, empName='张三',
        * age=23, sex='男', email='123@qq.com', dept=null},
        * Emp{eid=4, empName='赵六', age=34, sex='女', email='12@qq.com', dept=null}]}
        */
  }

   @Test
   public void testGetDepAndEmpMyStep() {
       SqlSession sqlSession = SqlSessionUtils.getSqlSession();
       DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
       Dept deptAndEmp = mapper.getDeptAndEmpByStepOne(1);
       System.out.println(deptAndEmp.getDeptName());
       /**
        * Dept{did=1, deptName='A', emps=[Emp{eid=1, empName='张三', age=23,
        * sex='男', email='123@qq.com', dept=null}, Emp{eid=4, empName='赵六',
        * age=34, sex='女', email='12@qq.com', dept=null}]}
        */
  }
 
 
posted @ 2022-11-03 21:20  zjw_rp  阅读(149)  评论(0)    收藏  举报