Day23——MyBatis自定义映射 collection

一. 知识储备

在这里插入图片描述

二. 例子

Department.java

package com.atguigu.mybatis.beans;

import java.util.List;

public class Department {
	
     private Integer id;
     private String departmentName;
     
     private List<Employee> emps;
     
     
	public Department() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
	public String getDepartmentName() {
		return departmentName;
	}


	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}


	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 getDepartment() {
		return departmentName;
	}
	public void setDepartment(String departmentName) {
		this.departmentName = departmentName;
	}
	@Override
	public String toString() {
		return "Department [id=" + id + ", department=" + departmentName + "]";
	}
     
}

DepartmentMapperResultMap.java

package com.atguigu.mybatis.dao;

import com.atguigu.mybatis.beans.Department;

public interface DepartmentMapperResultMap {

	public Department selectDepartmentAndEmployees(Integer id);
	
}

DepartmentMapperResultMap.xml

<!-- public Department selectDepartmentAndEmployees(Integer id); -->
	<select id="selectDepartmentAndEmployees" resultMap="myDeptAndEmps">
	     select d.id did, d.dept_name, e.id eid, e.last_name, e.email, e.gender
	     from tbl_dept d left join tbl_employee e 
	     on d.id = e.d_id where d.id = #{id}
	</select>
	<resultMap type="com.atguigu.mybatis.beans.Department" id="myDeptAndEmps">
	     <id column="did" property="id"/>
	     <result column="dept_name" property="departmentName"/>
	     <!-- 
	        collection:集合类型关联属性
	           property:关联属性名字
	           ofType: 指定集合中元素的类型
	      -->
	      <collection property="emps" ofType="com.atguigu.mybatis.beans.Employee">
	         <id column="eid" property="id"/>
	         <result column="last_name" property="lastName"/>
	         <result column="email" property="email"/>
	         <result column="gender" property="gender"/>
	      </collection>
	</resultMap>

TestMyBatisResultMap.java

	@Test
	public void testResultMapCollection() throws Exception {
		SqlSessionFactory ssf = getSqlSessionFactory();
		SqlSession session = ssf.openSession();
		
		try {
              DepartmentMapperResultMap mapper = 
            		  session.getMapper(DepartmentMapperResultMap.class);			
              Department dept = mapper.selectDepartmentAndEmployees(1);
              System.out.println(dept);
              System.out.println(dept.getEmps());
			
			session.commit();
		} finally {
			// TODO: handle finally clause
			session.close();
		}
	}

public SqlSessionFactory getSqlSessionFactory() throws Exception {
		String resource = "mybatis-config.xml"; 
		InputStream inputStream = 
				Resources.getResourceAsStream(resource); 
		SqlSessionFactory sqlSessionFactory = 
				new SqlSessionFactoryBuilder().build(inputStream);
		return sqlSessionFactory;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值