Mybatis:一对一查询映射处理



前言

本博主将用CSDN记录软件开发求学之路上亲身所得与所学的心得与知识,有兴趣的小伙伴可以关注博主!也许一个人独行,可以走的很快,但是一群人结伴而行,才能走的更远!

一、概述

MyBatis是一种流行的Java持久化框架,它提供了灵活而强大的查询映射功能。在一些复杂的数据模型中,一对一查询映射是一种常见的需求。本篇博客将详细介绍如何在MyBatis中处理一对一查询映射。

二、创建数据模型

假设我们有两张数据表,员工表和部门表,每个员工都只属于一个部门,我们需要创建对应的Java数据模型。

Emp.java

public class Emp {
    
    
    private Integer eid;
    private String empName;
    private Integer age;
    private String sex;
    private String email;
    private Dept dept;
    ...
    }

Dept.java

public class Dept {
    
    
    private Integer did;
    private String deptName;
    private List<Emp> emps;
    ...
    }

三、 问题

现在我们要查询员工信息以及员工所对应的部门信息,我们应该如何做呢?

四、解决方案

1、方案一:级联方式处理映射关系

EmpMapper

/**
  * @description:获取指定员工的信息(包括部门)
  * @author: Hey
  * @date: 2022/7/4 8:58
  * @param: [id]
  * @return: com.ir.mybatis.pojo.Emp
  **/
    Emp getAllEmpAndDept(@Param("eid") Integer eid);

EmpMapper.xml

<resultMap id="title1" 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="getAllEmpAndDept" resultMap="title1">
        select * from t_emp left join t_dept on t_emp.did = t_dept .did where t_emp.eid = #{eid}
    </select>

ResultTest

/**
     * @description:获取指定员工的信息(包括部门)
     * @author: Hey
     * @date: 2022/7/4 8:56
     * @param: []
     * @return: void
     **/
    @Test
    public void getAllEmpAndDept(){
    
    
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = mapper.getAllEmpAndDept(2);
        System.out.println(emp);//Emp{eid=2, empName='美羊羊', age=32, sex='女', email='[email protected]'}

    }

2、方案二:使用association处理映射关系

EmpMapper

/**
  * @description:获取指定员工的信息(包括部门)
  * @author: Hey
  * @date: 2022/7/4 8:58
  * @param: [id]
  * @return: com.ir.mybatis.pojo.Emp
  **/
    Emp getAllEmpAndDept(@Param("eid") Integer eid);

EmpMapper.xml

   <resultMap id="title1" 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:需要处理多对的映射关系的属性名
            javaType:该属性的类型
            过程:通过javaType,运用反射,确定其所有属性,再将column一一准确赋值
            给指定的属性,这样就得出了一个实体类对象,再将这个对象赋值给property
            中的对象名
        -->

        <association property="dept" javaType="Dept">
            <id property="did" column="did"></id>
            <result property="deptName" column="dept_name"></result>
        </association>
    </resultMap>

    <select id="getAllEmpAndDept" resultMap="title1">
        select * from t_emp left join t_dept on t_emp.did = t_dept .did where t_emp.eid = #{eid}
    </select>

ResultTest

/**
     * @description:获取指定员工的信息(包括部门)
     * @author: Hey
     * @date: 2022/7/4 8:56
     * @param: []
     * @return: void
     **/
    @Test
    public void getAllEmpAndDept(){
    
    
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = mapper.getAllEmpAndDept(3);
        System.out.println(emp);//Emp{eid=3, empName='懒洋洋', age=34, sex='男', email='[email protected]'}

    }

3、方案三:分步查询

mybatis-config.xml

 <!--设置MyBatis的全局配置-->
    <settings>
        <!--将_自动映射为驼峰,emp_name:empName-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
         <!--开启延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>

    </settings>

EmpMapper

/**
     * @description:通过分步查询查询员工以及员工所对应的部门信息
     *              分步查询第一步:查询员工信息
     * @author: Hey 
     * @date: 2022/7/4 9:41
     * @param: [eid]
     * @return: com.ir.mybatis.pojo.Emp
     **/
    Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);

EmpMapper.xml

<resultMap id="empAndDeptByStepResultMap" 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>
        <!--
            select:设置分步查询的sql的唯一标识(namespace.SQLId或mapper接口的全类名.方法名)
            column:设置分布查询的条件:根据员工的部门的did去查询该员工所属部门的信息
            fetchType:当开启了全局的延迟加载之后,可通过此属性手动控制延迟加载的效果
            fetchType="lazy|eager":lazy表示延迟加载,eager表示立即加载

        -->
        <association property="dept"
                     select="com.ir.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="did"
                     >
        </association>
</resultMap>
    <!--Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);-->
    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
        select * from t_emp where eid = #{eid}
    </select>

DeptMapper

/**
     * @description:通过分步查询查询部门以及部门中所有的员工信息
     *              分步查询第二步:根据did查询员工信息
     * @author: Hey 
     * @date: 2022/7/4 9:42
     * @param: [did]
     * @return: java.util.List<com.ir.mybatis.pojo.Emp>
     **/
    List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);

DeptMapper.xml

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

ResultTest

/**
     * @description:通过分步查询查询部门以及部门中所有的员工信息
     * @author: Hey 
     * @date: 2022/7/4 9:53
     * @param: []
     * @return: void
     **/
    @Test
    public void testGetEmpAndDeptByStep(){
    
    
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = mapper.getEmpAndDeptByStepOne(3);
        System.out.println(emp);//Emp{eid=3, empName='懒洋洋', age=34, sex='男', email='[email protected]'}

    }

猜你喜欢

转载自blog.csdn.net/weixin_52533007/article/details/131988707