[React/연습] #04. React back-end 세팅 (3)

Updated:

DTO, Service 등 데이터 처리에 필요한 클래스와 Controller를 완성하려고 한다.

여기서 작성하는 코드는 전부 이 블로그 작성자님께서 쓰신 걸 그대로 따라했다.

데이터 처리 클래스

dto 폴더에 DTO 클래스, util 폴더에 Pagination, Header, Search 클래스를 만들었다.

Service

Mapper 수정: BoardMapper.java

@Mapper
public interface BoardMapper {
    List<BoardEntity> getBoardList(HashMap<String, Object> paramMap);

    int getBoardTotalCount(HashMap<String, Object> paramMap);

    BoardEntity getBoardOne(Long idx);

    int insertBoard(BoardEntity entity);

    int updateBoard(BoardEntity entity);

    int deleteBoard(Long idx);
}

Mapper 수정: board.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper3.0//EN" "http://mybatis.org/schema/mybatis-3-mapper.dtd">

<mapper namespace="com.example.practice.db.BoardMapper">
    <select id="getBoardList" parameterType="Map" resultType="com.example.practice.db.BoardEntity">
        SELECT IDX
        , TITLE
        , CONTENTS
        , CREATED_BY
        , CREATED_AT
        FROM TB_BOARD
        WHERE 1=1

        <if test="sk != '' || sk != null">
            <if test="sk == 'title'">
                AND TITLE LIKE CONCAT('%', #{sv}, '%')
            </if>
            <if test="sk == 'contents'">
                AND CONTENTS LIKE CONCAT('%', #{sv}, '%')
            </if>
            <if test="sk == 'createdBy'">
                AND CREATED_BY LIKE CONCAT('%', #{sv}, '%')
            </if>
        </if>
        ORDER BY IDX DESC
        LIMIT #{page}, #{size}
    </select>

    <select id="getBoardTotalCount" parameterType="Map" resultType="Int">
        SELECT COUNT(IDX)
        FROM TB_BOARD
        WHERE 1=1
        <if test="sk != '' || sk != null">
            <if test="sk == 'title'">
                AND TITLE LIKE CONCAT('%', #{sv}, '%')
            </if>
            <if test="sk == 'contents'">
                AND CONTENTS LIKE CONCAT('%', #{sv}, '%')
            </if>
            <if test="sk == 'createdBy'">
                AND CREATED_BY LIKE CONCAT('%', #{sv}, '%')
            </if>
        </if>
    </select>

    <select id="getBoardOne" parameterType="Long" resultType="com.example.practice.db.BoardEntity">
        SELECT IDX
        , TITLE
        , CONTENTS
        , CREATED_BY
        , CREATED_AT
        FROM TB_BOARD
        WHERE IDX = #{idx}
    </select>

    <insert id="insertBoard" parameterType="com.example.practice.db.BoardEntity" keyProperty="idx" useGeneratedKeys="true">
        INSERT INTO TB_BOARD
        (
        TITLE
        , CONTENTS
        , CREATED_BY
        , CREATED_AT
        ) VALUES (
        #{title}
        , #{contents}
        , #{createdBy}
        , NOW()
        )
    </insert>

    <update id="updateBoard" parameterType="com.example.practice.db.BoardEntity">
        UPDATE TB_BOARD
        SET TITLE = #{title}
        , CONTENTS = #{contents}
        WHERE IDX = #{idx}
    </update>

    <delete id="deleteBoard" parameterType="Long">
        DELETE FROM TB_BOARD
        WHERE IDX = #{idx}
    </delete>
</mapper>

Controller 수정

컨트롤러와 같은 위치에 Service 클래스를 만들고 컨트롤러를 다음과 같이 수정했다.

@RequiredArgsConstructor
@RestController
public class BoardController {
    private final BoardService boardService;

    @GetMapping("/board")
    Header<List<BoardEntity>> getBoardList(@RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size, Search search) {
        return boardService.getBoardList(page, size, search);
    }

    @GetMapping("/board/{idx}")
    Header<BoardEntity> getBoardOne(@PathVariable Long idx) {
        return boardService.getBoardOne(idx);
    }

    @PostMapping("/board")
    Header<BoardEntity> createBoard(@RequestBody BoardSaveDto boardSaveDto) {
        return boardService.insertBoard(boardSaveDto);
    }

    @PatchMapping("/board")
    Header<BoardEntity> updateBoard(@RequestBody BoardSaveDto boardSaveDto) {
        return boardService.updateBoard(boardSaveDto);
    }

    @DeleteMapping("/board/{idx}")
    Header<String> deleteBoard(@PathVariable Long idx) {
        return boardService.deleteBoard(idx);
    }
}

Leave a comment