温故而知新!
List
Java代码
List<Person> selectByNames(@Param("names") List<String> names);
mapper
<select id="selectByNames" resultType="com.panda.mpt.pojo.Person">
select name,age from person where name in
<foreach collection="names" separator="," open="(" close=")" index="index" item="item">
#{names}
</foreach>
</select>
item:当前遍历的元素值。index:从0开始的序号。
Map
<foreach>不但支持list,也支持map
Java代码
List<Person> selectByNames2(@Param("maps") Map names);
mapper
<select id="selectByNames2" resultType="com.panda.mpt.pojo.Person">
select * from person where
<foreach collection="maps.entrySet()" separator="or" index="index" item="item">
( name = #{index} and age = ${item} )
</foreach>
</select>
index:entry的key,item:entry的value
separator:每次遍历后插入sql后的值
open: foreach开始是插入sql的语句
close:foreach结束后插入sql的语句