MyBatis的collection集合的分布查询
DepartmentMapper.java
package com.cn.mybatis.dao; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.MapKey; import org.apache.ibatis.annotations.Param; import com.cn.zhu.bean.Department; import com.cn.zhu.bean.Employee; public interface DepartmentMapper { public Department getDeptById(Integer id); public Department getDeptByIdPlus(Integer id); public Department getDeptByIdStep(Integer id); public List<Employee> getEmpsByDeptId(Integer deptId); }
EmployeeMapperPlus.java
[java] view plain copy
- package com.cn.mybatis.dao;
- import java.util.List;
- import java.util.Map;
- import org.apache.ibatis.annotations.MapKey;
- import org.apache.ibatis.annotations.Param;
- import com.cn.zhu.bean.Employee;
- public interface EmployeeMapperPlus {
- public Employee getEmpById(Integer id);
- public Employee getEmpAndDept(Integer id);
- public Employee getEmpByIdStep(Integer id);
- }
Department.java
[java] view plain copy
- package com.cn.zhu.bean;
- import java.util.List;
- 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 [departmentName=" + departmentName + ", id=" + id
- + "]";
- }
- }
DepartmentMapper.xml
<?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.cn.mybatis.dao.DepartmentMapper"> <!-- public Department getDeptById(Integer id); --> <select id="getDeptById" resultType="com.cn.zhu.bean.Department"> select id,dept_name departmentName from tbl_dept where id=#{id} </select> <!-- collection集合分段查询 --> <resultMap type="com.cn.zhu.bean.Department" id="MyDeptStept"> <id column="id" property="id" /> <result column="dept_name" property="departmentName" /> <collection property="emps" select="com.cn.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId" column="{deptId=id}"> </collection> </resultMap> <!-- public Department getDeptByIdStep(Integer id); --> <select id="getDeptByIdStep" resultMap="MyDeptStept"> select id,dept_name departmentName from tbl_dept where id=#{id} </select> <!-- 扩展,多列的值传递过去 将多列的值封装map传递 column="{key1=column,key2=column2}" fetchType="lazy" : 表示使用延迟加载 lazy 延迟 eager 立即加载 --> </mapper>
EmployeeMapperPlus.xml
<?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.cn.mybatis.dao.EmployeeMapperPlus"> <!-- 分布好处 可以使用延迟加载 Employee==>Dept 我们每次查询Employee对象的时候,都将一起查询出来。 部门信息在我们使用的时候再去查询 分段查询的基础之上加上两个配置 --> <!-- 查询二 查询部门的时候将部门对应的所有员工信息也查询出来 public List<Employee> getEmpsByDeptId(Integer deptId); --> <select resultType="com.cn.zhu.bean.Employee" id="getEmpsByDeptId"> select * from tbl_employee where d_id=#{deptId} </select> </mapper>
mybatis-config.xml
[html] view plain copy
- <mappers>
- <mapper resource="mybatis/mapper/EmployeeMapperPlus.xml" />
- <mapper resource="mybatis/mapper/DepartmentMapper.xml" />
- </mappers>