mybatis自定义映射ResultMap

1.处理字段和属性的映射关系

若字段名和实体类中的属性名不一致,则可以通过以下方法进行解决:

方法一:在sql语句中给字段名起别名,别名为属性名

例:

select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{empId}

方法二:若sql中的名字为_,java中的名字为驼峰式,可以在MyBatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,可以在查询表中数据时,自动将_类型的字段名转换为驼峰,自动将_映射为驼峰

例:emp_id---->empId

方法三:使用resultMap自定义映射处理

resultMap:设置自定义的映射关系

id:唯一标识

type:处理映射关系的实体类的类型

常用标签:

id:处理主键和实体类中属性的映射关系

result:处理普通字段和实体类中属性之间的关系

column:设置映射关系中的字段名,必须是sql中查询出的否个字段

property:设置映射关系中的属性的属性名,必须是实体类类型中的属性名

<resultMap id="empResultMap" type="Emp">
    <id column="emp_id" property="empId"></id>
    <result column="emp_name" property="empName"></result>
</resultMap>
<select id="getEmpByEmpId" resultMap="empResultMap">
    elect * from t_emp where emp_id = #{empId}
</select>

2.多对一映射处理

通过员工id获取其部门信息(使用resultMap处理多对一的映射关系)

方法一:级联

Emp getEmpAndDeptByEmpId(@Param("empId")Integer empId);
<resultMap id="empAndeDeptResultMap1" type="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"/>
        <result column="age" property="age"/>
        <result column="gender" property="gender"/>
        <result column="dept_id" property="dept.deptId"/>
        <result column="dept_name" property="dept.deptName"/>
    </resultMap>
    <select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap1">
        select * from t_emp
                          left join t_dept
                                    on t_emp.dept_id = t_dept.dept_id
        where t_emp.emp_id = #{empId}
    </select>
@Test
    public void testGetEmpByEmpId() throws IOException {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = mapper.getEmpByEmpId(2);
        System.out.println(emp);
    }

方法二:使用association标签

association:处理多对一的映射关系(处理实体类类型的属性)

property:设置需要处理映射关系的属性的属性名

Javatype:设置需要处理的属性的类型

 Emp getEmpAndDeptByEmpId(@Param("empId")Integer empId);
<resultMap id="empAndDeptResultMap2" type="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"/>
        <result column="age" property="age"/>
        <result column="gender" property="gender"/>
        
        <association property="dept" javaType="Dept">
            <id column="dept_id" property="deptId"/>
            <result column="dept_name" property="deptName"/>
        </association>
    </resultMap>
    <select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap2">
        select * from t_emp
            left join t_dept
                on t_emp.dept_id = t_dept.dept_id
                where t_emp.emp_id = #{empId}
    </select>
@Test
    public void testgetEmpAndDeptByEmpId() throws IOException {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = mapper.getEmpAndDeptByEmpId(2);
        System.out.println(emp);
    }

方法三:分步查询

1.通过分步查询员工以及对应的部门信息的第一步

Emp getEmpAndDeptByStepOne();
<!--Emp getEmpAndDeptByStepOne();-->
    <resultMap id="empAndDeptResultMap3ByStep1" type="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"/>
        <result column="age" property="age"/>
        <result column="gender" property="gender"/>
        <!--
        property:设置需要处理映射关系的属性的属性名
        select:设置分步查询的下一步的sql的唯一标识
        column:将查询出的某个字段作为下一步查询的sql条件
        fetchType:在开启了延迟加载的环境中,通过该属性来设置是否执行延迟加载 eager(立即加载) lazy(延迟加载)
        -->
        <association property="dept" fetchType="eager"
                     select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndeDeptByStepTwo"
                     column="dept_id"/>
    </resultMap>
    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptResultMap3ByStep1">
        select  * from t_emp where emp_id = #{empId}
    </select>

2.通过分步查询员工以及对应的部门信息的第二步

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

3.一对多的映射处理

通过deptId获取该部门所有员工的信息(使用resultMap处理一对多的映射关系)

方法一:collection标签

    Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);
 <!--方法一:collection标签-->
    <resultMap id="deptAndEmpResultMap" type="Dept">
        <id column="dept_id" property="deptId"/>
        <result column="dept_name" property="deptName"/>
        <!--
        collection:处理一对多的映射关系(处理实体类类型的属性  例:List<Emp>)
        ofType:设置集合中的属性
        -->
        <collection property="emps" ofType="Emp">
            <id column="emp_id" property="empId"/>
            <result column="emp_name" property="empName"/>
            <result column="age" property="age"/>
            <result column="gender" property="gender"/>
        </collection>
    </resultMap>
    <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>

方法二:分步查询

1.通过分步查询获得部门及部门中员工信息的第一步

Dept getDeptAndEmpByStepOne(@Param("deptId")Integer deptId);
<resultMap id="deptAndEmpResultMapByStep" type="Dept">
        <id column="dept_id" property="deptId"/>
        <result column="dept_name" property="deptName"/>
        <collection property="emps"
                    select="com.atguigu.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>

2.通过分步查询获得部门及部门中员工信息的第二步

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

分步查询的优点:可以实现延迟加载,但是必须在核心配置文件中设置全局配置信息: lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载 aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载,此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。此时可通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载, fetchType="lazy(延迟加载)|eager(立即加载)"


本文章使用limfx的vscode插件快速发布