• MyBatis的关联映射


    目录

    ​编辑

    文章目录

    前言

    一、表与表之间的关系

    一对一:

    一对多:

    多对一:

    多对多:

    二、多对一

    1.创建接口MoreMapper

    在MoreMapper里面写入方法,通过员工ID来查询员工的信息以及部门的信息,员工表代表多的一方。

    2.创建 MoreMapper.xml文件来写Sql语句

    在文件中,namespace要写接口的唯一标识,即在包下的位置,然后将方法名复制过来,对应查询的id即可。

    3、多对一查询实现的三种方法 

    方法一、用表名.属性名

    方法一测试类MoreMapperTest

    方法二、用association标签

    方法三、用分步查询

    三、一对多的使用

    1.创建接口LessMapper

    2、多对一查询实现的两种方法 

    方法一、用collection标签

    方法二、用分步查询

    四、总结


    文章目录

    • 一、表与表之间的关系
    • 二、多对一的使用
    • 三、一对多的使用
    • 四、总结

    前言

            在实际开发中,根据业务需要,数据表之间往往会存在某种关联关系,例如:一对一,一对多,多对一,多对多等,当程序操作数据库时,如果被操作的表与其他表相关联,那么处理这些表中的数据时必须要考虑它们之间的关联关系。

            为此,MyBatis提供了映射表之间关联关系的功能,如此一来,使用MyBatis能够以简洁的方式操作多张表。


    一、表与表之间的关系

    一对一:

            在一对一关系中,一方数据表中的一条记录最多可以和另一方数据表中的一条记录相关。例如:人的角度:一个人只能有一张身份证,身份证角度:一张身份证只能属于一个人。

    一对多:

            在一对多关系中,主键数据表中的一条记录可以和另外一张数据表的多条记录相关。例如:班级角度:一个班级可以有多名学生。学生角度:一个学生只能属于一个班级。

    多对一:

            在多对一关系中,外键数据表的每一个数据可以和另外一张表的一条数据相对应。例如:

    球员角度:多名球员属于一个球队。球队角度:一个球队里面的球员是唯一的(与一对多相反)

    多对多:

            在多对多关系中,两个数据表里的每一条记录都可以和另外一张表里的每一条记录相关。例如:学生角度:一名学生由多名老师授课,老师角度:一名老师为多个学生授课。

    二、多对一

    现有emp员工表和dept部门表如下:

    1.创建接口MoreMapper

    在MoreMapper里面写入方法,通过员工ID来查询员工的信息以及部门的信息,员工表代表多的一方。

    1. package com.mybatis.mapper;
    2. import com.mybatis.pojo.Emp;
    3. import org.apache.ibatis.annotations.Param;
    4. import java.util.List;
    5. public interface MoreMapper {
    6. //通过员工Id来查询员工信息和部门信息
    7. List selectEmpAndDeptByEmpId(@Param("empId") Integer empId);
    8. }

    2.创建 MoreMapper.xml文件来写Sql语句

    在文件中,namespace要写接口的唯一标识,即在包下的位置,然后将方法名复制过来,对应查询的id即可。

    1. "1.0" encoding="UTF-8" ?>
    2. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. "com.mybatis.mapper.MoreMapper">

    3、多对一查询实现的三种方法 

    方法一、用表名.属性名

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.mybatis.mapper.MoreMapper">
    6. <resultMap id="selectOne" type="Emp">
    7. <id column="emp_id" property="empId">id>
    8. <result column="emp_name" property="empName">result>
    9. <result column="age" property="age">result>
    10. <result column="gender" property="gender">result>
    11. <result column="dept_id" property="dept.deptId">result>
    12. <result column="dept_name" property="dept.deptName">result>
    13. resultMap>
    14. <select id="selectEmpAndDeptByEmpId" resultMap="selectOne">
    15. select *from emp join dept on emp.emp_id = dept.dept_id
    16. where emp_id = #{empId}
    17. select>
    18. mapper>

    运用resultMap来查询多个表,id标签代表查询的主键 ,

    id里面的column写数据库的字段名,property写实体类对象的属性名。

    方法一使用数据库里面的字段外键,属性名用点表示实体类里面的名字

    方法一测试类MoreMapperTest

    1. import com.mybatis.SqlSessionUtil;
    2. import com.mybatis.mapper.MoreMapper;
    3. import com.mybatis.pojo.Emp;
    4. import org.apache.ibatis.session.SqlSession;
    5. import org.junit.Test;
    6. import java.util.List;
    7. public class MoreMapperText {
    8. @Test
    9. public void textSelectEmpAndDeptByEmpId(){
    10. SqlSession sqlSession = SqlSessionUtil.getSqlSession();
    11. MoreMapper mapper = sqlSession.getMapper(MoreMapper.class);
    12. List emps = mapper.selectEmpAndDeptByEmpId(1);
    13. System.out.println(emps);
    14. }
    15. }

    测试运行:

    方法一测试成功!!!

    方法二、用association标签

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.mybatis.mapper.MoreMapper">
    6. <resultMap id="selectOne" type="Emp">
    7. <id column="emp_id" property="empId">id>
    8. <result column="emp_name" property="empName">result>
    9. <result column="age" property="age">result>
    10. <result column="gender" property="gender">result>
    11. <result column="dept_id" property="dept.deptId">result>
    12. <result column="dept_name" property="dept.deptName">result>
    13. resultMap>
    14. <resultMap id="selectTwo" type="Emp">
    15. <id column="emp_id" property="empId">id>
    16. <result column="emp_name" property="empName">result>
    17. <result column="age" property="age">result>
    18. <result column="gender" property="gender">result>
    19. <association property="dept" javaType="Dept">
    20. <id column="dept_id" property="deptId">id>
    21. <result column="dept_name" property="deptName">result>
    22. association>
    23. resultMap>
    24. <select id="selectEmpAndDeptByEmpId" resultMap="selectTwo">
    25. select *from emp join dept on emp.emp_id = dept.dept_id
    26. where emp_id = #{empId}
    27. select>
    28. mapper>

    测试运行:

    方法二测试成功!!!

    方法三、用分步查询

    分步查询第一步:

    只查询Emp表里面的全部内容

    在MoreMapper里面写入分步查询第一步方法

    1. package com.mybatis.mapper;
    2. import com.mybatis.pojo.Emp;
    3. import org.apache.ibatis.annotations.Param;
    4. import java.util.List;
    5. public interface MoreMapper {
    6. //通过员工Id来查询员工信息和部门信息
    7. List selectEmpAndDeptByEmpId(@Param("empId") Integer empId);
    8. //分步查询第一步
    9. List selectEmpAndDeptByStepOne(@Param("empId") Integer empId);
    10. }

    在MoreMapper.xml里面写入第一步查询语句

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.mybatis.mapper.MoreMapper">
    6. <resultMap id="selectOne" type="Emp">
    7. <id column="emp_id" property="empId">id>
    8. <result column="emp_name" property="empName">result>
    9. <result column="age" property="age">result>
    10. <result column="gender" property="gender">result>
    11. <result column="dept_id" property="dept.deptId">result>
    12. <result column="dept_name" property="dept.deptName">result>
    13. resultMap>
    14. <resultMap id="selectTwo" type="Emp">
    15. <id column="emp_id" property="empId">id>
    16. <result column="emp_name" property="empName">result>
    17. <result column="age" property="age">result>
    18. <result column="gender" property="gender">result>
    19. <association property="dept" javaType="Dept">
    20. <id column="dept_id" property="deptId">id>
    21. <result column="dept_name" property="deptName">result>
    22. association>
    23. resultMap>
    24. <select id="selectEmpAndDeptByEmpId" resultMap="selectTwo">
    25. select *from emp join dept on emp.emp_id = dept.dept_id
    26. where emp_id = #{empId}
    27. select>
    28. <resultMap id="MoreMapperOne" type="Emp">
    29. <id column="emp_id" property="empId">id>
    30. <result column="emp_name" property="empName">result>
    31. <result column="age" property="age">result>
    32. <result column="gender" property="gender">result>
    33. <association property="dept"
    34. select=""
    35. column="">association>
    36. resultMap>
    37. <select id="selectEmpAndDeptByStepOne" resultMap="">
    38. select *from emp where emp_id = #{empId}
    39. select>
    40. mapper>

    resultMap里面的关键字select表示第二步查询方法的唯一标识,column为查询的条件

    开始准备分步查询第二步,创建LessMapper接口写入方法名

    1. package com.mybatis.mapper;
    2. import com.mybatis.pojo.Dept;
    3. import org.apache.ibatis.annotations.Param;
    4. import java.util.List;
    5. public interface LessMapper {
    6. //多对一分步查询第二步
    7. List selectEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);
    8. }

     在LessMapper.xml文件中写入第二步查询方法

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.mybatis.mapper.LessMapper">
    6. <select id="selectEmpAndDeptByStepTwo" resultType="Dept">
    7. select *from dept where dept_id = #{dept}
    8. select>
    9. mapper>

    完善MoreMapper里面的select和column

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.mybatis.mapper.MoreMapper">
    6. <resultMap id="selectOne" type="Emp">
    7. <id column="emp_id" property="empId">id>
    8. <result column="emp_name" property="empName">result>
    9. <result column="age" property="age">result>
    10. <result column="gender" property="gender">result>
    11. <result column="dept_id" property="dept.deptId">result>
    12. <result column="dept_name" property="dept.deptName">result>
    13. resultMap>
    14. <resultMap id="selectTwo" type="Emp">
    15. <id column="emp_id" property="empId">id>
    16. <result column="emp_name" property="empName">result>
    17. <result column="age" property="age">result>
    18. <result column="gender" property="gender">result>
    19. <association property="dept" javaType="Dept">
    20. <id column="dept_id" property="deptId">id>
    21. <result column="dept_name" property="deptName">result>
    22. association>
    23. resultMap>
    24. <select id="selectEmpAndDeptByEmpId" resultMap="selectTwo">
    25. select *from emp join dept on emp.emp_id = dept.dept_id
    26. where emp_id = #{empId}
    27. select>
    28. <resultMap id="MoreMapperOne" type="Emp">
    29. <id column="emp_id" property="empId">id>
    30. <result column="emp_name" property="empName">result>
    31. <result column="age" property="age">result>
    32. <result column="gender" property="gender">result>
    33. <association property="dept"
    34. select="com.mybatis.mapper.LessMapper.selectEmpAndDeptByStepTwo"
    35. column="dept_id">association>
    36. resultMap>
    37. <select id="selectEmpAndDeptByStepOne" resultMap="MoreMapperOne">
    38. select *from emp where emp_id = #{empId}
    39. select>
    40. mapper>

    最后开始测试:

     分步查询成功!!!


    三、一对多的使用

    现在我们把Emp表当成多,Dept表当成一,在LessMapper里面写则可以实现一对多

    1.创建接口LessMapper

    1. package com.mybatis.mapper;
    2. import com.mybatis.pojo.Dept;
    3. import org.apache.ibatis.annotations.Param;
    4. import java.util.List;
    5. public interface LessMapper {
    6. //多对一分步查询第二步
    7. List selectEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);
    8. //一对多查询
    9. List selectEmpAndDeptByDeptId(@Param("deptId") Integer deptId);
    10. }

    2、多对一查询实现的两种方法 

    方法一、用collection标签

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.mybatis.mapper.LessMapper">
    6. <select id="selectEmpAndDeptByStepTwo" resultType="Dept">
    7. select *from dept where dept_id = #{dept}
    8. select>
    9. <resultMap id="selectOne" type="Dept">
    10. <id column="dept_id" property="deptId">id>
    11. <result column="dept_name" property="deptName">result>
    12. <collection property="emps" ofType="Emp">
    13. <id column="emp_id" property="empId">id>
    14. <result column="emp_name" property="empName">result>
    15. <result column="age" property="age">result>
    16. <result column="gender" property="gender">result>
    17. collection>
    18. resultMap>
    19. <select id="selectEmpAndDeptByDeptId" resultMap="selectOne">
    20. select *from dept join emp on dept.dept_id = emp.dept_id where dept.dept_id = #{deptId}
    21. select>
    22. mapper>

    测试结果如下:

    方法一测试成功!!!

    方法二、用分步查询

    分步查询第一步:

    只查询Dept表里面的全部内容

    在LessMapper里面写入分步查询第一步方法

    1. package com.mybatis.mapper;
    2. import com.mybatis.pojo.Dept;
    3. import org.apache.ibatis.annotations.Param;
    4. import java.util.List;
    5. public interface LessMapper {
    6. //多对一分步查询第二步
    7. List selectEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);
    8. //一对多查询
    9. List selectEmpAndDeptByDeptId(@Param("deptId") Integer deptId);
    10. //一对多分步查询第一步
    11. List selectDeptAndEmpByStepOne(@Param("deptId") Integer deptId);
    12. }

    在LessMapper.xml里面写入第一步查询语句

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.mybatis.mapper.LessMapper">
    6. <select id="selectEmpAndDeptByStepTwo" resultType="Dept">
    7. select *from dept where dept_id = #{dept}
    8. select>
    9. <resultMap id="selectOne" type="Dept">
    10. <id column="dept_id" property="deptId">id>
    11. <result column="dept_name" property="deptName">result>
    12. <collection property="emps" ofType="Emp">
    13. <id column="emp_id" property="empId">id>
    14. <result column="emp_name" property="empName">result>
    15. <result column="age" property="age">result>
    16. <result column="gender" property="gender">result>
    17. collection>
    18. resultMap>
    19. <select id="selectEmpAndDeptByDeptId" resultMap="selectOne">
    20. select *from dept join emp on dept.dept_id = emp.dept_id where dept.dept_id = #{deptId}
    21. select>
    22. <resultMap id="LessMapperOne" type="Dept">
    23. <id column="dept_id" property="deptId">id>
    24. <result column="dept_name" property="deptName">result>
    25. <collection property="emps"
    26. select=""
    27. column="">collection>
    28. resultMap>
    29. <select id="selectDeptAndEmpByStepOne" resultMap="LessMapperOne">
    30. select *from dept where dept_id = #{deptId}
    31. select>
    32. mapper>

    resultMap里面的关键字select表示第二步查询方法的唯一标识,column为查询的条件

    开始准备分步查询第二步,创建MoreMapper接口写入方法名

    1. package com.mybatis.mapper;
    2. import com.mybatis.pojo.Emp;
    3. import org.apache.ibatis.annotations.Param;
    4. import java.util.List;
    5. public interface MoreMapper {
    6. //通过员工Id来查询员工信息和部门信息
    7. List selectEmpAndDeptByEmpId(@Param("empId") Integer empId);
    8. //多对一分步查询第一步
    9. List selectEmpAndDeptByStepOne(@Param("empId") Integer empId);
    10. //一对多分步查询第二步
    11. List selectDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);
    12. }

     在LessMapper.xml文件中写入第二步查询方法

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.mybatis.mapper.MoreMapper">
    6. <resultMap id="selectOne" type="Emp">
    7. <id column="emp_id" property="empId">id>
    8. <result column="emp_name" property="empName">result>
    9. <result column="age" property="age">result>
    10. <result column="gender" property="gender">result>
    11. <result column="dept_id" property="dept.deptId">result>
    12. <result column="dept_name" property="dept.deptName">result>
    13. resultMap>
    14. <resultMap id="selectTwo" type="Emp">
    15. <id column="emp_id" property="empId">id>
    16. <result column="emp_name" property="empName">result>
    17. <result column="age" property="age">result>
    18. <result column="gender" property="gender">result>
    19. <association property="dept" javaType="Dept">
    20. <id column="dept_id" property="deptId">id>
    21. <result column="dept_name" property="deptName">result>
    22. association>
    23. resultMap>
    24. <select id="selectEmpAndDeptByEmpId" resultMap="selectTwo">
    25. select *from emp join dept on emp.emp_id = dept.dept_id
    26. where emp_id = #{empId}
    27. select>
    28. <resultMap id="MoreMapperOne" type="Emp">
    29. <id column="emp_id" property="empId">id>
    30. <result column="emp_name" property="empName">result>
    31. <result column="age" property="age">result>
    32. <result column="gender" property="gender">result>
    33. <association property="dept"
    34. select="com.mybatis.mapper.LessMapper.selectEmpAndDeptByStepTwo"
    35. column="dept_id">association>
    36. resultMap>
    37. <select id="selectEmpAndDeptByStepOne" resultMap="MoreMapperOne">
    38. select *from emp where emp_id = #{empId}
    39. select>
    40. <select id="selectDeptAndEmpByStepTwo" resultType="Emp">
    41. select * from emp where dept_id = #{deptId}
    42. select>
    43. mapper>

    完善LessMapper里面的select和column

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.mybatis.mapper.LessMapper">
    6. <select id="selectEmpAndDeptByStepTwo" resultType="Dept">
    7. select *from dept where dept_id = #{dept}
    8. select>
    9. <resultMap id="selectOne" type="Dept">
    10. <id column="dept_id" property="deptId">id>
    11. <result column="dept_name" property="deptName">result>
    12. <collection property="emps" ofType="Emp">
    13. <id column="emp_id" property="empId">id>
    14. <result column="emp_name" property="empName">result>
    15. <result column="age" property="age">result>
    16. <result column="gender" property="gender">result>
    17. collection>
    18. resultMap>
    19. <select id="selectEmpAndDeptByDeptId" resultMap="selectOne">
    20. select *from dept join emp on dept.dept_id = emp.dept_id where dept.dept_id = #{deptId}
    21. select>
    22. <resultMap id="LessMapperOne" type="Dept">
    23. <id column="dept_id" property="deptId">id>
    24. <result column="dept_name" property="deptName">result>
    25. <collection property="emps"
    26. select="com.mybatis.mapper.MoreMapper.selectDeptAndEmpByStepTwo"
    27. column="dept_id">collection>
    28. resultMap>
    29. <select id="selectDeptAndEmpByStepOne" resultMap="LessMapperOne">
    30. select *from dept where dept_id = #{deptId}
    31. select>
    32. mapper>

    开始测试:

    分步查询成功!!!


    四、总结

    创作不易,请大家多多点赞多多支持,努力创造出更好的作品!!!

  • 相关阅读:
    矩阵分析与应用-16-广义逆矩阵
    软件的开发步骤,需求分析,开发环境搭建,接口文档 ---苍穹外卖1
    排序3——C语言
    猿创征文|【FreeSwitch开发实践】使用sipp对FreeSwitch进行压力测试
    记一次 .NET某列控连锁系统 崩溃分析
    CSS 盒子模型(Box Model) | 青训营笔记
    java基础1
    【JVM调优实战100例】05——方法区调优实战(下)
    lua移植及使用
    【虚拟机】VMware的NAT模式、桥接模式、仅主机模式
  • 原文地址:https://blog.csdn.net/m0_62476845/article/details/133435813