批量删除

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 ( ? , ? ) )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值