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

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

  • 相关阅读:
    利用@media screen实现网页布局的自适应
    心得体悟帖---200315(任何时候,都不要为不值得的人,不值得的事,费时间,费心力)
    心得体悟帖---200315(急啥,复习什么录什么)
    vue项目目录结构详解
    日常英语---200313(npm WARN deprecated vue-cli@2.9.6: This package has been deprecated in favour of @vue/cli)
    vuex是干什么的
    石川es6课程---4、箭头函数
    石川es6课程---3、变量let和常量const
    vue参考---eslink编码规范检查
    vue参考---vue项目结构
  • 原文地址:https://www.cnblogs.com/famine/p/10041109.html
Copyright © 2020-2023  润新知