MyBatis框架关联映射实例详解

 更新时间:2022年11月22日 09:48:20   作者:努力的阿波  
这篇文章主要介绍了MyBatis框架关联映射,关系映射主要处理复杂的SQl查询,如子查询,多表联查等复杂查询,应用此种需求时可以考虑使用,需要的朋友可以参考下

关系映射

1. 关联映射概述

在关系型数据库中,多表之间存在着三种关联关系,分别为一对一,一对多和多对多,如图

  • 一对一的关系:就是在本类中定义对方类型的对象,如A类中定义B类类型的属性b,B类中定义A类类型的属性a。
  • 一对多的关系:就是一个A类类型对应多个B类类型的情况,需要在A类中以集合的方式引入B类类型的对象,在B类中定义A类类型的属性a。
  • 多对多的关系:在A类中定义B类类型的集合,在B类中定义A类类型的集合。

2. 环境搭建

创建t_emp表

t_dept表

实体类Dept

package com.atguigu.mybatis.pojo;

import java.util.List;

public class Dept {

    private Integer deptId;

    private String deptName;

    private List<Emp> emps;

    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;
    }

    public List<Emp> getEmps() {
        return emps;
    }

    public void setEmps(List<Emp> emps) {
        this.emps = emps;
    }

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

实体类Emp

package com.atguigu.mybatis.pojo;

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) {
        this.empId = empId;
        this.empName = empName;
        this.age = age;
        this.gender = gender;
    }

    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;
    }

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

    public Dept getDept() {
        return dept;
    }

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

}

3.处理字段名和属性名不一致的情况

SQL语句

接口:

public interface EmpMapper {
    Emp getEmpById(@Param("empId") Integer empId);
}

测试方法:

   public void test(){
        SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();
        SqlSession sqlSession = sqlSessionUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp empById = mapper.getEmpById(1);
        System.out.println(empById.toString());
 
    }

执行测试方法后会得到如下结果:

可以看到,我们的SQl语句并没有问题,但为什么查询出的结果会有NUll出现呢,这就是因为我们的数据库中的字段名于Emp实体类的属性名不一致,因此出现了无法对应的情况。

解决办法:

1.可以通过为字段起别名的方式,保证和实体类中的属性名保持一致

select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{empId};

再次执行尝试:

2.可以在MyBatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,

此属性可以在查询表中数据时,自动将_类型的字段名,即下划线转换为驼峰

举个栗子:

例如:字段名user_id,设置了mapUnderscoreToCamelCase,此时字段名就会转换为userId

<resultMap id="empResultMap" type="Emp">
    <id property="empId" column="emp_id"></id>
    <result property="empName" column="emp_name"></result>
    <result property="age" column="age"></result>
    <result property="gender" column="gender"></result>
</resultMap>
 
    <select id="getEmpById" resultMap="empResultMap">
        select * from t_emp where emp_id = #{empId};
    </select>

4. 处理一对一映射

调节数据库字段与实体类的属性对应需要标签resultMap,如上文那个简单的查询例子就可以这样写:

<resultMap id="empResultMap" type="Emp">
    <id property="empId" column="emp_id"></id>
    <result property="empName" column="emp_name"></result>
    <result property="age" column="age"></result>
    <result property="gender" column="gender"></result>
</resultMap>
 
    <select id="getEmpById" resultMap="empResultMap">
        select * from t_emp where emp_id = #{empId};
    </select>

属性:

  • id:表示自定义映射的唯一标识
  • type:查询的数据要映射的实体类的类型

子标签:

  • id:设置主键的映射关系
  • result:设置普通字段的映射关系
  • association :设置多对一的映射关系
  • collection:设置一对多的映射关系

属性:

  • property:设置映射关系中实体类中的属性名
  • column:设置映射关系中表中的字段名

5. 处理多对一映射

5.1 级联方式处理

  <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>
 
            <!--部门中的字段dept_id与Emp实体类中的属性dept中的deptId相对应 -->
            <!--部门中的字段dept_name与Emp实体类中的属性dept中的deptName相对应 -->
            <result column="dept_id" property="dept.deptId"></result>
            <result column="dept_name" property="dept.deptName"></result>
 </resultMap>
    <select id="getEmpAndDeptById" resultMap="empAndDeptResultMap">
      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>

接口:

测试方法:

    public void test(){
        SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();
        SqlSession sqlSession = sqlSessionUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp empAndDeptById = mapper.getEmpAndDeptById(1);
        System.out.println(empAndDeptById);
    }

查询结果:

5.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>

5.3 分步查询

