v3 - 게시글 삭제

coding S's avatar
Mar 15, 2024
v3 - 게시글 삭제

[ delete 쿼리문 생성 ]

@RequiredArgsConstructor @Repository public class BoardRepository { private final EntityManager em; @Transactional public void deleteById(int id) { Query query = em.createQuery("delete from Board b where b.id = :id"); query.setParameter("id", id); query.executeUpdate(); }
💡
delete는 em.remove 사용하지 말고, 쿼리문을 적어야함!
 

[ 테스트 ]

@Test public void deleteById_test() { //given int id = 1; //when boardRepository.deleteById(id); //delete쿼리 발동함! //then System.out.println("deleteById_test : " + boardRepository.findAll().size()); }
notion image
 

[ detail.mustache ]

notion image
💡
삭제 버튼 누르면 action 발동!
 

[ BoardController ]

@PostMapping("/board/{id}/delete") public String delete(@PathVariable Integer id){ //권한, 인증 체크 지금은 생략 boardRepository.deleteById(id); return "redirect:/"; }
 

[ 화면 확인 ]

notion image
💡
제목3 삭제 완료!
 
Share article

codingb