MyBatis参数传入集合之foreach用法

时间:2023-03-08 15:33:43

传入集合list

   // 账户类型包括门店和分公司
List<Object> scopeList = new ArrayList<Object>();
scopeList.add(UserConstants.UserScope.STORE);
scopeList.add(UserConstants.UserScope.BRANCH_COMPANY);
params.put("scopeList", scopeList);
PageResult<UserDto> userPage = userService.getOtherAccountList(params);

mybatis的sql文件

 <select id="getOtherAccountList" resultMap="UserMap">
SELECT <include refid="columns" />
FROM sys_user
<where>
<if test="scopeList !=null">
AND scope in <foreach collection="scopeList" index="index" item="item" open="(" separator="," close=")">#{item}</foreach>
</if>
<if test="account != null">
AND account LIKE CONCAT('%',#{account},'%' )
</if>
<if test="shopId !=null">
AND shop_id = #{shopId}
</if>
<if test="primary !=null">
AND primary = #{primary}
</if>
<if test="status !=null">
AND status = #{status}
</if>
</where>
order by id desc , create_time desc
</select>

collection是要遍历的参数集合,item是集合中每个项,取值就这样取#{item}