ssm框架整合入门系列——删除-员工的删除

ssm框架整合入门系列——删除-员工的删除

员工的删除包括单个删除和批量删除,由于我们并没有实现多个员工删除的sql语句,所以我们需要组装一个:
EmployeeService层:

/**
     * 批量员工删除方法
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        // TODO Auto-generated method stub
        EmployeeExample example = new EmployeeExample();
        Criteria criteria = example.createCriteria();
        //delete from xxx where emp_id in(1,2,3)
        criteria.andEmpIdIn(ids);
        employeeMapper.deleteByExample(example);
        
    }

EmployeeController的回调方法:

/**
     * 员工删除
     * 单个、批量删除二合一
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/emp/{ids}",method=RequestMethod.DELETE)
    public Msg deleteEmpById(@PathVariable("ids")String ids){
        if(ids.contains("-")){
            List<Integer> del_ids = new ArrayList();
            String[] str_ids = ids.split("-");
            for(String str : str_ids){
                del_ids.add(Integer.parseInt(str));
            }
            employeeService.deleteBatch(del_ids);
        }else{
            Integer id = Integer.parseInt(ids);
            employeeService.deleteEmp(id);
        }
        return Msg.success();
    }
    

有意思的是,ajax发送请求的id组,是用-连接,传到后台在用字符串split操作成数组,达到传输的目的。
看看,js操作:

//点击全部删除,就批量删除
        $("#emp_delete_all_btn").click(function(){
            var empNames = "";
            var del_idstr = "";
            $.each($(".check_item:checked"),function(){
                empNames += $(this).parents("tr").find("td:eq(2)").text()+",";
                //组装员工id字符串
                del_idstr += $(this).parents("tr").find("td:eq(1)").text()+"-";
            });
            //去除empNames最后的逗号
            empNames = empNames.substring(0,empNames.length-1);
            //去除del_idstr最后的-号
            del_idstr = del_idstr.substring(0,del_idstr.length-1);
            if(confirm("确认删除【"+empNames+"】吗?")){
                //发送ajax请求删除
                $.ajax({
                    url:"${path}/ssm-crud/emp/"+del_idstr,
                    type:"DELETE",
                    success:function(result){
                        alert(result.msg);
                        //回到当前页面
                        to_page(currentPage);
                        
                    }
                
                })
                
            }
            
        })

我发现细节是说不完的,以后只挑重点记。

转载于:https://www.cnblogs.com/famine/p/10041109.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值