MyBatis 关联查询– collection定义关联集合封装
应用场景 当关联查询时关联的是一个集合而不是一个对象时 例如:查询一个部门中的所有员工信息 此时使用ResultMap中的collection标签对关联的集合定义封装规则:
1。创建Department 和Employee对象
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;
}
public Department() {
super();
}
public Department(Integer id, String departmentName) {
super();
this.id = id;
this.departmentName = departmentName;
}
}
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
private Department dept;
public Employee(Integer id, String lastName, String email, String gender) {
super();
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
}
public Employee() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + "]";
}
}
定义接口方法
public interface EmployeeMapperPlus {
public Employee getEmployee(Integer id);
public Employee getEmpAndDept(Integer id);
}
collection嵌套结果集:
<!-- 查询部门将所有的员工 -->
<!-- public Employee getEmpAndDept(Integer id); -->
<resultMap type="com.atguigu.mybatis.bean.Department" id="Mydept">
<id column="did" property="id"/>
<result column="dept_name" property="departmentName"/>
<!--collection定义关联几个类型的属性的封装规则 -->
<!-- offType指定集合里面元素的类型 -->
<collection property="emps" ofType="com.atguigu.mybatis.bean.Employee">
<!-- 定义集合中元素的封装规则 -->
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</collection>
</resultMap>
<select id="getDepByIdPlus" resultMap="">
<!--
private Integer id;
private String departmentName;
private List<Employee> emps;
-->
SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
WHERE e.d_id=d.id AND e.id=#{id}
</select>
其中
property:表示要封装成集合的javabean中的属性 例如Department类中有一个属性list persons表示部门中的所有人
ofType:表示封装的集合中的元素的java类型
测试类
public void test06() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try{
DepartmentMapper mapper = openSession.getMapper(DepartmentMapper.class);
Department department = mapper.getDeptByIdPlus(1);
System.out.println(department.getEmps());
}finally{
openSession.close();
}
}