본문 바로가기

Spring Boot/Spring Boot

41: Springboot (Delete-board)

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>

 

출력!

삭제를 성공하면 성공 메시지 출력!