MyBatis 一对多查询(联合查询 ResultMap 映射 和 子查询映射)

前言:

在数据库操作中,我们可能习惯了单表、连表操作,突然然你来进行一对多操作,你还会吗?

什么是一对多?

一对多是最基础的表关系,简单来说就是 A 表中的一条数据对应 B 表中的多条数据,常见场景如下:

  • 部门和人员关系,一个部门对应多个人员。
  • 老师和学生关系,一个老师对应多个学生。
  • 学生和课程成绩关系,一个学生对应多门课程成绩。
  • 。。。。。。

collection 标签

Mybatis 提供了 collection 标签帮助我们简化了一对多关系的查询,collection 标签一般使用于 resultMap 标签中。

collection 属性:

  • property:属性名称。
  • ofType:属性类型。
  • select:关联查询的方法。
  • column:关联字段。

案例

本文以部门和部门下人员作为演示案例,分别有部门表 dept_info 和人员表 employee_info,使用一对多查询部门下的人员。

方法一,联合查询 ResultMap 映射:

java 复制代码
<resultMap id="treeMap" type="com.zt.dc.portal.admin.web.pojo.dto.bulletinboard.DepartmentPersonnelTreeVO">
	<result property="deptName" column="dept_name"/>
	<result property="deptId" column="dept_id"/>
	<result property="parentId" column="parent_id"/>
	<collection property="deptEmployee" ofType="com.zt.dc.portal.admin.web.pojo.entity.EmployeeInfo">
		<result property="name" column="name"/>
		<result property="jobNumber" column="job_number"/>
	</collection>
</resultMap>

<select id="queryDepartmentPersonnel" resultMap="treeMap">
	select di.dept_name ,di.dept_id ,di.parent_id ,ei.name ,ei.job_number
	from dept_info di left join employee_info ei  on di.dept_id =ei.dept_id and ei.emp_type = 1
	where di.dept_type = 1
</select>
  • 原理:sql 直接关联查询,结果集通过 resultMap 的 collection 标签完成一对多的映射。
  • 优点:条件过滤灵活,如果需要进行条件过滤,直接在 where 条件中过滤即可。
  • 缺点:不支持分页,因为是是联合查询后进行映射,无法得到正确的分页效果,如果要分页就先对主表进行分页后再进行关联查询。

方法二,子查询映射:

java 复制代码
<resultMap id="treeMap2" type="com.zt.dc.portal.admin.web.pojo.dto.bulletinboard.DepartmentPersonnelTreeVO">
	<result property="deptName" column="dept_name"/>
	<result property="deptId" column="dept_id"/>
	<result property="parentId" column="parent_id"/>
	<collection property="deptEmployee" select="queryEmployeeInfo" column="dept_id"/>
</resultMap>

<select id="queryDepartmentPersonnel" resultMap="treeMap2">
	select di.dept_name ,di.dept_id ,di.parent_id
	from dept_info di
</select>

<select id="queryEmployeeInfo" resultType="com.zt.dc.portal.admin.web.pojo.entity.EmployeeInfo">
	select ei.name ,ei.job_number from employee_info ei where ei.dept_id=#{deptId}
</select>
  • 原理:通过 collection 标签的 select 属性去调用子查询,查询条件通过 column 属性传递。
  • 优点:普通查询和分页查询都能够支持,主表条件过滤灵活,直接在 where 条件中过滤即可。
  • 缺点:子查询条件过滤比较困难,column 属性只能传递主表已有的字段,多个字段用 column="{prop1=col1,prop2=col2}" 传递。

使用推荐:

建议使用联合查询 ResultMap 映射的方式实现一对多,做了一个小小的测试,两种方式的效率对比,联合查询 ResultMap 映射的方式的效率远高于子查询映射的方式。

欢迎提出建议及对错误的地方指出纠正。

相关推荐
瓦特what?8 分钟前
关于C++的#include的超超超详细讲解
java·开发语言·数据结构·c++·算法·信息可视化·数据挖掘
silver988633 分钟前
sql链接的url中serverTimezone的作用
数据库·sql
是乐谷1 小时前
阿里云杭州 AI 产品法务岗位信息分享(2025 年 8 月)
java·人工智能·阿里云·面试·职场和发展·机器人·云计算
Java水解1 小时前
Java中的四种引用类型详解:强引用、软引用、弱引用和虚引用
java·后端
tanxiaomi1 小时前
数据库索引视角:对比二叉树到红黑树再到B树
数据结构·数据库·b树
lifallen1 小时前
JCTools 无锁并发队列基础:ConcurrentCircularArrayQueue
java·开发语言·数据结构·算法
一叶飘零_sweeeet1 小时前
IDEA 插件 Trae AI 全攻略
java·人工智能·intellij-idea
水无痕simon1 小时前
5 索引的操作
数据库·elasticsearch
欧哈东哥2 小时前
【C++】标准库中用于组合多个值的数据结构pair、tuple、array...
java·数据结构·c++
python_1362 小时前
web请求和响应
java·spring·github