在查询数据库时,需要以查询结果为查询条件进行关联查询。

在mybatis中通过association标签和collection标签实现子查询。

1. collection(集合)和association(关联)的区别

collection用于一对多关系, association用于一对一和多对一

实例代码:

public class User{

private Card card_one;	//一对一,映射时使用association

private List<Card> card_many;	//一对多,映射时使用collection

}

2. 标签属性

property: 集合属性的名称,如User的card_one和card_many

ofType: 集合中元素的类型,如Card(谨慎起见,应带上包名)

select: 子查询的ID

column: 传给子查询的参数

javaType: 一般为ArrayList

示例:

<collection property="实体类属性名"
ofType="包名.实体类名"
column="{传入参数名1 = 对应的数据表名称, ...}"
select="子查询ID"
javaType="java.util.ArrayList" />

3.传入参数注意事项

子查询的参数中:

  • <if test="">时,需要指定别名,如:column="{projectId=project_id}"
  • 没有<if test="">时,有时不能有别名,否则会出现注入参数为空,如: column="project_id"

4.代码示例

mybatis实现部门树结构查询,子查询使用和父查询一样的resultMap,递归查询子部门

组织结构

<resultMap id="departmentTreeMap" type="com.cdqd.app.entity.DepartmentEntity">
<id column="department_id" property="departmentId" jdbcType="INTEGER" />
<result column="department_name" property="departmentName" jdbcType="VARCHAR" />
<result column="department_level" property="departmentLevel" jdbcType="INTEGER" />
<result column="parent_id" property="parentId" jdbcType="INTEGER" />
<result column="leader_id" property="leaderId" jdbcType="INTEGER" />
<result column="department_status" property="departmentStatus" jdbcType="INTEGER" />
<result column="department_remark" property="departmentRemark" jdbcType="VARCHAR" />
<result column="nick_name" property="leaderName" jdbcType="VARCHAR" />
<result column="user_name" property="leaderLoginName" jdbcType="VARCHAR" />
<result column="user_tel" property="leaderTel" jdbcType="VARCHAR" />
<collection property="children"
ofType="com.cdqd.app.entity.DepartmentEntity"
column="{departmentId = department_id}"
select="selectWithLeader"
javaType="java.util.ArrayList" />
</resultMap>

第一级部门

<select id="selectWithChildren" resultMap="departmentTreeMap" parameterType="java.util.HashMap">
select
d.*,
u.nick_name,
u.user_name,
u.user_tel
from department d
left join user_info u on d.leader_id = u.user_id
<where>
d.department_status != 2
<!--department_level = 0时为公司,不显示,从公司直属部门开始查询-->
<if test="startDepartmentId == null">
and d.department_level = 1
</if>
<if test="startDepartmentId != null">
and d.department_id = #{startDepartmentId, jdbcType = INTEGER}
</if>
</where>
</select>

子部门查询

<select id="selectWithLeader" resultMap="departmentTreeMap">
select
d.*,
u.nick_name,
u.user_name,
u.user_tel
from department d
left join user_info u on d.leader_id = u.user_id
<where>
d.department_status != 2
<if test="departmentId != null">
and d.parent_id = #{departmentId}
</if>
</where>
</select>

