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);
}
})
}
})
我發現細節是說不完的,以后只挑重點記。
