mybatis的配置檔案中使用兩個或多個foreach進行多個集合遍歷的問題
阿新 • • 發佈:2018-12-16
<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>即可。