概述
MyBatis学习–映射文件_select_resultMap_关联查询_环境搭建以及级联属性封装结果以及association定义关联对象封装规则,主要通过association来实现关联查询,这篇文章通过collection定义关联集合封装规则来实现关联查询。
两篇文章的区别:
- association主要解决的是多对一的问题(例如,多个员工属于同一部门,查询某个员工信息,同时查询其所属部门信息,这个员工只能隶属一个部门)
- collection主要针对一对多的问题(例如,一个部门下可以有很多员工,查询某个部门信息,同时查询该部门下所有员工信息)
collection定义关联集合封装规则,实现关联查询示例
- 相关的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