当查询的参数为一个时
findIds(List<Long> ids)
当参数类型为List或Array时,查询语句collection必须为list或array。
select * from student where id in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
select * from student where id in
<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
#{item}
</foreach>
属性描述
collection: 表示迭代集合的名称,可以使用@Param注解指定,如下图所示 该参数为必选
item: 表示本次迭代获取的元素,若collection为List、Set或者数组,则表示其中的元素;若collection为map:则代表key-value的value,该参数为必选
open:表示该语句以什么开始,最常用的是左括弧’(’,注意:mybatis会将该字符拼接到整体的sql语句之前,并且只拼接一次,该参数为可选项
close:表示该语句以什么结束,最常用的是右括弧’)’,注意:mybatis会将该字符拼接到整体的sql语 句之后,该参数为可选项
separator: mybatis会在每次迭代后给sql语句append上separator属性指定的字符,该参数为可选项
index: 在list、Set和数组中,index表示当前迭代的位置,在map中,index代指是元素的key,该参数是可选项。
当参数为多个时
findIds(List<Long>ids,String name)
可以在业务逻辑层使用Map,以Map的形式传参,这样可以指定collection属性名称
Map<String,Object> params = new HashMap<String,Object>(2);
params.put("communityId",activiteManage.getCommunityId());
params.put("buildingIds",buildingIds)
mapper.selectBuildingListById(params)
mapper层
List<ActiviteManage> selectBuildingListById(Map<String, Object> params);
mapper.xml
<select id="selectBuildingListById" resultType="com.wr.remote.estate.activity.ActiviteManage" parameterType="list" resultMap="ActiviteManageResult">
SELECT
building_id,
community_id,
building_name
FROM
xccj_community.bus_building
WHERE
del_flag = 0
AND community_id = #{communityId} and building_id in
<foreach item="item" index="index" collection="buildingIds"
open="(" separator="," close=")">
#{item}
</foreach>
</select>