版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aodeng110/article/details/82835571
版权声明
版权声明:本文由 低调小熊猫 发表于 低调小熊猫的博客
转载声明:自由转载-非商用-非衍生-保持署名,非商业转载请注明作者及出处,商业转载请联系作者本人qq:2696284032
文章链接:https://aodeng.cc/archives/deletebug
出现的Bug
如图,我开始复制delete语句和参数到数据库执行,删除两条数据,但是后台执行确只删除一条数据,当时表示一脸懵逼
分析原因
如图,正确的参数传值应该是这样的,聪明的同学,应该就知道哪里错了
解决问题
我就不贴开始的代码了,直接贴解决bug的代码
mybatis中的代码
<!-- 批量删除-->
<delete id="deleteByIds" parameterType="int[]">
<![CDATA[
DELETE FROM p_customer
WHERE customerId in
]]>
<foreach collection="array" item="arr" index="no" open="("
separator="," close=")">
#{arr}
</foreach>
</delete>
controller中的代码
/**
* 删除和批量删除
*/
@RequestMapping(value = "/del", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<PCustomerVo> delete(@RequestParam String customerId) throws Exception {
//获取批量删除的id,去掉最后一个“,”
customerId=customerId.substring(0,customerId.length()-1);
String[] strarr=customerId.split(",");
int[] arr=new int[strarr.length];
for(int i=0;i<strarr.length;i++){
arr[i]=Integer.parseInt(strarr[i]);
}
pcustomerService.deletes(arr);
return new ResponseEntity<>(HttpStatus.OK);
}