1. 當查詢的引數只有一個時
findByIds(List<Long> ids)
1.a 如果引數的型別是List, 則在使用時,collection屬性要必須指定為 list
<select id="findByIdsMap" resultMap="BaseResultMap">
Select
<include refid="Base_Column_List" />
from jria where ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
1.b 如果引數的型別是Array,則在使用時,collection屬性要必須指定為 array
<select id="findByIdsMap" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from jria where ID in
<foreach item="item" index="index" collection="array"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
2. 當查詢的引數有多個時,例如 findByIds(String name, Long[] ids)
這種情況需要特別注意,在傳引數時,一定要改用Map方式, 這樣在collection屬性可以指定名稱
下面是一個示例
Map<String, Object> params = new HashMap<String, Object>(2);
params.put("name", name);
params.put("ids", ids);
mapper.findByIdsMap(params);
<select id="findByIdsMap" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from jria where ID in
<foreach item="item" index="index" collection="ids"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
完整的示例如下:
例如有一個查詢功能,Mapper介面檔案定義如下方法:
List<Jria> findByIds(Long... ids);
使用 in 查詢的sql拼裝方法如下:
<select id="findbyIds" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from jria where ID in
<foreach item="item" index="index" collection="array"
open="(" separator="," close=")">
#{item}
</foreach>
</select>