MyBatis的collection集合的分布查询

 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

  1. package com.cn.mybatis.dao;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6.   
  7. import org.apache.ibatis.annotations.MapKey;  
  8. import org.apache.ibatis.annotations.Param;  
  9.   
  10. import com.cn.zhu.bean.Employee;  
  11.   
  12. public interface EmployeeMapperPlus {  
  13.     public Employee getEmpById(Integer id);  
  14.     public Employee getEmpAndDept(Integer id);  
  15.     public Employee getEmpByIdStep(Integer id);  
  16.  }  

Department.java

[java] view plain copy

  1. package com.cn.zhu.bean;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class Department {  
  6.     private     Integer  id;  
  7.     private  String  departmentName;  
  8.     private List<Employee> emps;  
  9.       
  10.       
  11.       
  12.     public List<Employee> getEmps() {  
  13.         return emps;  
  14.     }  
  15.     public void setEmps(List<Employee> emps) {  
  16.         this.emps = emps;  
  17.     }  
  18.     public Integer getId() {  
  19.         return id;  
  20.     }  
  21.     public void setId(Integer id) {  
  22.         this.id = id;  
  23.     }  
  24.     public String getDepartmentName() {  
  25.         return departmentName;  
  26.     }  
  27.     public void setDepartmentName(String departmentName) {  
  28.         this.departmentName = departmentName;  
  29.     }  
  30.     @Override  
  31.     public String toString() {  
  32.         return "Department [departmentName=" + departmentName + ", id=" + id  
  33.                 + "]";  
  34.     }  
  35.       
  36. }  


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

  1. <mappers>  
  2.     <mapper resource="mybatis/mapper/EmployeeMapperPlus.xml" />  
  3.     <mapper resource="mybatis/mapper/DepartmentMapper.xml" />  
  4. </mappers>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值