mybatis处理多对一的映射关系

创建数据库t_emp和t_dept

 

 

 

 

 

 

创建对应实体类

package org.example.entity;

public class Emp {
    private Integer empId;
    private String empName;
    private Integer age;
    private String gender;
    private Dept dept;

    public Emp() {
    }

    public Emp(Integer empId, String empName, Integer age, String gender, Dept dept) {
        this.empId = empId;
        this.empName = empName;
        this.age = age;
        this.gender = gender;
        this.dept = dept;
    }

    public Integer getEmpId() {
        return empId;
    }

    public void setEmpId(Integer empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "empId=" + empId +
                ", empName='" + empName + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", dept=" + dept +
                '}';
    }
}

 

package org.example.entity;

public class Dept {
    private Integer deptId;
    private String deptName;

    public Dept() {
    }

    public Dept(Integer deptId, String deptName) {
        this.deptId = deptId;
        this.deptName = deptName;
    }

    public Integer getDeptId() {
        return deptId;
    }

    public void setDeptId(Integer deptId) {
        this.deptId = deptId;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "deptId=" + deptId +
                ", deptName='" + deptName + '\'' +
                '}';
    }
}

 

mapper接口

public Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);

 

处理方式一:级联方式处理

mapper.xml

 <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 t_emp.*,t_dept.dept_name FROM t_emp left join t_dept on  t_emp.dept_id = t_dept.dept_id where t_emp.emp_id = #{empId}
    </select>

 

处理方式二:使用association

mapper.xml

<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:设置需要处理映射关系的属性的属性名
            javaType:设置需要处理的属性的类型
        -->
        <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 t_emp.*,t_dept.dept_name FROM t_emp left join t_dept on  t_emp.dept_id = t_dept.dept_id where t_emp.emp_id = 1
    </select>

 

测试代码

 

 

 

处理方式三:分步查询

 创建EmpMapper

public interface EmpMapper {

   public Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);
}

 创建DeptMapper

public interface DeptMapper {

    Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);
}

 

EmpMapper.xml

<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>
        <!--
            property:设置需要处理映射关系的属性的属性名
            select:设置分布查询的sql的唯一标识
            column:将查询出的某个字段作为分布查询的sql的条件
        -->
        <association property="dept" column="dept_id" select="org.example.mapper.DeptMapper.getEmpAndDeptByStepTwo">
            <id column="dept_id" property="deptId"></id>
            <result column="dept_name" property="deptName"></result>
        </association>
    </resultMap>


    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
        select *
        from t_emp where emp_id = #{empId};
    </select>

DeptMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mapper.DeptMapper">

    <select id="getEmpAndDeptByStepTwo" resultType="dept">
        select  * from t_dept where dept_id = #{deptId}
    </select>
</mapper>

 

测试代码

  @Test
    public void testGetEmpAndDeptByStepOne(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = mapper.getEmpAndDeptByStepOne(2);
        System.out.println(emp);
        sqlSession.close();
    }

 

mybatis懒加载全局配置

    <settings>
        <!--标准的日志-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--开启懒加载(开启延迟加载)-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!--关闭实时加载-->
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

 

posted @ 2023-03-01 21:11  Mr_sven  阅读(25)  评论(0编辑  收藏  举报