8、自定义映射resultMap

8、自定义映射resultMap

搭建MyBatis框架

新建modules ----->resultMap
t_emp表:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_emp
-- ----------------------------
DROP TABLE IF EXISTS `t_emp`;
CREATE TABLE `t_emp`  (
  `emp_id` int(0) NOT NULL AUTO_INCREMENT,
  `emp_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `age` int(0) NULL DEFAULT NULL,
  `gender` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `dept_id` int(0) NULL DEFAULT NULL,
  PRIMARY KEY (`emp_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of t_emp
-- ----------------------------
INSERT INTO `t_emp` VALUES (1, '张三', 18, '男', 1);
INSERT INTO `t_emp` VALUES (2, '李四', 19, '男', 2);
INSERT INTO `t_emp` VALUES (3, '王五', 20, '女', 1);
INSERT INTO `t_emp` VALUES (4, '赵六', 21, '男', 3);

SET FOREIGN_KEY_CHECKS = 1;

t_dept表:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_dept
-- ----------------------------
DROP TABLE IF EXISTS `t_dept`;
CREATE TABLE `t_dept`  (
  `dept_id` int(0) NOT NULL AUTO_INCREMENT,
  `dept_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  PRIMARY KEY (`dept_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of t_dept
-- ----------------------------
INSERT INTO `t_dept` VALUES (1, 'A');
INSERT INTO `t_dept` VALUES (2, 'B');
INSERT INTO `t_dept` VALUES (3, 'C');
INSERT INTO `t_dept` VALUES (4, 'D');

SET FOREIGN_KEY_CHECKS = 1;

8.1、resultMap处理字段和属性的映射关系

使用全局配置处理字段名和属性名不一致的问题
<settings>
        <!--将下划线映射为驼峰-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

在这里插入图片描述
在这里插入图片描述

    private Integer empId;

    private String empName;

    private Integer age;

    private  String gender;

在这里插入图片描述

 <!--
        字段名和属性名不一致的情况,如何处理映射关系
        1.为查询的字段设置别名,和属性名保持一致
        2.当查询的字段符合MYSQL的要求使用_,而属性符合java的要求使用驼峰
        此时可以在mybatis的核心配置文件中设置一个全局配置,可以自动将下划线映射为驼峰
        emp_id:empId   emp_name:empName
    -->
    <!-- Emp getEmpById(@Param("empId") int empId);-->
    <select id="getEmpById" resultType="Emp">
        <!--select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{empId}-->
        select * from t_emp where emp_id = #{empId}
    </select>

使用resultMap处理字段名和属性名不一致的问题
 <!--
        resultMap:设置自定义映射
        属性:
        id:表示自定义映射的唯一标识
        type:查询的数据要映射的实体类的类型
        子标签:
        id:设置主键的映射关系
        result:设置普通字段的映射关系
        association:设置多对一的映射关系(处理实体类类型的属性)
        collection:设置一对多的映射关系
        属性:
        property:设置映射关系中实体类中的属性名
        column:设置映射关系中表中的字段名


    -->
    <resultMap id="empResultMap" 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>
    </resultMap>
    <select id="getEmpById" resultMap="empResultMap">
        select * from t_emp where emp_id = #{empId}
    </select>

8.2、多对一映射处理

8.2.1、级联方式处理映射关系

Emp中添加dept

private Dept dept;
    <resultMap id="getemptAndDeptByEmpId" 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>
    <!--    Emp getempAndDeptByEmpId(@Param("empId") Integer empId);-->
    <select id="getempAndDeptByEmpId" resultMap="getemptAndDeptByEmpId">
        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>

    @Test
    public void  testGetEmpAndDeptByEmpId(){
        SqlSession sqlSession = sqlSessionUtil.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp empById = mapper.getempAndDeptByEmpId(1);
        System.out.println(empById);
    }

8.2.2、使用association处理映射关系
    <!--
        assocation:处理多对一多的映射关系(处理实体类类型的属性)
        propertye:设置需要处理映射关系的属性的属性名
        javaType:设置需要处理的属性的类型
    -->
    <resultMap id="getemptAndDeptByEmpId" 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="dept" javaType="dept">
            <id column="dept_id" property="deptId"></id>
            <result column="dept_name" property="deptName"></result>
        </association>
    </resultMap>

8.2.3、分步查询

EmpMapper接口

    //分步查询(1)①查询员工信息
    Emp getempAndDeptByStepOne (@Param("empId") Integer empId);

DeptMapper接口

    //分步查询(2)②根据员工所对应的部门id查询部门信息
    Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);

EmpMapper.xml

<!--
        proerty:设置㤇处理映射关系的属性的属性名
        Select:设置需要分步查询的SQL的唯一标识
        column:将查询出的某个字段作为分步查询的SQL的条件
    -->
    <resultMap id="getemptAndDeptStepOne" 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="dept"
                     select="com.gao.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="dept_id">

        </association>
    </resultMap>
    <!--Emp getempAndDeptByStepOne (@Param("empId") Integer empId);-->
    <select id="getempAndDeptByStepOne" resultMap="getemptAndDeptStepOne">
        select * from t_emp where emp_id = #{empId}
    </select>

DeptMapper.xml

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

测试

    @Test
    public void  testGetEmpAndDeptByStep(){
        SqlSession sqlSession = sqlSessionUtil.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = mapper.getempAndDeptByStepOne(2);
        System.out.println(emp);
    }
延迟加载

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

    <settings>
        <!--将下划线映射为驼峰-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--开启延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!--按需加载-->
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

测试:

    @Test
    public void  testGetEmpAndDeptByStep(){
        SqlSession sqlSession = sqlSessionUtil.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = mapper.getempAndDeptByStepOne(2);
        System.out.println(emp.getEmpName());
    }
 <resultMap id="getemptAndDeptStepOne" 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="dept" fetchType="lazy"
                     select="com.gao.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="dept_id">

        </association>
    </resultMap>
fetchType:在开启了延迟加载的环境中,通过该属性设置当前的分步查询是否使用延迟加载
        fetchType="lazy(延迟) | eager(立即加载)"

8.3、一对多映射处理

8.3.1、collection

Dept类增加属性:

private List<Emp> emps;
 //查询部门以及部门中员工的信息
    Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);

