查询Emp的同时,查出emp对应的部门Department

方法1:联合查询,使用级联属性封装结果集

<!--

联合查询,使用级联属性封装结果集

type:要自定义规则的javaBean类型

id:唯一标识,方便引用

column:指的是数据库表中的列名

property:列名对应的javaBean中的属性名称

-->

<resultMap type="cn.bdqn.mybatis.been.Emp" id="myDifemp">

<id column="id" property="id" />

<result column="last_name" property="last_name" />

<result column="gender" property="gender" />

<result column="did_id" property="dept.id" />

<result column="dept_name" property="dept.departmentName" />

</resultMap>

<!--public Emp getEmpAndDept(Integer id); -->

<select id="getEmpAndDept" resultMap="myDifEmp2">

SELECT e.id AS id,e.last_name AS last_name,e.gender AS gender,e.email AS

email,d.id AS d_id,d.`dept_name` AS dept_name

FROM emp e,dept d WHERE e.`d_id`=d.id and e.id=#{id}

</select>

方法2:association来实现多表查询

<!--

association来实现多表查询

association标签的property属性来指定javaBean中的哪个属性是我们自定义的(即:哪个属性是联合的对象),

javaType是用来指定这个属性的类型(不能省略)

association标签中继续使用id和result标签来封装相应的封装规则

-->

<resultMap type="cn.bdqn.mybatis.been.Emp" id="myDifEmp2">

<id column="id" property="id" />

<result column="last_name" property="last_name" />

<result column="gender" property="gender" />

<result column="email" property="email"/>

<association property="dept"  javaType="cn.bdqn.mybatis.been.Department">

<id column="did_id"  property="id"/>

<result column="dept_name" property="departmentName"/>

</association>

</resultMap>

方法3:association进行分步式多表查询

01.先按照员工id查询员工信息

02.根据查出员工信息中的d_id值去部门表查询部门信息

03.把查出的部门设置到员工中;

创建Department的javaBean,编写对应的映射文件,

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

<!--namespace与接口进行绑定 -->

<mapper namespace="cn.bdqn.mybatis.dao.DeptMapper">

<!--public Department getDepartmentById(Integer id); -->

<select id= "getDepartmentById" resultType="cn.bdqn.mybatis.been.Department">

<!--我们数据表中部门名称的字段的名字是dept_name 而我们javaBean中对应的 名字是departmentName,不一致,所以我们要将查询的列一一写出,并且将名称不相同的,起别名 -->

select id,dept_name as departmentName from dept where id=#{id}

</select>

</mapper>

编写emp对应的映射文件

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

<!--namespace:名称空间 -->

<mapper namespace="cn.bdqn.mybatis.dao.EmpMapperPlus">

<!-- association来实现多表查询 association标签的property属性来指定javaBean中的哪个属性是我们自定义的(即:哪个属性是联合的对象),

javaType是用来指定这个属性的类型(不能省略) association标签中继续使用id和result标签来封装相应的封装规则 -->

<resultMap type="cn.bdqn.mybatis.been.Emp" id="myDifEmp2">

<id column="id" property="id" />

<result column="last_name" property="last_name" />

<result column="gender" property="gender" />

<result column="email" property="email" />

<association property="dept" javaType="cn.bdqn.mybatis.been.Department">

<id column="did_id" property="id" />

<result column="dept_name" property="departmentName" />

</association>

</resultMap>

<!--

使用association进行分步式查询

1.先按照员工id查询员工信息

2.根据查出员工信息中的d_id值去部门表查询部门信息

3.把查出的部门设置到员工中;

-->

<resultMap type="cn.bdqn.mybatis.been.Emp" id="myEmpBy">

<id column="id" property="id" />

<result column="last_name" property="last_name" />

<result column="gender" property="gender" />

<result column="email" property="email" />

<!--association定义关联对象的封装规则 select:select的属性值是部门的映射文件的名称空间+对应的方法名称 表明当前属性是调用select指定的方法查出来的结果

column:指定将哪一列的值传给这个方法 流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性 -->

<association property="dept"

select="cn.bdqn.mybatis.dao.DeptMapper.getDepartmentById" column="d_id">

</association>

</resultMap>

<!-- public Emp getEmpByIdStep(Integer id); -->

<select id="getEmpByIdStep" resultMap="myEmpBy">

select * from emp where

id=#{id}

</select>

</mapper>

注意:这两个映射文件写好后,必须都要在全局配置文件mybatis-config.xml中进行注册

<mappers>

<mapper resource="EmpMapperPlus.xml" />

<mapper resource="DepartmentMapper.xml"/>

</mappers>

分步查询,可以使用延迟加载(懒加载/按需加载)

emp==>Dept

使用联合查询和其他复杂查询时,emp的信息和Dept的信息都会同时查出,这样很浪费资源

而我们希望部门信息在我们使用的时候再去查询,这样就很节省数据库了

