MyBatis的级联查询(两种方式)

 

与上次唯一不同的一下几个类

Department.java

 

package com.cn.zhu.bean;

public class Department {
	private 	Integer  id;
	private  String  departmentName;
	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
				+ "]";
	}
	
}

Employee.java

package com.cn.zhu.bean;

import org.apache.ibatis.type.Alias;

@Alias("emp")
public class Employee {
	private Integer id;
	private String lastName;
	private String email;
	private String gender;
	private Department dept;
	
	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public Employee(Integer id, String lastName, String email, String gender) {
		super();
		this.id = id;
		this.lastName = lastName;
		this.email = email;
		this.gender = gender;
	}

	
	public Department getDept() {
		return dept;
	}

	public void setDept(Department dept) {
		this.dept = dept;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	@Override
	public String toString() {
		return "Employee [email=" + email + ", gender=" + gender + ", id=" + id
		+ ", lastName=" + lastName + "]";
	}

}


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);
 }


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">

	<!--
		自定义某个javaBean的封装规则 id 定义主键会底层有优化 type: 自定义规则的java类型 property :
		唯一id方便引用
	-->
	<!--
		<resultMap type="com.cn.zhu.bean.Employee" id="MySimpleEmp"> 指定主键的封装规则
		column 指定哪一列 property 指定对应的javaBean属性 <id column="id" property="id"/>
		定义普通列封装 <result column="last_name" property="lastName"/> 其他不指定的列会自动封装
		,我们只要写resultMap就把全部的映射规则都写上 <result column="email" property="email"/>
		<result column="gender" property="gender"/> </resultMap>
	--><!-- public Employee getEmpById(Integer id); -->
	<!-- resultMap: 自定义结果集映射规则 -->
	<!--
		<select id="getEmpById" resultMap="MyEmp"> select * from tal_employee
		where id=#{id} </select>
	-->
	<!--
		场景一 : 查询Employee的同时查询员工对应的部门 Employee ===department 一个员工有与之对应的部门信息
	-->

	<resultMap type="com.cn.zhu.bean.Employee" id="MyDifEmp">
		<!--
			指定主键的封装规则 column 指定哪一列 property 指定对应的javaBean属性
		-->
		<!-- 
                  联合查询 :级联属性封装结果
        -->
		<id column="id" property="id" />
		<!-- 定义普通列封装 -->
		<result column="last_name" property="lastName" />
		<!-- 其他不指定的列会自动封装 ,我们只要写resultMap就把全部的映射规则都写上 -->
		<result column="gender" property="gender" />
		<result column="did" property="dept.id" />
		<result column="dept_name" property="dept.departmentName" />
	</resultMap>

  <!--  使用association  定义单个对象的封装规则 -->
	<resultMap type="com.cn.zhu.bean.Employee" id="MyDifEmp2">
		<id column="id" property="id" />
		<!-- 定义普通列封装 -->
		<result column="last_name" property="lastName" />
		<!-- 其他不指定的列会自动封装 ,我们只要写resultMap就把全部的映射规则都写上 -->
		<result column="gender" property="gender" />
		<!--association  可以指定联合的javaBean对象
		  property 指定哪个是联合的对象
		  javaType: 指定这个属性对象的类型
		  
		  -->
		<association property="dept" javaType="com.cn.zhu.bean.Department">
		  <id column="did" property="id"/>
		   <result column="dept_name" property="departmentName"/>
		</association>
	</resultMap>


	<!-- public Employee getEmpAndDept(Integer id); -->
	<select id="getEmpAndDept" resultMap="MyDifEmp2">
		SELECT e.id id,e.last_name last_name ,e.gender gender,e.d_id d_id,
		d.id did,d.dept_name dept_name from tbl_employee e,tbl_dept d
		where e.d_id=d.id and e.id=#{id}
	</select>
</mapper>

 

以上是两种映射封装结果


增加EmployeeMapperPlus.xml 后,一定要在mybatis-config.xml里加上这个配置文件的声明,

 

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

另外sql语句一定要正确,可以先在navict数据库图形化管理工具中,测试sql

 

 

下面编写测试类(这里直接写测试方法吧):详细请看映射一

 

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

		try{
			
			EmployeeMapperPlus mapper=openSession.getMapper(EmployeeMapperPlus.class);
            Employee empAndDept=mapper.getEmpAndDept(1);
            System.out.println(empAndDept);
            System.out.println(empAndDept.getDept());
		}finally{
			openSession.commit();
		}
	}


测试完成

 

 

接下来,会写分布查询,请看下一篇文章

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值