<!--
        处理一对多的映射关系:
        1.collection:处理一对多的映射关系(处理集合类型的属性)
        2.分步查询

        ofType:设置集合类型的属性中存储的数据的类型
    -->

    <resultMap id="deptAndEmpResultMap" type="dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
        <collection property="emps" ofType="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>
        </collection>
    </resultMap>
    <!--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>

测试

    @Test
    public void  testGetDeptAndEmpByDeptId(){
        SqlSession sqlSession = sqlSessionUtil.getSqlSession();
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        Dept deptAndEmpByDeptId = mapper.getDeptAndEmpByDeptId(1);
        System.out.println(deptAndEmpByDeptId);
    }

8.3.2、分步查询

Dept

private List<Emp> emps;

DeptMapper

    //分步查询(1)查询部门以及部门中的员工
    Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);
    <resultMap id="deptAndEmpResultMapStep" type="dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
        <collection property="emps"
                    fetchType="eager"
                    select="com.gao.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
                    column="dept_id">
        </collection>
    </resultMap>
    <!--Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);-->
    <select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpResultMapStep">
        select * from t_dept where dept_id = #{deptId}
    </select>

EmpManner

 //分步查询(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>

测试

    @Test
    public void  testGetDeptAndEmpByStep(){
        SqlSession sqlSession = sqlSessionUtil.getSqlSession();
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        Dept deptAndEmpByStepOne = mapper.getDeptAndEmpByStepOne(1);
        System.out.println(deptAndEmpByStepOne);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值