练涛

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

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

posted @ 2018-11-29 21:46  练涛  阅读(1465)  评论(0编辑  收藏  举报