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;

下面分别演示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>

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

法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>
<!--Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);-->
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
    SELECT * FROM t_emp WHERE emp_id = #{empId};
</select>

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

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

一对多映射关系

  • 根据部门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>
<!--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 = 2;
</select>

法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>
<!--Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);-->
<select id="getDeptAndEmpByStepOne" resultMap="empAndDeptResultMap">
    SELECT * FROM t_dept WHERE dept_id = #{deptId};
</select>

EmpMapper接口抽象方法:
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(立即加载)"

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值