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

前言:

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

什么是一对多?

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

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

collection 标签

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

collection 属性:

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

案例

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

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

<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 条件中过滤即可。
  • 缺点:不支持分页,因为是是联合查询后进行映射,无法得到正确的分页效果,如果要分页就先对主表进行分页后再进行关联查询。

方法二,子查询映射:

<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 映射的方式的效率远高于子查询映射的方式。

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

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis中,常用的一对多关联查询方式是使用<collection>元素来映射多个结果。具体使用方法如下: 1. 在主对象的映射文件中使用<collection>元素,指定关联查询的SQL语句和对应的子对象映射文件。 ``` <!-- UserMapper.xml --> <select id="getUserWithOrders" resultMap="userWithOrdersResultMap"> select * from user where id = #{id} </select> <!-- userWithOrdersResultMap --> <resultMap id="userWithOrdersResultMap" type="User"> <id property="id" column="id" /> <result property="username" column="username" /> <collection property="orders" ofType="Order" resultMap="orderResultMap"/> </resultMap> ``` 2. 在子对象的映射文件中定义对应的映射关系,例如: ``` <!-- OrderMapper.xml --> <select id="getOrdersByUserId" resultMap="orderResultMap"> select * from order where user_id = #{userId} </select> <!-- orderResultMap --> <resultMap id="orderResultMap" type="Order"> <id property="id" column="id" /> <result property="name" column="name" /> <result property="price" column="price" /> </resultMap> ``` 上述代码中,<collection>元素将User对象和Order对象关联起来,通过在主对象映射文件中使用<collection>元素引用子对象映射文件的<resultMap>元素来定义映射关系。 需要注意的是,上述代码中的<collection>元素使用了ofType属性来指定子对象的类型,使用了resultMap属性来指定子对象映射的<resultMap>元素。同时,可以在<collection>元素中使用fetchType属性来指定关联查询的时机,例如lazy、eager等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值