1、发送ajax请求
$("#delete_all_bt").click(function(){
var empNames="";
var del_idstr="";
//循环获取已选择的用户姓名和ID
$.each($(".check_item:checked"),function(){
empNames+=$(this).parents("tr").find("td:eq(2)").text()+",";
del_idstr+=$(this).parents("tr").find("td:eq(1)").text()+"-";
});
//去除姓名和ID最后多余的符号
empNames=empNames.substring(0,empNames.length-1);
del_idstr=del_idstr.substring(0,del_idstr.length-1);
if(confirm("确认删除"+empNames+"?")){
$.ajax({
url:"${APP_PATH}/emp/"+del_idstr,
type:"DELETE",
success:function (result) {
alert(result.msg);
//返回当前页
to_page(currentPage);
}
});
}
});
2、Controller内的处理请求方法----前端ID请求例如为1-2-3
@RequestMapping(value="/emp/{ids}",method = RequestMethod.DELETE)
@ResponseBody
public Msg deleteEmpById(@PathVariable("ids") String ids){
//批量删除,先去除连接符号,再组装
if(ids.contains("-")){
List<Integer> del_ids=new ArrayList<>();
String[] str_ids=ids.split("-");
for(String string:str_ids){
del_ids.add(Integer.parseInt(string));
}
employeeService.deleteBatch(del_ids);
}else{
//单个删除
Integer id = Integer.parseInt(ids);
employeeService.deleteEmpById(id);
}
return Msg.success();
}
3、Service层的deleteBath(del_ids)方法
public void deleteBatch(List<Integer> ids) {
EmployeeExample example=new EmployeeExample();
EmployeeExample.Criteria criteria = example.createCriteria();
criteria.andIdIn(ids);
employeeMapper.deleteByExample(example);
}
Criteria查询也叫做QBC查询(Query by Criteria),是完全面向对象的查询。
Criterion 是 Criteria 的查询条件。Criteria 提供了 add(Criterion criterion) 方法来添加查询条件。
例如上述方法中批量删除就是利用Criteria增加了where后面的条件: delete from emp WHERE ( id in ( ? , ? ) )