Mybatis 子查询
在查询数据库时,需要以查询结果为查询条件进行关联查询。
在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 子查询的更多相关文章
- MyBatis子查询
一.父查询BaseChildResultMap: <?xml version="1.0" encoding="UTF-8" ?> <!DOCT ...
- coding++:mybatis 嵌套查询子查询column传多个参数描述
mybatis 嵌套查询子查询column传多个参数如下: 2.代码示例 备注:注意,相同颜色的单词都是有关联的 <resultMap id="blogResult" typ ...
- Mybatis 一对多延迟加载,并且子查询中与主表字段不对应 (19)
Mybatis 一对多延迟加载,并且子查询中与主表字段不对应应用说明. 实现一对多关联(懒加载),一个教研组对应多个教师,既:教师的教研编号与教研组的教研编号关联,并且教师关联教研组外键与教研组编号 ...
- mybatis中collection子查询注入参数为null
具体实现参照网上,但是可能遇到注入参数为null的情况,经过查阅及自己测试记录一下: 子查询的参数中,有<if test="">之类,需要指定别名,通过 http:// ...
- MyBatis关联查询分页
背景:单表好说,假如是MySQL的话,直接limit就行了. 对于多对多或者一对多的情况,假如分页的对象不是所有结果集,而是对一边分页,那么可以采用子查询分页,再与另外一张表关联查询,比如: sele ...
- mybatis分页查询的万能模板
分页查询项目里太多了,而这种分页查询,在mybatis里面的配置几乎一模一样,今天就整理一个比较好和实用的模板,供以后直接Ctrl+C <select id="queryMember& ...
- mybatis分页查询,SqlServer 2008 查询速度很慢
一个业务场景,需要进行union查询: 查询速度非常慢,大概要37秒: 直接复制sql在数据库客户端执行,速度很快,由此可知是mybatis的原因,在网上搜索,可以配置fetchSize=" ...
- MyBatis高级查询 一对一映射
drop database if exists simple; create database simple; use simple; drop table if exists sys_user; c ...
- MyBatis 关联查询的实现:一对多
有2个实体:用户.订单,一个用户可以拥有多个订单,同时这多个订单属于一个用户,即一对多. user_tb: order_tb: 在“多”的一方(order)添加“一”的一方(user)的主键(user ...
随机推荐
- LeetCode 611. Valid Triangle Number有效三角形的个数 (C++)
题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...
- 数据库创建,用户管理,导入dmp文件
创建数据库文件 CREATE TABLESPACE toolset LOGGING DATAFILE '/home/oracle/app/oracle/oradata/orcl/toolset.dbf ...
- CF1163E Magical Permutation(线性基,构造)
虽然做起来有一点裸……但是就是想不到啊…… 首先令 $d_i=p_i\oplus p_{i-1}$,那么 $d_i$ 都是 $S$ 中的数,$a_i=d_i\oplus d_{i-1}\oplus \ ...
- [LeetCode] 38. Count and Say 计数和读法
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- Nacos集成Spring Cloud Gateway 基础使用
项目结构 项目 端口 描述 nacos-provider 8000 服务 nacos-getway 8001 网关 nacos-provider项目依赖 <dependencies> &l ...
- Ubuntu apache安装,配置,卸载
阿里云服务器等记得到控制台开启防火墙 安装 sudo apt-get update sudo apt-get install apache2 配置 apache2 默认的几个配置文件 /etc/apa ...
- Destoon手机搜索点击提示 http 403 forbidden解决方法
以下是网上搜到的答案: 最近发现用destoon开发的手机版网站,在手机版百度搜素网站的时候,点击之后出现 http 403 forbidden的弹出窗.必须再次的刷新网页才可以打开网站.出现这个问题 ...
- 部门工资前三高的所有员工 - LeetCode
Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId . +----+-------+--------+---- ...
- IScroll自动计算高度代码
setScrollHeight: function () { var temp_height = 0; temp_height = $("#" + this.wrapperId). ...
- Sitecore 营销分类
分类法是一种分层结构,可用于识别和组织信息.在Sitecore体验平台中,您可以使用分类法对营销活动进行分类,例如广告,目标和活动.您可以在市场营销控制面板中将分类标签应用于这些项目. 营销分类的四种 ...