关联.多对一关联查询
package org.mybatis.example.dao;

import java.util.Date;
//雇员类
public class Emp {
	private Integer empno;
	private String ename;
	private String job;
	private Integer mgr;
	private Date hiredate;
	private Integer sal;
	private Integer comm;
	private Dept dept;
	public Emp() {
		// TODO Auto-generated constructor stub
	}
	public Integer getEmpno() {
		return empno;
	}
	public void setEmpno(Integer empno) {
		this.empno = empno;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public Integer getMgr() {
		return mgr;
	}
	public void setMgr(Integer mgr) {
		this.mgr = mgr;
	}
	public Date getHiredate() {
		return hiredate;
	}
	public void setHiredate(Date hiredate) {
		this.hiredate = hiredate;
	}
	public Integer getSal() {
		return sal;
	}
	public void setSal(Integer sal) {
		this.sal = sal;
	}
	public Integer getComm() {
		return comm;
	}
	public void setComm(Integer comm) {
		this.comm = comm;
	}
	public Dept getDept() {
		return dept;
	}
	public void setDept(Dept dept) {
		this.dept = dept;
	}

}

//EmpMapper.java接口

package org.mybatis.example.dao;
import java.util.List;

public interface EmpMapper {
	public List<Emp>selectManytoOne();
}

关联关系,体现的是两个类之间的一种强依赖关系。比如在员工类中,有一个属性是部门类的对象;先看第一种 嵌套查询:

通过执行另外一个SQL映射语句来返回语气的复杂类型。

//整体mybatis配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<properties resource="db.properties"/>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
            <property name="driver" value="${driver}"/>
			<property name="url" value="${url}"/>
			<property name="username" value="${username}"/>
			<property name="password" value="${password}"/>
            </dataSource>
        </environment>
        </environments>
    <mappers>
        <mapper resource="org/mybatis/example/dao/DeptMapper.xml"/>
        <mapper resource="org/mybatis/example/dao/EmpMapper.xml"/>
    </mappers>
</configuration>

//EmpMapper.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="org.mybatis.example.dao.EmpMapper">
	<resultMap type="org.mybatis.example.dao.Emp" id="getEmpresultMap">
		<id column="empno" property="empno"/>
		<result column="ename" property="ename"/>
		<result column="job" property="job"/>
		<result column="mgr" property="mgr"/>
		<result column="hiredate" property="hiredate"/>
		<result column="sal" property="sal"/>
		<result column="comm" property="comm"/>
		<association property="dept" column="deptno"
			javaType="org.mybatis.example.dao.Dept">
			<id column="deptno" property="deptno"/>
			<result column="dname" property="dname"/>
			<result column="loc" property="loc"/>
		</association>
	</resultMap>
	<select id="selectManytoOne" resultMap="getEmpresultMap">
		select
		e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,
		e.deptno,d.dname,d.loc
		from emp e left join dept d on e.deptno=d.deptno
	</select>
</mapper>

//测试类

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.example.dao.Emp;
import org.mybatis.example.dao.EmpMapper;
import org.mybatis.example.dao.SqlSessionFactoryUtil;

public class Test21 {
	public static void main(String[] args) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		EmpMapper empmapper=session.getMapper(EmpMapper.class);
		List<Emp>empList=empmapper.selectManytoOne();
		for(Emp emp:empList){
			System.out.println(emp.getEname()+"的部门是:"+emp.getDept().getDname());
		}
	}
}

2.嵌套结果查询:使用嵌套结果映射来处理重复的联合结果的子集。

EmpMapper.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="org.mybatis.example.dao.EmpMapper">
	<resultMap id="empResult" type="org.mybatis.example.dao.Emp">
		<association property="dept" column="deptno"
			javaType="org.mybatis.example.dao.Dept" select="selectDept"/>
	</resultMap>
	<select id="selectEmp" parameterType="int" resultMap="empResult">
		select * from emp where empno=#{id}
	</select>
	<select id="selectDept" parameterType="int"
		resultType="org.mybatis.example.dao.Dept">
		select * from dept where deptno=#{id}
	</select>
	<select id="selectManytoOne" resultMap="getEmpresultMap">
        select
        e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,
        e.deptno,d.dname,d.loc
        from emp e left join dept d on e.deptno=d.deptno
    </select>
</mapper>

EmpMapper.java接口

增加方法:public List<Emp>selectEmp(int id);

测试类

public static void main(String[] args) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		EmpMapper empmapper=session.getMapper(EmpMapper.class);
		List<Emp>empList=empmapper.selectEmp(2);
		System.out.println(empList.size());
		for(Emp emp:empList){
			System.out.println(emp.getEname()+"的部门是:"+emp.getDept().getDname());
		}
	}

但是这种方法,在查询的时候只能孤立的查询某个员工id比较复杂,可以使用如下EmpMapper.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="org.mybatis.example.dao.EmpMapper">
	<resultMap type="org.mybatis.example.dao.Emp" id="getEmpresultMap">
		<id column="empno" property="empno"/>
		<result column="ename" property="ename"/>
		<result column="job" property="job"/>
		<result column="mgr" property="mgr"/>
		<result column="hiredate" property="hiredate"/>
		<result column="sal" property="sal"/>
		<result column="comm" property="comm"/>
		<association property="dept" column="deptno"
			javaType="org.mybatis.example.dao.Dept" resultMap="deptresultmap">
		</association>
	</resultMap>
	<resultMap type="org.mybatis.example.dao.Dept" id="deptresultmap">	
	<id column="deptno" property="deptno"/>
	<result column="dname" property="dname"/>
	<result column="loc" property="loc"/>
	</result>
	<select id="selectManytoOne" resultMap="getEmpresultMap">
		select
		e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,
		e.deptno,d.dname,d.loc
		from emp e left join dept d on e.deptno=d.deptno
	</select>
