MyBatis的collection集合封装规则

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhupengqq/article/details/78627609

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

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

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>
    
    
	<!-- public Department getDeptByIdPlus(Integer id); -->
	<!-- 嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则 -->
	<resultMap type="com.cn.zhu.bean.Department" id="MyDept">
		<id column="did" property="id" />
		<result column="dept_name" property="departmentName" />
		<!--
			collection 定义关联集合类型的属性的封装 ofType 指定集合里面元素的类型
		-->
		<collection property="emps" ofType="com.cn.zhu.bean.Employee">
			<!-- 定义这个集合元素的封装规则 -->
			<id column="eid" property="id" />
			<result column="last_name" property="lastName" />
			<result column="email" property="email" />
			<result column="gender" property="gender" />
		</collection>
	</resultMap>
	<select id="getDeptByIdPlus" resultMap="MyDept">
		SELECT d.id
		did,d.dept_name dept_name,
		e.id eid,e.last_name last_name,
		e.email
		email,e.gender gender
		FROM tbl_dept d LEFT JOIN tbl_employee e
		on
		d.id=e.d_id
		where d.id=#{id}
    </select>

	
	<!--  扩展,多列的值传递过去 
              将多列的值封装map传递
              column="{key1=column,key2=column2}"
              fetchType="lazy" : 表示使用延迟加载
                  lazy 延迟
                  eager 立即加载
    -->

</mapper>

mybatis-config.xml

	<mappers>
		<mapper resource="mybatis/mapper/EmployeeMapperPlus.xml" />
		<mapper resource="mybatis/mapper/DepartmentMapper.xml" />
	</mappers>

测试方法:

@Test
	public void test06() throws IOException{
		SqlSessionFactory sqlsessionFactory=getSqlSessionFactory();
		// 1  获取到的sqlsession不会自动提交数据
		SqlSession openSession=sqlsessionFactory.openSession();

		try{

			DepartmentMapper mapper=openSession.getMapper(DepartmentMapper.class);
			//级联查询 
			Department   employee=mapper.getDeptByIdPlus(1);
			System.out.println(employee);
			System.out.println(employee.getEmps());
			

		}finally{
			openSession.commit();
		}
	}


接下来会写collection的分段查询

展开阅读全文

没有更多推荐了,返回首页