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

<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>即可。


Original url: Access
Created at: 2020-03-12 20:20:47
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论