</mapper>

//一对多关联集合查询

重写下Dept类

增加多的一方的集合属性,private List<Emp>emps;并且添加相应的getter/setter方法;

重新配置DeptMapper.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="org.mybatis.example.dao.DeptMapper">
	<resultMap type="org.mybatis.example.dao.Dept" id="deptresultMap">
		<id column="deptno" property="deptno"/>
		<result column="dname" property="dname"/>
		<result column="loc" property="loc"/>
		<collection property="emps" ofType="org.mybatis.example.dao.Emp"
			resultMap="empresultmap">
		</collection>
	</resultMap>
	<resultMap type="org.mybatis.example.dao.Emp" id="empresultmap">
			<id column="empno" property="empno"/>
	        <result column="ename" property="ename"/>
	        <result column="job" property="job"/>
	        <result column="mgr" property="mgr"/>
	        <result column="hiredate" property="hiredate"/>
	        <result column="sal" property="sal"/>
	        <result column="comm" property="comm"/>
	</resultMap>
	<select id="selectOnetoMany" resultMap="deptresultMap">
		select
        e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,
        e.deptno,d.dname,d.loc
        from emp e left join dept d on e.deptno=d.deptno
	</select>
</mapper>

测试类代码

public static void main(String[] args) {
		SqlSession session=SqlSessionFactoryUtil.getSqlSession();
		DeptMapper deptmapper=session.getMapper(DeptMapper.class);
		List<Dept>deptList=deptmapper.selectOnetoMany();
		for(Dept dept:deptList){
			System.out.println("部门名称:"+dept.getDname());
			System.out.println("所属员工的个数:"+dept.getEmps().size());
		}
	}

MyBatis.4关联的更多相关文章

  1. MyBatis实现关联表查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

  2. Mybatis之关联查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

  3. MyBatis——实现关联表查询

    原文:http://www.cnblogs.com/xdp-gacl/p/4264440.html 一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创 ...

  4. mybatis 一对一关联 association 返回空值

    mybatis 一对一关联 association 返回空值 最近学习spring mvc + mybatis开发,看的书是<Spring MVC+Mybatis开发 从入门到精通>,在学 ...

  5. SpringBoot+Mybatis实现关联查询

    SpringBoot+Mybatis实现关联查询 今天学习了下Mybatis的动态查询,然后接着上次的Demo改造了下实现表的关联查询. 话不多说,开始今天的小Demo 首先接着上次的项目 https ...

  6. Java基础-SSM之mybatis一对一关联

    Java基础-SSM之mybatis一对一关联 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.准备测试环境(创建数据库表)  1>.创建husbands和wifes表并建 ...

  7. Mybatis系列(三):Mybatis实现关联表查询

    原文链接:http://www.cnblogs.com/xdp-gacl/p/4264440.html 一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 ...

  8. Mybatis表关联多对一

    在上章的 一对多 中,我们已经学习如何在 Mybatis 中关联多表,但在实际项目中也是经常使用 多对一 的情况,这些查询是如何处理的呢,在这一节中我们来学习它.多表映射的多对一关系要用到 mybit ...

  9. Mybatis之关联查询及动态SQL

    前言 实际开发项目中,很少是针对单表操作,基本都会联查多表进行操作,尤其是出一些报表的内容.此时,就可以使用Mybatis的关联查询还有动态SQL.前几篇文章已经介绍过了怎么调用及相关内容,因此这里只 ...

  10. MyBatis—实现关联表查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

随机推荐

  1. Netty源码分析第6章(解码器)---->第2节: 固定长度解码器

    Netty源码分析第六章: 解码器 第二节: 固定长度解码器 上一小节我们了解到, 解码器需要继承ByteToMessageDecoder, 并重写decode方法, 将解析出来的对象放入集合中集合, ...

  2. Spring入门学习笔记(2)——基于Java的配置

    目录 基于Java的配置 @Configuration & @Bean Annotations Example 注入Bean依赖 @Import注解 Lifecycle Callbacks(声 ...

  3. P4562 [JXOI2018]游戏

    题面 题目描述 她长大以后创业了,开了一个公司. 但是管理公司是一个很累人的活,员工们经常背着可怜偷懒,可怜需要时不时对办公室进行检查. 可怜公司有 \(n\) 个办公室,办公室编号是 \(l\) 到 ...

  4. [Paper Reading] Image Captioning using Deep Neural Architectures (arXiv: 1801.05568v1)

    Main Contributions: A brief introduction about two different methods (retrieval based method and gen ...

  5. TensorFlow中的卷积函数

    前言 最近尝试看TensorFlow中Slim模块的代码,看的比较郁闷,所以试着写点小的代码,动手验证相关的操作,以增加直观性. 卷积函数 slim模块的conv2d函数,是二维卷积接口,顺着源代码可 ...

  6. OO第一阶段作业总结

    对于OO这门课,学长学姐偶尔提起,大家都略有耳闻,但是并没有将其和计组相提并论.因此,在刚开始接触的时候,并不认为其会比计组难到哪里去,然而事实证明,还是不要想当然去判断,以及不提前学好JAVA对于O ...

  7. java第四次实验报告

    课程:Java程序与设计     班级:1352 姓 名:池彬宁  小组成员: 20135212池彬宁 20135208贺邦 学号:20135212 成绩:             指导教师:娄嘉鹏  ...

  8. Sqlite数据库初步的了解

    转载与:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0714/1438.html    来自:泡在网上的日子. 和其他数据库一 ...

  9. php----函数大全

    字符串函数 数组函数 数学函数

  10. 剑指offer:旋转数组的最小数字

    题目描述: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个 ...