过年啦

collection标签多条件查询

场景:要查的数据在两个表,并且这个两个表为一对多关系。
eg:图片名称
以上为我最终要得到的数据实体,现在要开始查这些数据

思路:先查【一对多】中的【】这张表基本信息,其次查【】中你要进行多条件筛选的这些条件字段

代码:

1、查基本信息

<select id="selectByDeviceCode" resultMap="map">
select distinct d.name deviceName, d.type deviceType, d.model deviceModel, p.code planCode,m.type,p.id
from device d
left join plan p on p.device_type_id = d.type_id
left join model m on m.plan_id = p.id
where p.del_flag = 0
<if test="deviceCode!=null and deviceCode != ''">
and d.code = #{deviceCode}
</if>
<if test="type!=null and type != ''">
and m.type = #{type}
</if>
</select>
图片名称 这里的后两个条件,则是 条件字段

2、进行映射

<resultMap id="map" type="MainDPVo">
<id property="id" column="id"/>
<result property="deviceName" column="deviceName"/>
<result property="deviceType" column="deviceType"/>
<result property="deviceModel" column="deviceModel"/>
<result property="planCode" column="planCode"/>
<result property="type" column="type"/>
<collection property="modelList" column="{type=type,id=id}" javaType="java.util.ArrayList"
select="MaintPlanModelMapper.modelLists"/>
</resultMap>

本文关键也在于此 : 图片名称

column里传入了多条件,这个条件在第一张图里面,我们已经查出来了。javaType里面写了list表明你有多条件

多条件用在另外一个查询

<select id="modelLists" resultType="MaintPlanModel">
select *
from model
where del_flag = 0
<if test="type!=null and type != ''">
and type = #{type}
</if>
<if test="id!=null and id != ''">
and plan_id = #{id}
</if>
</select>

这个sql的路径在上一个代码块的这里写
图片名称

posted @   一只快乐的柠檬精J  阅读(112)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示