spring-data-jpa 대량 삭제 실현

1877 단어
4
  • dao층:
  • @Modifying	
    	@Transactional
    	@Query("delete  from Student s  where  s.id in (:ids) ")
    	void deleteStudentById(@Param("ids") List ids);

      2. 서비스 계층:
    @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"
    	  		
    			
    		})
    }

    좋은 웹페이지 즐겨찾기