Delete
(Delete라 쓰고 Update라 읽는다) 구현 방법
우선 DB board table에 isDel 컬럼을 추가한다
isDeleted(enum 초기값 = N)를 Y로 업데이트 하는 Delete 기능을 구현할 것이다.
isDel 값이 'Y'인 글은 출력이 안되게 만들어서 delete기능이지만 데이터는 남고 삭제되지는 않는다
Controller
쿼리를 작업하거나 데이터를 입력할때 헷갈리지 않게 updateIsDelBoardById 이름을 붙인다
// isDeleted : Y로 업데이트 시킴.
// 요청URL은 DELETE http://localhost:8080/board/{id}
@DeleteMapping(value = "/{id}")
public ApiResponse<BoardDTO> updateIsDelBoardById(@PathVariable int id) throws Exception {
return boardService.updateIsDelBoardById(id);
}
updateIsDelBoardById 에 id값 보내서 DAO로 넘긴다.
쿼리를 수행한 값이 0이상, 즉 성공했을때 출력과 실패했을때 출력
// Board테이블의 isDel 컬럼의 데이터를 'Y' 로 업데이트
public ApiResponse<BoardDTO> updateIsDelBoardById(int id) {
int updatedRow = boardDAO.updateIsDelBoardById(id);
if(updatedRow > 0) {
return new ApiResponse(true, "board id " + id + " is successfully deleted");
}
return new ApiResponse(false, "failed to delete board id " + id);
}
아래는 DAO에 추가한 내용과 Mapper의 쿼리이다. (id값을 받아 해당 id값의 isDel 정보를 Y 로 upadate)
// DAO
int updateIsDelBoardById(int id);
//Mapper.xml
<update id="updateIsDelBoardById" parameterType="int">
UPDATE BOARD
SET isDEL = 'Y'
WHERE id = #{id}
</update>
출력!
'Spring Boot > Spring Boot' 카테고리의 다른 글
44 : Springboot(글 읽기 비즈니스 로직 추가) (0) | 2021.10.08 |
---|---|
42 : Springboot (글과 댓글 함께 출력) (0) | 2021.10.07 |
40: Springboot (Read-board) (0) | 2021.10.01 |
38 : Springboot (insert - test, board) (0) | 2021.09.29 |
37 : Springboot (Board - Mybatis) (0) | 2021.09.29 |