
1. 게시글 삭제 쿼리 작성
@Transactional
public void deleteById(int id) {
Query query =
em.createNativeQuery("delete from board_tb where id = ?");
query.setParameter(1, id);
query.executeUpdate();
}
2. 게시글 삭제 쿼리 테스트
@Test
public void deleteById_test() {
//given
int id = 1;
//when
boardNativeRepository.deleteById(id);
//then
List<Board> boardList = boardNativeRepository.findAll();
assertThat(boardList.size()).isEqualTo(3);
}

3. 컨트롤러 delete Endpoint 만들기
@PostMapping("/board/{id}/delete")
public String delete(@PathVariable Integer id) {
boardNativeRepository.deleteById(id);
return "redirect:/";
}
Share article