mybatis中子查询对象及对象集合(子查询中带多参数)
来源:SegmentFault
时间:2023-02-23 17:28:44 192浏览 收藏
数据库小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《mybatis中子查询对象及对象集合(子查询中带多参数)》带大家来了解一下mybatis中子查询对象及对象集合(子查询中带多参数),希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!
就国内来说项目中mybatis使用的较多,因为方便灵活,上手快,会写sql就能用好mybatis,另外sql优化等简单易做,遇到慢sql了比hibernate更好排查。除了一大痛点,因为项目短平快,所以开发过程中大概率会出现发现某个表要加字段要删字段要改字段等头疼的事,你以为我会说,这么麻烦不想调么,不,不存在的。
言归正传,在查询里使用子查询是比较频繁的。这文章的下半部分就是要解决子查询一个对象,一个对象列表的问题,并且子查询中的条件除了关联字段还有其他的参数需要传入。
本示例中要查询的表结构如下:
create table if not exists tb_station(
id bigint not null constraint pk_tb_station primary key,
code varchar(18),
geo_id bigint, //分组id
label_id bigint, // 标牌id
employee_id bigint, //员工id
status integer
);
需要获取的结果对象为
public class StationVO {
private Long id;//主键
private String code;//工位编号
private Long stationGeoId;//StationGeoVO,所属工位分区对象
private Long labelId;//工位绑定的标牌id
private String labelCode;//标牌code
private String eslCode;//标牌eslcode
private EmployeeVO employee;//EmployeeVO,分配的员工对象
private String status;//工位状态(0:空闲,1:正在使用)
private List<biz> bizList;//工位分组推送vo
}
biz对象说明:biz对象对应的表里有一个geo_id字段进行关联
</biz>
此处的需求是要根据分组id(即geoId)查询stationVO对象
mybatis中对应的映射及代码如下:
<resultmap id="StationVOResultMap" type="org.sit.cto.smmrepository.vo.StationVO"><id column="id" jdbctype="BIGINT" property="id"></id><result column="code" jdbctype="VARCHAR" property="code"></result><result column="status" jdbctype="INTEGER" property="status"></result><result column="label_id" jdbctype="BIGINT" property="labelId"></result><result column="esl_code1" jdbctype="VARCHAR" property="eslCode"></result><result column="lcode" jdbctype="VARCHAR" property="labelCode"></result><result column="geo_id" jdbctype="BIGINT" property="stationGeoId"></result><association property="employee" column="employee_id" select="selectEmployeeByEmployeeId"></association><collection property="bizList" column="{geoId=geo_id,eslCode=esl_code1}" oftype="map" select="selectBizVOByStationId"></collection></resultmap><resultmap id="EmployeeResultMap" type="org.sit.cto.smmrepository.model.Employee"><id column="id" jdbctype="BIGINT" property="id"></id><result column="code" jdbctype="VARCHAR" property="code"></result><result column="real_name" jdbctype="VARCHAR" property="realName"></result><result column="company" jdbctype="VARCHAR" property="company"></result><result column="dept_name" jdbctype="VARCHAR" property="deptName"></result><result column="position" jdbctype="VARCHAR" property="position"></result><result column="phone" jdbctype="VARCHAR" property="phone"></result><result column="status" jdbctype="INTEGER" property="status"></result></resultmap><resultmap id="BizVOResultMap" type="org.sit.cto.smmrepository.vo.BizVO"><result column="esl_code" jdbctype="VARCHAR" property="eslCode"></result><result column="temp_id" jdbctype="BIGINT" property="labelTempId"></result><result column="key" jdbctype="VARCHAR" property="key"></result><result column="value" jdbctype="VARCHAR" property="value"></result></resultmap><select id="selectEmployeeByEmployeeId" resultmap="EmployeeResultMap">
select
id,
code,
real_name,
company,
dept_name,
position,
phone,
status
from tb_employee
where id=#{employeeId}
</select><select id="selectBizVOByStationId" parametertype="java.util.Map" resultmap="BizVOResultMap">
select
esl_code,
temp_id,
key,
value
from tb_biz
where associated_id=#{geoId}
and esl_code=#{eslCode}
and biz_type=2
</select><select id="getStationVOListByGeoId" resultmap="StationVOResultMap">
SELECT
ts.ID,
ts.code,
ts.geo_id,
ts.label_id,
ts.employee_id,
ts.status,
tl.code lcode,
tl.esl_code1
FROM
tb_station ts
LEFT JOIN tb_label tl ON ts.label_id = tl.ID
WHERE
ts.geo_id =#{geoId}
ORDER BY
ts.code
</select>
子查询获取一个关联对象
例子中的employee
<association property="employee" column="employee_id" select="selectEmployeeByEmployeeId"></association> 子查询对象,标签使用 <association></association> property的内容对应目标对象中的属性名 column对应表中的关联字段的名字 select对应获取关联对象子查询的查询名
子查询获取一个关联对象的集合
示例中Biz对象的集合
<collection property="bizList" column="{geoId=geo_id,eslCode=esl_code1}" oftype="map" select="selectBizVOByStationId"></collection>
子查询,标签使用<collection></collection>
property的内容对应目标对象中的属性名
column对应表中的关联字段的名字
select对应获取对象子查询的查询名
ofType 此处子查询需要传入其他参数,故用map传入,若无其他参数则该标签中不需要此属性,column处直接填写关联字段名(如column="geo_id")
需要传入其他参数时,写法如上例,在对应的子查询的#{}占位符处需要与该map中的key对应
规范定义好resultmap,能让sql更有条理更美观
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于数据库的相关知识,也可关注golang学习网公众号。
声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
499 收藏
-
244 收藏
-
235 收藏
-
157 收藏
-
101 收藏
最新阅读
更多>
-
460 收藏
-
349 收藏
-
223 收藏
-
406 收藏
-
486 收藏
-
294 收藏
-
117 收藏
-
411 收藏
-
420 收藏
-
264 收藏
-
266 收藏
-
392 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习