• MyBatis ---- 自定义映射resultMap


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

    当实体类中的属性名和数据库中的属性名不一致时
    在这里插入图片描述

    在这里插入图片描述

        
        <select id="getAllEmp" resultType="emp">
            select * from t_emp;
        select>
    
    • 1
    • 2
    • 3
    • 4

    输出时并不会报异常,而是对于属性值不一样的值,输出结果为 null
    在这里插入图片描述
    解决方案

    1. 为字段起别名的方式,保证和实体类中的属性名保持一致
        
        <select id="getAllEmp" resultType="emp">
            
            select eid, emp_name empName, age, sex, email from t_emp;
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    1. 可以在 MyBatis 的核心配置文件中设置一个全局配置信息 mapUnderscoreToCamelCase
        
        <settings>
            
            <setting name="mapUnderscoreToCamelCase" value="true"/>
        settings>
    
    • 1
    • 2
    • 3
    • 4
    • 5
        
        <select id="getAllEmp" resultType="emp">
            
            
            select * from t_emp;
        select>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    1. 通过 resultMap 自定义映射
        <resultMap id="EmpResultMap" 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>
        resultMap>
        
        
        <select id="getAllEmp" resultMap="EmpResultMap">
            select * from t_emp;
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    resultMap:设置自定义映射
    属性:
    id:表示自定义映射的唯一标识
    type:查询的数据要映射的实体类的类型
    子标签:
    id:设置主键的映射关系
    result:设置普通字段的映射关系
    property:设置映射关系中实体类中的属性名
    column:设置映射关系中表中的字段名

    在这里插入图片描述

    2. 多对一映射处理

    a>级联方式处理映射关系

        <resultMap id="empDeptMap" 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>
    
        
        <select id="getEmpAndDeptByEid" resultMap="empDeptMap">
            select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid = #{eid};
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    b>使用association处理映射关系

        <resultMap id="empDeptMap" 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="dept" javaType="Dept">
                <id property="did" column="did">id>
                <result property="deptName" column="dept_name">result>
            association>
        resultMap>
    
        
        <select id="getEmpAndDeptByEid" resultMap="empDeptMap">
            select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid = #{eid};
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    c>分布查询

    1. 查询员工信息
        /**
         * 分布查询第一步
         * @param eid
         * @return
         */
        Emp getEmpByStepOne(@Param("eid") Integer eid);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        <resultMap id="empAndDeptByStep" 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="dept" select="com.fickler.mybatis.mapper.DeptMapper.getDeptByStepTwo" column="did">association>
        resultMap>
    
        
        <select id="getEmpByStepOne" resultMap="empAndDeptByStep">
            select * from t_emp where eid = #{eid};
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    1. 根据员工所对应的部门id查询部门信息
        /**
         * 分布查询第二步
         * @param did
         * @return
         */
        Dept getDeptByStepTwo(@Param("did") Integer did);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        
        <select id="getDeptByStepTwo" resultType="dept">
            select * from t_dept where did = #{did}
        select>
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

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

    lazyLoadingEnabled

        
        <settings>
            
            <setting name="mapUnderscoreToCamelCase" value="true"/>
            
            <setting name="lazyLoadingEnabled" value="true"/>
        settings>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
        @Test
        public void testGetEmpAndDeptByStep(){
    
            SqlSession sqlSession = SqlSessionUtils.getSqlSession();
            EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
            Emp empByStepOne = empMapper.getEmpByStepOne(5);
            System.out.println(empByStepOne.getEmpName());
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    只执行第一个 sql 语句
    在这里插入图片描述
    aggressiveLazyLoading

    开启之后,是立即加载

        
        <settings>
            
            <setting name="mapUnderscoreToCamelCase" value="true"/>
            
            <setting name="lazyLoadingEnabled" value="true"/>
            
            <setting name="aggressiveLazyLoading" value="true"/>
        settings>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述
    fetchType

    fetchType 的值为 eager,为立即加载,即使全局配置中设置为延迟加载,fetchType 可以按需设置为立即加载

        
        <settings>
            
            <setting name="mapUnderscoreToCamelCase" value="true"/>
            
            <setting name="lazyLoadingEnabled" value="true"/>
            
    
        settings>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
        <resultMap id="empAndDeptByStep" 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="dept" fetchType="eager" select="com.fickler.mybatis.mapper.DeptMapper.getDeptByStepTwo" column="did">association>
        resultMap>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    在这里插入图片描述

    此时为 延迟加载

        <resultMap id="empAndDeptByStep" 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="dept" fetchType="lazy" select="com.fickler.mybatis.mapper.DeptMapper.getDeptByStepTwo" column="did">association>
        resultMap>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    在这里插入图片描述

    3. 一对多映射处理

    a>collection

        /**
         * 查询部门中的所有员工信息
         * @param did
         * @return
         */
        Dept getDeptEmpByDid(@Param("did") Integer did);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        <resultMap id="deptEmpByDid" type="dept">
            <id property="did" column="did">id>
            <result property="deptName" column="dept_name">result>
            <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>
    
        
        <select id="getDeptEmpByDid" resultMap="deptEmpByDid">
            select * from t_dept left join t_emp on t_dept.did = t_emp.did where t_dept.did = #{did};
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    b>分步查询

    1. 查询部门信息
        /**
         * 分布查询第一步
         * @param did
         * @return
         */
        Dept getDeptByStep(@Param("did") Integer did);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        <resultMap id="deptEmpStep" type="dept">
            <id property="did" column="did">id>
            <result property="deptName" column="dept_name">result>
            <collection property="emps" select="com.fickler.mybatis.mapper.EmpMapper.getEmpByStep" column="did">collection>
        resultMap>
    
        
        <select id="getDeptByStep" resultMap="deptEmpStep">
            select * from t_dept where t_dept.did = #{did};
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. 根据部门 id 查询部门中的所有员工
        /**
         * 分组查询第二步
         * @param did
         * @return
         */
        Emp getEmpByStep(@Param("did") Integer did);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        
        <select id="getEmpByStep" resultType="emp">
            select * from t_emp where t_emp.did = #{did};
        select>
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

  • 相关阅读:
    示例 方法的重载 221107
    idea中jar包关联源码
    30天Python入门(第三天:深入了解Python中的运算符)
    Vue中的计算属性、方法和侦听属性的区别入门版
    《虚拟现实技术》教学上机实验报告
    【机器学习】符号主义类模型:解码智能的逻辑之钥
    Vue如何引入ElementUI并使用
    【算法 - 动态规划】找零钱问题Ⅰ
    MySQL(基础篇)——函数、约束
    print() 函数
  • 原文地址:https://blog.csdn.net/qq_52354698/article/details/127147563