第一步:查询员工信息

    <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="com.atguigu.mybatis.mapper.DeptMapper.getDeptByStep" column="dept_id">
        </association>
 
    </resultMap>
 
    <select id="getEmpAndDeptByStep" resultMap="empAndDeptByStepResultMap">
     select * from t_emp where emp_id=#{empId};
    </select>

注意:

select:设置分步查询,查询某个属性的值的sql的标识(namespace.sqlid)

column:将sql以及查询结果中的某个字段设置为分步查询的条件

第二步:根据员工所对应的部门 id 查询部门信息

    <select id="getDeptByStep" resultType="com.atguigu.mybatis.pojo.Dept">
        select * from t_dept where dept_id=#{deptId};
    </select>

测试方法:

public void test(){
    SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();
    SqlSession sqlSession = sqlSessionUtils.getSqlSession();
    EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
    Emp empAndDeptByStep = mapper.getEmpAndDeptByStep(1);
    System.out.println(empAndDeptByStep);
 
}

执行结果:

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

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

6. 处理一对多查询

接口:

使用collection处理

  • collection :设置一对多的映射关系
  • ofType :设置 collection 标签所处理的集合属性中存储数据的类型
    <resultMap id="DeptAndEmpByDeptIdResultMap" 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="DeptAndEmpByDeptIdResultMap">
      SELECT t_emp.*,t_dept.* FROM t_dept
      LEFT JOIN t_emp
      ON t_emp.dept_id=t_dept.dept_id
      WHERE t_dept.dept_id=#{deptId}
    </select>

测试:

    public void test4(){
        SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();
        SqlSession sqlSession = sqlSessionUtils.getSqlSession();
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        Dept deptAndEmpByDeptId = mapper.getDeptAndEmpByDeptId(1);
        System.out.println(deptAndEmpByDeptId);
    }

执行结果:

7. 小结

关系映射主要处理复杂的SQl查询,如子查询,多表联查等复杂查询,应用此种需求时可以考虑使用。

到此这篇关于MyBatis框架关联映射的文章就介绍到这了,更多相关MyBatis关联映射内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java SpringCache+Redis缓存数据详解

    Java SpringCache+Redis缓存数据详解

    本篇文章主要介绍了浅谈SpringCache与redis缓存数据的解决方案,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-10-10
  • SpringBoot中Bean注入冲突的四种解决方案

    SpringBoot中Bean注入冲突的四种解决方案

    在Spring Boot应用开发中,依赖注入是最常用的功能之一,它极大地简化了对象之间的依赖关系管理,然而,当Spring容器中存在多个类型相同的Bean时,就会产生注入冲突问题,本文将介绍Spring Boot中的四种Bean注入冲突解决方案,需要的朋友可以参考下
    2025-06-06
  • Java增强for循环的增删操作代码

    Java增强for循环的增删操作代码

    Foreach循环(Foreach loop)是计算机编程语言中的一种控制流程语句,通常用来循环遍历数组或集合中的元素,本文通过实例演示普通for循环和foreach循环使用,java增强for循环的操作代码感兴趣的朋友一起看看吧
    2024-02-02
  • 多个java泛型示例分享

    多个java泛型示例分享

    这篇文章主要介绍了多个java泛型示例,需要的朋友可以参考下
    2014-04-04
  • Spring MVC学习笔记之Controller查找(基于Spring4.0.3)

    Spring MVC学习笔记之Controller查找(基于Spring4.0.3)

    这篇文章主要给大家介绍了关于Spring MVC学习笔记之Controller查找(基于Spring4.0.3)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-03-03
  • Java Spring的核心与设计思想你知道吗

    Java Spring的核心与设计思想你知道吗

    这篇文章主要为大家详细介绍了Java Spring的核心与设计思想,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • mybatis之增删改查

    mybatis之增删改查

    本篇文章主要介绍了Mybatis实现数据的增删改查实例(CRUD),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-07-07
  • Java直接插入排序算法实现

    Java直接插入排序算法实现

    这篇文章主要介绍了Java直接插入排序算法实现,有需要的朋友可以参考一下
    2014-01-01
  • Spring深入刨析声明式事务注解的源码

    Spring深入刨析声明式事务注解的源码

    在spring注解中,使用声明式事务,需要用到两个核心的注解:@Transactional注解和@EnableTransactionManagement注解。将@Transactional注解加在方法上,@EnableTransactionManagement注解加在配置类上
    2022-07-07
  • 示例解析java重载Overloading与覆盖Overriding

    示例解析java重载Overloading与覆盖Overriding

    这篇文章主要介绍了java重载Overloading与覆盖Overriding的示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05

最新评论