SSM-CRUD-删除

来源:互联网 发布:sql课程实训教学计划 编辑:程序博客网 时间:2024/06/03 21:21

1、单个删除
• URI:/emp/{id} DELETE
2、批量删除

单个删除

1.index.jsp:

//单个删除$(document).on("click",".delete_btn",function(){    //1、弹出是否确认删除对话框    var empName = $(this).parents("tr").find("td:eq(2)").text();    var empId = $(this).attr("del-id");    //alert($(this).parents("tr").find("td:eq(1)").text());    if(confirm("确认删除【"+empName+"】吗?")){        //确认,发送ajax请求删除即可        $.ajax({            url:"${APP_PATH}/emp/"+empId,            type:"DELETE",            success:function(result){                alert(result.msg);                //回到本页                to_page(currentPage);            }        });    }});

2.EmployeeService.java:

/** * 员工删除 * @param id */public void deleteEmp(Integer id) {    // TODO Auto-generated method stub    employeeMapper.deleteByPrimaryKey(id);}

批量删除

1.index.jsp:

//完成全选/全不选功能$("#check_all").click(function(){    //attr获取checked是undefined;    //我们这些dom原生的属性;attr获取自定义属性的值;    //prop修改和读取dom原生属性的值    $(".check_item").prop("checked",$(this).prop("checked"));});//check_item$(document).on("click",".check_item",function(){    //判断当前选择中的元素是否5个    var flag = $(".check_item:checked").length==$(".check_item").length;    $("#check_all").prop("checked",flag);});//点击全部删除,就批量删除$("#emp_delete_all_btn").click(function(){    //    var empNames = "";    var del_idstr = "";    $.each($(".check_item:checked"),function(){        //this        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);    //去除删除的id多余的-    del_idstr = del_idstr.substring(0, del_idstr.length-1);    if(confirm("确认删除【"+empNames+"】吗?")){        //发送ajax请求删除        $.ajax({            url:"${APP_PATH}/emp/"+del_idstr,            type:"DELETE",            success:function(result){                alert(result.msg);                //回到当前页面                to_page(currentPage);            }        });    }});

2.EmployeeService.java:

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);}

3.EmployeeController.java:

/** * 单个批量二合一 * 批量删除:1-2-3 * 单个删除:1 *  * @param id * @return */@ResponseBody@RequestMapping(value="/emp/{ids}",method=RequestMethod.DELETE)public Msg deleteEmp(@PathVariable("ids")String ids){    //批量删除    if(ids.contains("-")){        List<Integer> del_ids = new ArrayList<Integer>();        String[] str_ids = ids.split("-");        //组装id的集合        for (String string : str_ids) {            del_ids.add(Integer.parseInt(string));        }        employeeService.deleteBatch(del_ids);    }else{        Integer id = Integer.parseInt(ids);        employeeService.deleteEmp(id);    }    return Msg.success();}
原创粉丝点击