day 113- mybatis的查询resultMap

发布时间 2023-06-28 21:22:57作者: 北海之上

mybatis中的resultMap

resultMap用来处理字段名和属性名不一致的情况,处理映射关系

若字段名和实体类中的属性名不一致,则可以通过resultMap设置自定义映射

<!--
    字段名和属性名不一致的情况,处理映射关系:
    1. 为查询的字段设置别名,和属性名保持一致
    2. 当字段符合MySQL要求使用下划线_,而属性符合Java要求使用驼峰命名。
        此时可以在mybatis的核心配置文件中设置一个全局配置,可以自动将下划线命名为驼峰
    3.  使用resultMap自定义映射处理
-->
<!--
    resultMap:来设置自定义的映射关系
    id:唯一标识
    type:处理映射关系的实体类的类型
    常用标签:
    id:处理主键和实体类中的映射关系
    result:处理普通字段与实体类中属性的映射关系
    association:处理多对一的映射关系,处理实体类类型属性
    column:映射关系中的字段名,必须是sql查询出的某个字段
    property:设置映射关系中的属性名,必须是处理实体类类型中的属性名
    collection:处理一对多的映射关系,处理集合类型的属性
-->

 

多对一的映射处理

场景: 查询员工信息以及员工对应的部门信息

使用级联方式处理映射关系‘

/**
 * 获取员工以及所对应的部门信息
 * @param empId
 * @return
 */
Emp getEmpAndDept(@Param("empId") Integer empId);

 

<!--  Emp getEmpAndDept(@Param("empId") Integer empId);  -->
    <select id="getEmpAndDept" resultMap="empAndDeptResultMap">
        select
        t_emp.*,t_dept.*
        from t_emp
        left join t_dept
        on t_emp.dept_id = t_dept.dept_id
        where t_emp.emp_id = #{empId}
    </select>

 

result Map:

<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>

 

自定义的方式定义映射文件

使用association处理映射关系

<!--  Emp getEmpAndDept(@Param("empId") Integer empId);  -->
    <select id="getEmpAndDept" resultMap="empAndDeptResultMap">
        select
        t_emp.*,t_dept.*
        from t_emp
        left join t_dept
        on t_emp.dept_id = t_dept.dept_id
        where t_emp.emp_id = #{empId}
    </select>
<resultMap id="empAndDeptResultMap" 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:设置要处理的属性的类型
        fetchType:在开启了延迟加载的环境中,通过该属性设置当前的分步查询是否开启延迟加载
                    eager/lazy 立即加载/延迟加载
    -->
    <association property="dept"
                 javaType="com.gu.mybatis.pojo.Dept"
                fetchType="lazy">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
    </association>
</resultMap>

 

分步查询

查询员工信息
/**
 * 获取员工以及所对应的部门信息,通过分步查询(第一步)
 * @param empId
 * @return
 */
Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);

 

<!--  Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);  -->
    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
        select * from t_emp where emp_id = #{empId}
    </select>
<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的条件,将查询出的某个字段作为下一个sql查询需要的条件
        -->
        <association property="dept"
                     select="com.gu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="dept_id">
        </association>
    </resultMap>

 

根据员工对应的部门id查询部门信息
/**
 * 获取员工以及所对应的部门信息,通过分步查询(第二步)
 * @return
 */
Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);

 

<!--  Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);  -->
<select id="getEmpAndDeptByStepTwo" resultType="com.gu.mybatis.pojo.Dept">
    select * from t_dept where dept_id = #{deptId}
    </select>

 

一对多的映射处理

collection

/**
 * 查询部门及部门中员工信息collection方法
 * @return
 */
Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);

 

<select id="getDeptAndEmpByDeptId" resultMap="deptAndEmpResultMap">
    select *
    from t_dept
    left join t_emp
    on t_dept.dept_id = t_emp.dept_id
    where t_dept.dept_id = #{deptId}
    </select>
<resultMap id="deptAndEmpResultMap" type="Dept">
    <id column="dept_id" property="deptId"></id>
    <result column="dept_name" property="deptName"></result>
    <!--
        ofType:设置集合类型的属性中存储的数据的类型
    -->
    <collection property="emps" ofType="com.gu.mybatis.pojo.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>
    </collecti

 

on>
</resultMap>

分步查询

查询部门信息
/**
 * 查询部门及部门中员工信息 分步查询第一步
 * @param deptId
 * @return
 */
Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);

 

<!--  Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);  -->
<select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpResultMapByStep">
    select * from t_dept where dept_id = #{deptId}
    </select>
<resultMap id="deptAndEmpResultMapByStep" type="Dept">
    <id column="dept_id" property="deptId"></id>
    <result column="dept_name" property="deptName"></result>
    <collection property="emps"
                ofType="com.gu.mybatis.pojo.Emp"
                select="com.gu.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
                column="dept_id"></collection>
</resultMap>

 

根据部门id查询部门中的所有员工
/**
 * 查询部门及部门中员工信息 分步查询第2步
 * @param deptId
 * @return
 */
List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);

 

<!--  List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);  -->
    <select id="getDeptAndEmpByStepTwo" resultType="com.gu.mybatis.pojo.Emp">
        select * from  t_emp where dept_id = #{deptId}
    </select>

 

总结

处理多对一的映射关系:
1. 级联方式
2. association
3. 分步查询
  分步查询的优点:可以实现延迟加载
      延迟加载:需要在核心配置文件中配置全局信息:
      lazyLoadingEnabled:设置开启延迟加载
      aggressiveLazyLoading:设置按需加载
                     
处理一对多关系的映射:
  1. collection
      resultMap中collection标签处理一对多的映射关系,处理集合类型的属性
  2. 分步查询

over