Mybatis中级联有关联(association)、集合(collection)、鉴别器(discriminator)三种。其中,association对应一对一关系、collection对应一对多关系、discriminator可以根据情况选择采用哪个类作为实例,关联不同的结果集。

一、关联

Mybatis的关联有两种方式:

  • 嵌套查询:通过执行另外一个 sql映射语句来返回预期的复杂类型
  • 嵌套结果:使用嵌套结果映射来处理重复了联合结果的子集

场景一:一个部门Department有一个部门经理Leader

<!--    嵌套查询 (N+1查询问题)    -->
<resultMap type="Department" id="deptResult">
  <id property="id" column="d_id"/>
  <result property="name" column="d_name"/>
  <association property="leader" column="leader_id" select="getLeader" javaType="Leader"/>
</resultMap>
<select id="getDept" parameterType="int" resultMap="deptResult">
  select * from department where d_id=#{id}
</select> <select id="getLeader" parameterType="int" resultType="Leader">
  select * from leader where l_id=#{id}
</select> <!-- 嵌套结果 -->
<resultMap type="task.Department" id="deptResult">
  <id property="id" column="d_id"/>
  <result property="name" column="d_name"/>
  <association property="leader" javaType="Leader">
    <id property="id" column="l_id"/>
    <result property="name" column="l_name"/>
  </association>
</resultMap>
<select id="getDept" parameterType="int" resultMap="deptResult">
  select * from department d, leader l where d.leader_id = l.l_id and d.d_id=#{id}
</select>

场景二:获取指定部门的所有上级部门(获取子结点的所有父结点,一个结点最多只有一个直接父结点)

<resultMap type="Department" id="deptResult">
  <id column='id' property='id'/>
  <result column='name' property='name'/>
  <association property='parent' javaType='Department' select='getPDept' column='p_id'/>
</resultMap>
<select id="getDept" resultMap="deptResult">
  select id,name,p_id from department where id =#{id}
</select>

二、集合

和关联一样,集合也有嵌套查询和嵌套结果两种方式,只是多了个关键字ofType,用来区分JavaBean属性类型和集合包含的类型。

场景:一个部门Department有多个员工Worker

<!--    嵌套查询    -->
<resultMap type="Department" id="deptResult">
<id property="id" column="id"/>
<result property="name" column="d_name"/>
<!-- javaType属性不是必需的 -->
<collection property="workers" javaType="ArrayList" column="id" ofType="Worker" select="getWorkers"/>
</resultMap>
<resultMap type="Worker" id="workerResult" >
<id property="id" column="w_id"/>
<result property="name" column="w_name"/>
<result property="age" column="age"/>
</resultMap>
<select id="getDept" parameterType="int" resultMap="deptResult">
select id,d_name from department where id=#{id}
</select>
<select id="getWorkers" parameterType="int" resultMap="workerResult">
select w_id,w_name,age from worker where d_id=#{id}
</select> <!-- 嵌套结果 -->
<resultMap type="Department" id="deptResult">
<id property="id" column="id"/>
<result property="name" column="d_name"/>
<collection property="workers" ofType="Worker">
<id property="id" column="w_id"/>
<result property="name" column="w_name"/>
<result property="age" column="age"/>
</collection>
</resultMap>
<select id="getDept" parameterType="int" resultMap="deptResult">
select id,d_name,w_id,w_name,age from department,worker where id=d_id and id=#{id}
</select>

三、鉴别器

有时一个数据库查询语句会返回很多不同数据类型的结果集。鉴别器用于处理这种情况,还包括类的继承层次结构,其表现相当于Java中的switch语句。

场景:交通工具Vehicle,有子类小汽车Car,卡车Truck两种类型,根据vehicle表中type字段区别数据,将查询出的数据自动封装成不同类型的对象

<resultMap type="Vehicle" id="vehicleResult">
<id property="id" column="id"/>
<result property="color" column="color"/>
<discriminator javaType="int" column="type">
<case value="1" resultType="Car">
<result property="doorCount" column="door_count"></result>
</case>
<case value="2" resultType="Truck">
<result property="boxSize1" column="box_size1"></result>
<result property="boxSize2" column="box_size2"></result>
</case>
</discriminator>
</resultMap>
<select id="getVehicle" resultMap="vehicleResult">
select * from vehicle
</select>

