0. foreach
foreach主要用来迭代集合,通常用在in条件中,要注意collection属性的用法。
1. 参数是List
getByIds(List<Long> asList)
<select id="getByIds">
select * from t_table t
where t.id in
<foreach collection="list" item="itemId" index="index" open="(" close=")" separator=",">
#{itemId}
</foreach>
</select>
2. 参数是Array
getByIds(Long[] asList)
<select id="getByIds">
select * from t_table t
where t.id in
<foreach collection="array" item="itemId" index="index" open="(" close=")" separator=",">
#{itemId}
</foreach>
</select>
3. 参数是Map,那么所有的#{}里面都应该是这个map的key
getByIds(Map<String, Object> map)
<select id="getByIds">
select * from t_table t
<where>
<if test="key1OfMap != null and key1OfMap != ''">
t.id1 = #{key1OfMap}
</if>
<if test="keyListOfMap != null and keyListOfMap.length != 0">
And t.id2 in
<foreach collection="keyListOfMap" item="itemId" index="index" open="(" close=")" separator=",">
#{itemId}
</foreach>
</if>
</where>
</select>