1. findByIds(List ids)
如果参数的类型是List, 则在使用时,collection属性要指定为 list
<select id="findByIds" resultMap="BaseResultMap">
Select
<include refid="Base_Column_List" />
from user where id in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</select>
也可以使用org.apache.ibatis.annotations.Param注解,collection对应的就可以使用别名了
findByIds(@Param("ids")List ids)
@Param相当于取了一个别名
<select id="findByIds" resultMap="BaseResultMap">
Select
<include refid="Base_Column_List" />
from user where id in
<foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
#{item}
</foreach>
</select>
2:findByIds(Integer[] ids)
如果参数的类型是Array,collection属性要可以指定为 array
<select id="findByIds" resultMap="BaseResultMap">
Select
<include refid="Base_Column_List" />
from user where id in
<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
#{item}
</foreach>
</select>
当然也可以用@Param注解在mapper接口上
findByIds(@Param("ids")Integer[] ids)
<select id="findByIds" resultMap="BaseResultMap">
Select
<include refid="Base_Column_List" />
from user where id in
<foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
#{item}
</foreach>
</select>
3.findByMap(@Param("params")Map<String, Object> params)
Map<String, Object> params = new HashMap<String, Object>(2);
params.put("name", "hha");
params.put("ids", ids); ids是数组或者list都可以 主要是后面遍历ids
<select id="findByMap" resultMap="BaseResultMap">
Select
<include refid="Base_Column_List" />
from user where id in
<foreach item="item" index="index" collection="params.ids" open="(" separator="," close=")">
#{item}
</foreach>
</select>
foreach属性主要有item,index,collection,open,separator,close。
1、item表示集合中每一个元素进行迭代时的别名,
2、index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,
3、open表示该语句以什么开始,
4、separator表示在每次进行迭代之间以什么符号作为分隔符,
5、close表示以什么结束,
6、collection属性,如果mapper接口上没有@Param("") 注解的,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的:一般有list和array