mybatis的配置文件中使用两个或多个foreach进行多个集合遍历的问题

<select id="selectTrafficEventIngByType" resultMap="BaseResultMap">
		select 
		<include refid="Base_Column_List"/>
		from T_TRAFFIC_EVENT
		where to_char(EVENT_TIME,'dd')=to_char(sysdate,'dd')
		and ROWNUM <![CDATA[ <= ]]> 100 
    	<if test="eventType!=null and eventType!='' ">
	    	and EVENT_TYPE in
	        <foreach collection="eventType" index="index" item="item" open="(" separator="," close=")"> 
	        	#{item} 
	    	</foreach>
    	</if>
    	<if test="eventLevel!=null and eventLevel!='' ">
	    	and EVENT_LEVEL in
	        <foreach collection="eventLevel" index="index" item="item" open="(" separator="," close=")"> 
	        	#{item} 
	    	</foreach>
    	</if>
		order by EVENT_TIME desc
	</select>

从上面可以看到,where条件后需要对两个集合进行遍历,解决办法就是把这两个集合放入map中,foreach中的collection分别对应参数map中的key即可。
如下controller层代码:

Map map = new HashMap<>();
map.put("eventLevel", listLevel);
map.put("eventType", listType);
List<TrafficEventModel> events =   trafficeEventServer.selectTrafficEventIngByType(map);

其中service和dao层参数类型写成Map<String,Object>即可。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值