• MyBatis 多对一映射和一对多映射的处理


    • 员工表t_emp
      在这里插入图片描述
    • 部门表t_dept
      在这里插入图片描述

    员工和部门之间是多对一的关系,而部门和员工是一对多的关系

    • Emp和Dept类的属性
    //Emp
        private Integer empId;
        private String empName;
        private Integer age;
        private String gender;
        private Dept dept;
        
    //Dept
        private Integer deptId;
        private String deptName;
        private List<Emp> emps;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    下面分别演示mybatis中这两种情况的映射的处理

    多对一映射关系

    • 根据员工号查询员工和对应部门的信息

    法1. 使用级联处理

    接口抽象方法Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);

    <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>
            <result column="dept_id" property="dept.deptId">result>
            <result column="dept_name" property="dept.deptName">result>
    resultMap>
    
    <select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap">
            SELECT * FROM t_emp
            LEFT JOIN t_dept
            ON t_emp.dept_id = t_dept.dept_id
            WHERE emp_id = #{empId};
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    法2. 使用association处理

    <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="dept" javaType="Dept">
                <id column="dept_id" property="deptId">id>
                <result column="dept_name" property="deptName">result>
            association>
    resultMap>
    
    <select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap">
            SELECT * FROM t_emp
            LEFT JOIN t_dept
            ON t_emp.dept_id = t_dept.dept_id
            WHERE emp_id = #{empId};
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    法3. 分步查询

    EmpMapper接口抽象方法:
    Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);

    <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>
        <association property="dept"
                     select="lrc.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="dept_id">association>
    resultMap>
    
    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
        SELECT * FROM t_emp WHERE emp_id = #{empId};
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    DeptMapper接口抽象方法:
    Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);

    
    <select id="getEmpAndDeptByStepTwo" resultType="Dept">
        SELECT * FROM t_dept WHERE dept_id = #{deptId};
    select>
    
    • 1
    • 2
    • 3
    • 4

    一对多映射关系

    • 根据部门id查询部门以及部门中的员工信息

    法1. 使用collection处理

    接口抽象方法:Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);

    <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>
    
    <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 = 2;
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    法2. 分步查询

    DeptMapper接口抽象方法:
    Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);

    <resultMap id="empAndDeptResultMap" type="Dept">
        <id column="dept_id" property="deptId">id>
        <result column="dept_name" property="deptName">result>
        <association property="emps"
                     select="lrc.mapper.EmpMapper.getDeptAndEmpByStepTwo"
                     column="dept_id">association>
    resultMap>
    
    <select id="getDeptAndEmpByStepOne" resultMap="empAndDeptResultMap">
        SELECT * FROM t_dept WHERE dept_id = #{deptId};
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    EmpMapper接口抽象方法:
    List getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);

    
    <select id="getDeptAndEmpByStepTwo" resultType="Emp">
        SELECT * FROM t_emp WHERE dept_id = #{deptId};
    select>
    
    • 1
    • 2
    • 3
    • 4

    分步查询

    分步查询的优点:可以实现延迟加载,但是必须在核心配置文件中设置全局配置信息:

    lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载
    aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载
    此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。

    可通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载,
    fetchType="lazy(延迟加载)|eager(立即加载)"

  • 相关阅读:
    PyTorch 1.13 正式发布:CUDA 升级、集成多个库、M1 芯片支持
    MacOS 安装MySQL 8(最详细,包含MacOS下MySQL的下载&安装&使用三部曲)
    Linux零拷贝原理
    Solon 1.7 重要发布,更现代感的应用开发框架
    leetcode 518. 零钱兑换 II、377. 组合总和 Ⅳ
    CW023A-H035 CW023A-R230铜合金硬度材质书
    leecode337. 打家劫舍 III
    lua协程
    KubeSphere核心实战_kubesphere部署redis02_创建redis现指定存储卷_配置外网访问服务---分布式云原生部署架构搭建048
    主成分分析
  • 原文地址:https://blog.csdn.net/weixin_47869348/article/details/127615218