mybatis映射文件select_resultMap_关联查询_collection定义关联集合
Posted shuaiflying
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis映射文件select_resultMap_关联查询_collection定义关联集合相关的知识,希望对你有一定的参考价值。
知识点:查询一个实体类,并查出这个类下面的集合
Employee.java实体类
package com.hand.mybatis.bean;
public class Employee {
private Integer eId;
private String eName;
private Integer gender;
private String email;
private Department dept;
public Employee() {
super();
}
public Employee(Integer eId,String eName, Integer gender, String email) {
super();
this.eId=eId;
this.eName = eName;
this.gender = gender;
this.email = email;
}
public Integer geteId() {
return eId;
}
public void seteId(Integer eId) {
this.eId = eId;
}
public String getEName() {
return eName;
}
public void setEname(String ename) {
this.eName = ename;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee [eId=" + eId + ", ename=" + eName + ", gender=" + gender + ", email=" + email + "]";
}
}
Department.java实体类
package com.hand.mybatis.bean;
import java.util.List;
public class Department {
private Integer id;
private String departName;
private List<Employee> empList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartName() {
return departName;
}
public void setDepartName(String departName) {
this.departName = departName;
}
public List<Employee> getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
}
@Override
public String toString() {
return "Department [id=" + id + ", departName=" + departName + "]";
}
}
DepartmentMapper.java接口
package com.hand.mybatis.dao;
import com.hand.mybatis.bean.Department;
public interface DepartmentMapper {
Department getDeptById(Integer did);
//根据部门id,查询部门信息,同时将部门下的所有员工信息查询出来
Department getDeptByIdPlus(Integer did);
//分步查询部门信息
Department getDeptByIdStep(Integer did);
}
DepartmentMapper.xm l映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hand.mybatis.dao.DepartmentMapper">
<!-- Department getDeptById(Integer did); -->
<select id="getDeptById" resultType="com.hand.mybatis.bean.Department">
SELECT did id,deptname departName FROM dept WHERE did=#{did}
</select>
<!--1.查询部门的时候将部门对应的所有员工信息也查询出来
-->
<resultMap type="com.hand.mybatis.bean.Department" id="departmentMap">
<result property="id" column="did"/>
<result property="departName" column="deptname"/>
<!--collection定义关联集合类型的属性的封装规则
ofType:指定集合里面元素的类型 -->
<collection property="empList" ofType="com.hand.mybatis.bean.Employee">
<!-- 定义这个集合元素的封装规则 -->
<id column="eid" property="eId"/>
<result column="ename" property="eName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</collection>
</resultMap>
<!-- Department getDeptByIdStep(Integer did); --> 接口一:根据部门did查询部门信息,员工信息
<select id="getDeptByIdPlus" resultMap="departmentMap">
SELECT d.did,d.deptname,e.eid,e.ename,e.email,e.gender
FROM dept d
LEFT JOIN emp e
ON d.did=e.did
WHERE d.did=#{did}
</select>
<!--2. select_resultMap关联查询collection分步查询 -->
<resultMap type="com.hand.mybatis.bean.Department" id="departmentMapPlus">
<id column="did" property="id"/>
<result column="deptname" property="departName"/>
<collection property="empList"
select="com.hand.mybatis.dao.EmployeeMapper.getEmpByDeptId"
column="{dId=did}" fetchType="lazy">//可以多列传值
</collection>
</resultMap>
<!--Department getDeptByIdStep(Integer did); -->接口二:根据部门id分步查询员工信息
<select id="getDeptByIdStep" resultMap="departmentMapPlus">
SELECT did,deptname
from dept
WHERE did=#{did}
</select>
<!-- 扩展,多列的值传递过去,将多列的值封装map传递
column="{key1=column1,key2=column2}"
fetchType="lazy":表示使用延迟加载
- lazy:延迟
- eager:立即-->
</mapper>
以上是关于mybatis映射文件select_resultMap_关联查询_collection定义关联集合的主要内容,如果未能解决你的问题,请参考以下文章