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的路径在上一个代码块的这里写
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~