spring-data-jpa实现批量删除

  1. dao层:
@Modifying	
	@Transactional
	@Query("delete  from Student s  where  s.id in (:ids) ")
	void deleteStudentById(@Param("ids") List ids);

  2. service层:

@Transactional
	@Override
	public void deleteStudent(List ids) {
		
		studentDao.deleteStudentById(ids);
	}

3.controller层:

	@GetMapping("/deletes")
	@ResponseBody
	public String deletes(String fitList,RedirectAttributes attributes,HttpServletResponse response) throws IOException {
		
		 String t = fitList.replaceAll("\\\"","");

			String replaceAll = t .replace("[", "").replace("]","");
		 System.out.println(replaceAll);
		
		 String[] str = replaceAll.split(",");
		 List list= new ArrayList<>();
		for(String l:str) {
			Integer i = Integer.valueOf(l);
			list.add(i);
		}
		studentService.deleteStudent(list);
		response.getWriter().write("success");
		attributes.addFlashAttribute("message", "操作成功");
		return null;
		
	}

4.前端页面:

function delArc(aid){
	var publishIds="";
	    var ids=[] ;
	    $('input[name="id"]:checked').each(function(){  
	     ids.push($(this).val());
	    }); 
	    publishIds=ids.join(',');
	 
	  var fitList = JSON.stringify(ids);
	  $.ajax({
			url:"/student/deletes",
			type:"get",
			dataType:"json",
			data:{
				"fitList":fitList},
				  success : function(data) {
			            alert(data);
			            // 删除成功后刷新页面
			            window.location.reload();
			        },
			        error : function() {
			            alert("请求失败");
			        },
			        dataType : "text"
	  		
			
		})
}

 

你可能感兴趣的:(spring-data-jpa实现批量删除)