Mybatis级联:关联、集合和鉴别器的使用的更多相关文章

  1. mybatis映射文件select_resultMap_关联查询_collection定义关联集合

    知识点:查询一个实体类,并查出这个类下面的集合 Employee.java实体类 package com.hand.mybatis.bean;public class Employee {    pr ...

  2. mybatis 级联

    级联是一个数据库实体的概念.一对多的级联,一对多的级联,在MyBatis中还有一种被称为鉴别器的级联,它是一种可以选择具体实现类的级联. 级联不是必须的,级联的好处是获取关联数据十分便捷,但是级联过多 ...

  3. 04—mybatis的关联映射

    mybatis的关联映射一对一一对多多对多 一.一对一(一个人只能有一个身份证号) 1.创建表创建表tb_card CREATE TABLE `tb_card` ( `id` int(11) NOT ...

  4. MyBatis实现关联表查询

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

  5. Mybatis之关联查询

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

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

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

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

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

  8. Mybatis表关联多对一

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

  9. MyBatis.4关联

    关联.多对一关联查询 package org.mybatis.example.dao; import java.util.Date; //雇员类 public class Emp { private ...

随机推荐

  1. centos7镜像文件

    1.打开网址https://www.centos.org/download/2.点击Minimall ISO 按钮  3.选择aliyun地址 4.centos版本介绍 在CentOS官方网站上,Ce ...

  2. Python3 多线程例子

    import threading, zipfile class AsyncZip(threading.Thread): def __init__(self, infile, outfile): thr ...

  3. IMPALA部署和架构(一)

    IMPALA部署和架构(一)  一,概要 因公司业务需求,需要一个查询引擎满足快速查询TB级别的数据,所以我们找到了presto和impala,presto在前面讲过今天只说impala,impala ...

  4. GitHub Pages:静态站点托管服务(待补充)

    不管是 react 还是 vue 项目路由都必须使用 hash 方式,否则页面打不开,切记!!! 如果使用的是 vue-cli 3 创建项目,那么你要在根目录创建 vue.config.js 文件,并 ...

  5. 鏈接Redis報錯`AUTH` failed: ERR Client sent AUTH, but no password is set [tcp://127.0.0.1:6379]

    問題 鏈接Redis報錯`AUTH` failed: ERR Client sent AUTH, but no password is set [tcp://127.0.0.1:6379] 解決 啟動 ...

  6. 漫画赏析:Linux 内核到底长啥样(转)

    知乎链接:https://zhuanlan.zhihu.com/p/51679405 来自 http://TurnOff.us 的漫画 “InSide The Linux Kernel” 本文转载自: ...

  7. USD在CentOS7.0操作系统下的安装方法

    最近Pixar的开源USD软件很火,官方在Introduce中明确讲到这个软件的设计开发目标是增强艺术家协作,减少不确定因素,最大化资产版本迭代效率,追求更大的承载能力. 当今行业中传统的线性的制作方 ...

  8. 由consequence忽然发现英语也挺有意思

    con- 是拉丁语前缀, 有 with, together 的意思. con- 和 com- 一样的. 只是因为 在 b p m 前发 m 音更方便, 所以这些音前的 con- 变为 com- (例 ...

  9. RN 时间戳

    let curTime = Date.now(); 获取到当前时间 curTime: 1555120690696 是指从1970.1.1到现在的毫秒(ms)数 cxk() { //之前时间 let p ...

  10. 工控随笔_09_西门子_S7-200 Smart与V20 USS通信USS_RPM_R利用轮询的方式通讯异常

    前两天处理过一个故障,是S7-200 Smart与V20的USS通讯,设备厂家在程序里面利 用USS_RPM _R程序循环轮询5个V20设备读取频率和电流值等信息. 图 USS_RPM_R读取信息 上 ...