在项目中,我们会遇到批量修改,和批量删除,我们先把思路理清,可以两种方式,一种通过数据库操作,一种通过后台获取数据id,循环删除方法,但相比较而言,数据库语句耗时小,速度快。
下面来介绍通过数据库操作来进行批量修改状态
前台传入id,ajax传递data数据(join() 方法用于把数组中的所有元素放入一个字符串。)
data:{
infoIdList:infoId.join(','),
status:status
},
后台接收
@RequestMapping("/updStatusList")
@ResponseBody
public boolean updStatusList(
@RequestParam(value = "infoIdList") List<Integer> infoIdList,
@RequestParam(value = "status") String status) {
for (Integer infoId : infoIdList) {
WechatStudensInfo wechatStudensInfo = wechatStudensInfoManager.queryWechatStudensInfoById(infoId);
if(wechatStudensInfo.getStatus().equals("delete")){
return false;
}
}
return wechatStudensInfoManager.updShowStatus(infoIdList, status);
}
mapper做的数据库处理
<update id="updShowStatus">
update wechat_studens_info set status = #{status} where info_id in
<foreach collection="infoIdList" item="infoId" index="index" open="(" separator="," close=")">
#{infoId}
</foreach>
</update>
open="(" separator="," close=")的意思:以“(”开始,中间用“,”分开,以“)”结束。 item表示集合中每一个元素进行迭代时的别名, index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置, open表示该语句以什么开始, separator表示在每次进行迭代之间以什么符号作为分隔 符, close表示以什么结束。 在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况: 1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list 2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可