MyBatis参数传入集合之foreach用法

传入集合list

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

mybatis的sql文件

 1 <select id="getOtherAccountList" resultMap="UserMap">
 2         SELECT <include refid="columns" />
 3         FROM sys_user 
 4         <where>
 5             <if test="scopeList !=null">
 6                 AND scope in <foreach collection="scopeList" index="index" item="item" open="(" separator="," close=")">#{item}</foreach>
 7             </if>
 8             <if test="account != null">
 9                 AND account LIKE CONCAT('%',#{account},'%' )
10             </if>
11            <if test="shopId !=null">
12                 AND shop_id = #{shopId}
13             </if>
14             <if test="primary !=null">
15                 AND primary = #{primary}
16             </if>
17            <if test="status !=null">
18                 AND status = #{status}
19             </if>
20         </where>
21         order by id desc , create_time desc
22     </select>

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

 

posted @ 2016-04-18 17:29  乱码出黑客  阅读(1025)  评论(0)    收藏  举报