Mybatis 子查询的更多相关文章

  1. MyBatis子查询

    一.父查询BaseChildResultMap: <?xml version="1.0" encoding="UTF-8" ?> <!DOCT ...

  2. coding++:mybatis 嵌套查询子查询column传多个参数描述

    mybatis 嵌套查询子查询column传多个参数如下: 2.代码示例 备注:注意,相同颜色的单词都是有关联的 <resultMap id="blogResult" typ ...

  3. Mybatis 一对多延迟加载,并且子查询中与主表字段不对应 (19)

    Mybatis  一对多延迟加载,并且子查询中与主表字段不对应应用说明. 实现一对多关联(懒加载),一个教研组对应多个教师,既:教师的教研编号与教研组的教研编号关联,并且教师关联教研组外键与教研组编号 ...

  4. mybatis中collection子查询注入参数为null

    具体实现参照网上,但是可能遇到注入参数为null的情况,经过查阅及自己测试记录一下: 子查询的参数中,有<if test="">之类,需要指定别名,通过 http:// ...

  5. MyBatis关联查询分页

    背景:单表好说,假如是MySQL的话,直接limit就行了. 对于多对多或者一对多的情况,假如分页的对象不是所有结果集,而是对一边分页,那么可以采用子查询分页,再与另外一张表关联查询,比如: sele ...

  6. mybatis分页查询的万能模板

    分页查询项目里太多了,而这种分页查询,在mybatis里面的配置几乎一模一样,今天就整理一个比较好和实用的模板,供以后直接Ctrl+C <select id="queryMember& ...

  7. mybatis分页查询,SqlServer 2008 查询速度很慢

    一个业务场景,需要进行union查询: 查询速度非常慢,大概要37秒: 直接复制sql在数据库客户端执行,速度很快,由此可知是mybatis的原因,在网上搜索,可以配置fetchSize=" ...

  8. MyBatis高级查询 一对一映射

    drop database if exists simple; create database simple; use simple; drop table if exists sys_user; c ...

  9. MyBatis 关联查询的实现:一对多

    有2个实体:用户.订单,一个用户可以拥有多个订单,同时这多个订单属于一个用户,即一对多. user_tb: order_tb: 在“多”的一方(order)添加“一”的一方(user)的主键(user ...

随机推荐

  1. zz2017-2018年AI技术前沿进展与趋势

    2017年AI技术前沿进展与趋势 人工智能最近三年发展得如火如荼,学术界.工业界.投资界各方一起发力,硬件.算法与数据共同发展,不仅仅是大型互联网公司,包括大量创业公司以及传统行业的公司都开始涉足人工 ...

  2. 关于matlab tfdata的用法

      加上'v',可以让输出的值由元胞数组改为数组直接输出:举个例子:h = tf([1 1],[1 2 5]);[num,den] = tfdata(h)可以看出输出的num和den为元胞数组的形式无 ...

  3. .NET中线程同步的几种方法

    lock.Monitor: lock是Monitor的语法糖 [MethodImpl(MethodImplOptions.Synchronized)]: 特性标记一个方法是需要被同步的,最终实现的效果 ...

  4. maven warnning 'build.plugins.plugin.version' is missing

    裝完maven后,package或clean时出错:[WARN] [WARN] Some problems were encountered while building the effective ...

  5. set去重应用

    1.其中涉及__hash__与__eq__这两个内置方法. 2.列如: 要求用类生成多个对象,其中姓名和性别相同的对象可认为是同一个人,用set原理做去重 class People: def __in ...

  6. [LeetCode] 465. Optimal Account Balancing 最优账户平衡

    A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...

  7. 第04组 团队Git现场编程实战

    组员职责分工 组员 分工 林涛(组长) 分配任务.整理数据.写博客 童圣滔 UI界面制作 林红莲 UI界面制作 潘雨佳 测评出福州最受欢迎的商圈 于瀚翔 测评出福州最受欢迎的商圈 覃鸿浩 测评出福州人 ...

  8. QSS文件美化界面无效

    问题描述: 同样的qss内容,内置有效,写进qss文件加载无效. 搜寻.........很多网友的经理是qss文件编码和qt文本编辑器的属性........而我怎么尝试,怎么失败. 解决方案: 从fi ...

  9. ASP.NET Core基于微软微服务eShopOnContainer事件总线EventBus的实现

    这个EventBus的实现是基于微软微服务https://github.com/dotnet-architecture/eShopOnContainers项目的,我把它从项目中抽离出来,打包成nuge ...

  10. LInux 就该这么学 笔记分享

    看了Linux就该这么学的前部分书,觉得写的还可以,就在网上找了下面这个同学写的笔记,觉得很详细,所以保存地址,供以后查阅参看.这里对作者表示感谢!!! 博客地址: https://www.cnblo ...