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

在上上篇中我们用级联属性的方式把数据查出来封装到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.控制台信息




二、分段查询

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.控制台信息:分步查询效果


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值