欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【Mybatis】collection解决一对多关联查询

程序员文章站 2022-05-23 14:17:42
...
在上上篇中我们用级联属性的方式把数据查出来封装到Employee中,还有另一种方式实现数据的封装。
接下来小编将介绍collection标签的相关知识,包括嵌套查询和分段查询两种方式。

现在有员工和部门两张表,前两篇文章介绍的都是根据员工查部门,本篇文章介绍由部门查所有员工

首先请参照上上篇中步骤1,2,3 , 4,此处从第5步开始


一、嵌套查询

5.新增DepartmentMapper.java接口方法

  1. public interface DepartmentMapper {
  2. public Department getDeptByIdPlus(Integer id);
  3. }

6.新增DepartmentMapper.xml文件,配置信息如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.atguigu.mybatis.dao.DepartmentMapper">
  6. <!-- collection嵌套结果集的方式,定义关联的集合类型元素的封装规则-->
  7. <!-- 嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则 -->
  8. <resultMap type="com.atguigu.mybatis.bean.Department" id="MyDept">
  9. <id column="did" property="id"/>
  10. <result column="dept_name" property="departmentName"/>
  11. <!--
  12. collection定义关联结合类型的属性的封装规则
  13. ofType:指定集合里面元素的类型
  14. -->
  15. <collection property="emps" ofType="com.atguigu.mybatis.bean.Employee">
  16. <!-- 定义这个集合中元素的封装规则 -->
  17. <id column="eid" property="id"/>
  18. <result column="last_name" property="lastName"/>
  19. <result column="email" property="email"/>
  20. <result column="gender" property="gender"/>
  21. </collection>
  22. </resultMap>
  23. <!-- public Department getDeptByIdPlus(Integer id); -->
  24. <select id="getDeptByIdPlus" resultMap="MyDept" >
  25. SELECT
  26. d.id did,
  27. d.dept_name dept_name,
  28. e.id,
  29. e.last_name last_name,
  30. e.email email,
  31. e.gender gender
  32. FROM
  33. tbl_dept d
  34. LEFT JOIN tbl_employee e ON d.id = e.d_id
  35. WHERE
  36. d.id = #{id}
  37. </select>
  38. </mapper>

7.新增测试方法

  1. @Test
  2. public void test06() throws IOException{
  3. SqlSessionFactory sqlSesssionFactory = getSqlSesssionFactory();
  4. SqlSession openSession = sqlSesssionFactory.openSession();
  5. try {
  6. DepartmentMapper mapper = openSession.getMapper(DepartmentMapper.class);
  7. Department deptByIdPlus = mapper.getDeptByIdPlus(2);
  8. System.out.println(deptByIdPlus);
  9. System.out.println(deptByIdPlus.getEmps());
  10. } finally {
  11. openSession.close();
  12. }
  13. }

8.控制台信息

【Mybatis】collection解决一对多关联查询



二、分段查询

5.新增EmployeeMapperPlus.xml文件,配置信息如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.atguigu.mybatis.dao.EmployeeMapperPlus">
  6. <select id="getEmpsByDeptId" resultType="com.atguigu.mybatis.bean.Employee">
  7. select * from tbl_employee where d_id=#{deptId}
  8. </select>
  9. <mapper/>

6.DepartmentMapper.xml文件,配置信息

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.atguigu.mybatis.dao.EmployeeMapperPlus">
  6. <resultMap type="com.atguigu.mybatis.bean.Department" id="MyDeptStep">
  7. <id column="id" property="id"/>
  8. <id column="dept_name" property="departmentName"/>
  9. <collection property="emps"
  10. select="com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
  11. column="{deptId=id}" fetchType="lazy"></collection>
  12. </resultMap>
  13. <!-- public Department getDeptByIdStep(Integer id); -->
  14. <select id="getDeptByIdStep" resultMap="MyDeptStep">
  15. select id,dept_name departmentName from tbl_dept where id=#{id}
  16. </select>
  17. <mapper/>


7.新增测试方法

  1. @Test
  2. public void test06() throws IOException{
  3. SqlSessionFactory sqlSesssionFactory = getSqlSesssionFactory();
  4. SqlSession openSession = sqlSesssionFactory.openSession();
  5. try {
  6. DepartmentMapper mapper = openSession.getMapper(DepartmentMapper.class);
  7. Department deptByIdStep = mapper.getDeptByIdStep(1);
  8. System.out.println(deptByIdStep);
  9. } finally {
  10. openSession.close();
  11. }
  12. }

8.控制台信息:分步查询效果

【Mybatis】collection解决一对多关联查询

相关标签: Mybatis collection