21.MyBatis学习--映射文件_select_resultMap_关联查询_collection分步查询&延迟加载

一、概述

在前的文章中介绍了通过association实现分步查询&延迟加载(MyBatis学习–映射文件_select_resultMap_关联查询_association分步查询&&延时加载),以及上一篇文章中通过collection实现级联查询结果集封装(MyBatis学习-映射文件_select_resultMap_关联查询_collection定义关联集合封装规则),这篇文章主要通过collection来实现分步查询&延迟加载。

二、使用场景

这里,以查询某一部门信息,同时查询部门下所有员工信息为例。

  1. 首先根据部门id查询出部门信息;
  2. 然后根据查询到的部门信息的部门id查找所有的员工信息
  3. 将查询的员工信息封装

三、使用collection实现分步查询

  1. 相关JavaBean
    Department.java
public class Department {
   

	private Integer id;
	private String departmentName;
	private List<Employee> emps;



	public List<Employee> getEmps() {
   
		return emps;
	}
	public void setEmps(List<Employee> emps) {
   
		this.emps = emps;
	}
	public Integer getId() {
   
		return id;
	}
	public void setId(Integer id) {
   
		this.id = id;
	}
	public String getDepartmentName() {
   
		return departmentName;
	}
	public void setDepartmentName(String departmentName) {
   
		this.departmentName = departmentName;
	}
	@Override
	public String toString() {
   
		return "Department [id=" + id + ", departmentName=" + departmentName
				+ "]";
	}

	

}

Emlpoyee.java

public class Employee {
   

	private Integer id;
	private String lastName;
	private String email;
	private String gender;
	private Department dept;

	public Employee() {
   
		super();
	}

	public Employee(Integer id, String lastName, String email, String gender) {
   
		super();
		this.id = id;
		this.last
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值