Mybatis中<collection>标签实现多参数查询
发现问题
需求: 在mybatis使用<collection>标签一对多嵌套查询时, 需要传多个参数查询
解决方案
只需要使用<collection>标签中的column属性通过规定格式即可实现, 如下所示
column属性解析
column中多个参数需要使用{}包裹, 且多个参数使用逗号隔开, 单个参数等号左边为子查询中参数名, 如上所示分别为deviceId和routeId, 等号右边为字段名, 也就是<result>标签中的column属性值
<resultMap type="com.lnsoft.sdhxgk.point.inspect.domain.InspectDevice" id="InspectDeviceMap">
<result property="id" column="id" jdbcType="VARCHAR"/>
<result property="deviceId" column="device_id" jdbcType="VARCHAR"/>
<result property="routeId" column="route_id" jdbcType="VARCHAR"/>
<collection property="projects" select="selectProjectsByDeviceId" ofType="com.lnsoft.sdhxgk.point.inspect.domain.DlpInspectProject"
column="{deviceId=device_id,routeId=route_id}" foreignColumn="device_id"></collection>
</resultMap>
<select id="selectProjectsByDeviceId" resultMap="ProjectMap">
select
pro.id, pro.code, pro.name, pro.content, pro.type, pro.result_option, pro.upper, pro.lower,
pro.field_name, pro.status, pro.remark, pro.create_user, pro.create_time, pro.update_user, pro.update_time
from project pro
where
1 = 1
<if test="deviceId != null and deviceId != ''">
and pro.dev_id = #{deviceId}
</if>
<if test="routeId!= null and routeId!= ''">
and pro.rou_id= #{routeId}
</if>
</select>