使用List对sql in进行传参时
UserFenceMapper.java
public List<UserFence> selectUserFenceByUIds(List uids);
UserFenceMapper.xml
<resultMap type="UserFence" id="UserFenceResult">
<result property="id" column="id" />
<result property="userId" column="user_id" />
<result property="type" column="type" />
<result property="fenceParams" column="fence_params" />
<result property="createTime" column="create_time" />
<result property="updateTime" column="update_time" />
</resultMap>
<sql id="selectUserFenceVo">
select id, user_id, type, fence_params, create_time, update_time from user_fence
</sql>
<select id="selectUserFenceByUIds" parameterType="List" resultMap="UserFenceResult">
<include refid="selectUserFenceVo"/>
<where>
<if test="list != null ">
and user_id IN
<foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
foreach标签用于遍历集合,它的属性:
- collection:代表要遍历的集合元素,注意编写时不要写#{}
- open:代表语句的开始部分(一直到动态的值之前)
- close:代表语句结束部分
- item:代表遍历集合的每个元素,生成的变量名(随便取)
- sperator:代表分隔符 (动态值之间的分割)