今天做关于批量删除的操作,之前都是拼接字符串然后到后台转换,感觉有点麻烦,于是试了直接传递数组到后端,做个笔记方便之后回顾。
传递数组对象前端用Ajax实现,具体代码如下:
var checkedArr = [];
......
$.ajax({
type : 'POST',
url: "settings/dictionary/deleteDic.do",
data : {'arrays[]':checkedArr},
dataType:"json",
success : function(data) {
if (data.code==1){
alert("删除成功");
window.location.href="settings/dictionary/selectDictionary.do";
} else{
alert("删除失败");
}
},
后台接收:
@RequestMapping(value="/settings/dictionary/deleteDic.do",method = { RequestMethod.POST})
@ResponseBody
public Object deleteDic( @RequestParam(value = "arrays[]", required = false)String[] arrays){
Map<String,Object> map = new HashMap<>();
int i =tblDicTypeService.deleteByPrimaryKey(arrays);
...
xml:
<delete id="deleteByPrimaryKey" parameterType="java.util.Arrays" >
delete from tbl_dic_type where code in
<foreach collection="array" open="(" close=")" separator="," item="code">
#{ code}
</foreach>
</delete>