只需要在分段查询的基础之上加上两个配置:

只需要在全局配置文件中开启懒加载模式

<setting name="lazyLoadingEnabled" value="true"/>

和按需加载

<setting name="aggressiveLazyLoading" value="false"/>

便可以实现

MyBatis联合查询和使用association 进行分步式查询的更多相关文章

  1. MyBatis(10)使用association进行分步查询

    (1)接口中编写方法 public Emp getEmpByStep(Integer id); public Dept getDeptById(Integer id); (2)Mapper文件 < ...

  2. 大数据量查询容易OOM?试试MySQL流式查询

    一.前言 程序访问 MySQL 数据库时,当查询出来的数据量特别大时,数据库驱动把加载到的数据全部加载到内存里,就有可能会导致内存溢出(OOM). 其实在 MySQL 数据库中提供了流式查询,允许把符 ...

  3. OpenHarmony 3.1 Beta版本关键特性解析——探秘隐式查询

    ​(以下内容来自开发者分享,不代表 OpenHarmony 项目群工作委员会观点)​ 徐浩 隐式查询是 OpenAtom OpenHarmony(以下简称"OpenHarmony" ...

  4. MyBatis联合查询association使用

    1.需求 两张表 channels(频道表)  member(会员表) 频道表里面有会员id,查询频道列表的时候需要关联查询出会员的名称,头像等信息 . 2.channels.xml定义,配置主要在这 ...

  5. MyBatis从入门到精通(十):使用association标签实现嵌套查询

    最近在读刘增辉老师所著的<MyBatis从入门到精通>一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不胜荣幸! 本篇博客主要讲解使用associati ...

  6. mybatis联合查询

    1.有学生实体 @Component @Scope("prototype") public class StudentInfo { private Integer studentI ...

  7. Mybatis 高级结果映射 ResultMap Association Collection

    在阅读本文章时,先说几个mybatis中容易混淆的地方: 1. mybatis中的列不是数据库里的列而是查询里的列,可以是别名(如 select user_name as userName,这时col ...

  8. Mybatis中的collection、association来处理结果映射

    前不久的项目时间紧张,为了尽快完成原型开发,写了一段效率相当低的代码. 最近几天闲下来,主动把之前的代码优化了一下:)   标签:Java.Mybatis.MySQL 概况:本地系统从另外一个系统得到 ...

  9. MyBatis学习总结(5)——实现关联表查询

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

随机推荐

  1. 二叉搜索树(BST)

    (第一段日常扯蛋,大家不要看)这几天就要回家了,osgearth暂时也不想弄了,毕竟不是几天就能弄出来的,所以打算过完年回来再弄.这几天闲着也是闲着,就掏出了之前买的算法导论看了看,把二叉搜索树实现了 ...

  2. BZOJ3625 [Codeforces Round #250]小朋友和二叉树(生成函数+多项式开根)

    设f(n)为权值为n的神犇二叉树个数.考虑如何递推求这个东西. 套路地枚举根节点的左右子树.则f(n)=Σf(i)f(n-i-cj),cj即根的权值.卷积的形式,cj也可以通过卷上一个多项式枚举.可以 ...

  3. Android 访问 Webapi 更新UI

    首先,写一个访问webapi的工具类 import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import or ...

  4. Codeforces965E Short Code 【启发式合并】【堆】

    题目大意: 给出总长度不超过1E+5的不重复字符串集,给每个字符串选一个前缀使得可以区分它. 题目分析: KAN出的DIV2难度一般不高,想升Ranting的可以试试. 简单的树上启发式合并,建出Tr ...

  5. Codeforces Round #337 (Div. 2) B. Vika and Squares

    B. Vika and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. day11 匿名函数

    格式 lambda 形参 :逻辑运算方式 lambda x:x+1 普通的方式计算 卧槽.这么长! def calc(x): return x+1 res = calc(10) print(res) ...

  7. ecplise debug 无法命中断点 一直在加载中

    发生原因:可能是特殊关闭了Ecplise 导致 1.这个是没问题的,网上大部分都说这个问题 2.删除所有断点再来(试了无效) 3.删除  X:\workspace\.metadata\.plugins ...

  8. 自学Zabbix12.4 Zabbix命令-zabbix_sender

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix12.4 Zabbix命令-zabbix_sender 1. zabbix_sen ...

  9. [luogu2149][bzoj1880][SDOI2009]Elaxia的路线【拓扑排序+最短路+DP】

    题目描述 最近,Elaxia和w的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起的时间. Elaxia和w每天都要奔波于宿舍和实验室之间,他们 希望在节约时间 ...

  10. yiming

      关于双城 专家团队 总裁专栏 双城荣誉 诚聘英才 双城著作移民服务 移民专题 移民专刊 移民百科 成功案例 联系我们   澳大利亚 匈牙利 美国 加拿大 欧洲 瓦努阿图 双城集团 双城地产 君益诚 ...