ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 03/24(Wed) @Transactional๊ณผ @RequestBody
    HangHae99/TIL-hanghae99 2021. 3. 24. 22:33

    ๐Ÿšฉ ์–ด์ œ๊นŒ์ง„ CR๋งŒ ๋งŒ๋“ค์—ˆ๊ณ  ์˜ค๋Š˜์€ UD๋ฅผ ๋งˆ์ € ๋งŒ๋“ค์–ด๋ณด์ž

     

    @Transactional๊ณผ @RequestBody๋ฅผ ๋นผ๋จน๊ณ  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์„ ๋•Œ ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€ ์•Œ์•„๋ณด์ž.

    ์ž์ฃผ ๋นผ๋จน๊ณ  ์ž‘์„ฑํ•˜๋Š” ๋ฐ”๋žŒ์— ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚ฌ๋Š”์ง€ ์ด๋ฏธ ์•Œ๊ณ  ์žˆ์ง€๋งŒ ์ •๋ฆฌ ํ•˜๊ณ  ๋„˜์–ด๊ฐ€์•ผ๊ฒ ๋‹ค.

     

     

    ๐Ÿ“• Update์™€ Delete

     

    ์ˆ˜์ •, ์‚ญ์ œ๋ฅผ ์ถ”๊ฐ€ํ•œ detail.html

     

     

     

    ๐Ÿ“‘ ์ฝ”๋“œ ํ๋ฆ„

    1. ์‚ญ์ œ ๊ธฐ๋Šฅ

    ์‚ญ์ œ๋Š” ๊ฐ„๋‹จํ•˜๋‹ค. ์‚ญ์ œ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ๊ฒŒ์‹œ๊ธ€์ด ์—†์–ด์ง€๋ฉด ๋˜๋‹ˆ๊นŒ ํด๋ผ์ด์–ธํŠธ -> ์ปจํŠธ๋กค๋Ÿฌ ๋งคํ•‘์„ ํ•ด๋ณด์ž.

     

    function addBoardHTML(id, title, username, createdAt, contents) {
                let tempHtml = `<div class="card board">
                                    <div class="card-header title-area">
                                        <div class="one-title">${title}</div>
                                        <footer class="blockquote-footer">${username} &nbsp;|
                                            <cite title="Source Title"> ${createdAt} &nbsp;| </cite>
                                            <span id="${id}-board-edit" class="board-edit" onclick="location.href='rewrite.html?id=${id}'">์ˆ˜์ • &nbsp;|</span>
                                            <span id="${id}-board-del" class="board-del" onclick="delBoard(${id})">์‚ญ์ œ</span>
                                        </footer>
                                    </div>
                                    <div class="card-body">
                                        <blockquote class="blockquote mb-0 content-area">
                                            <p class="one-content">${contents}</p>
                                        </blockquote>
                                    </div>
                                </div>`
                $('#boards-box').append(tempHtml);
            }

     

    detail.html์—์„œ ์‚ญ์ œ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด delBoard(id) ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋œ๋‹ค.

     

    delBoard(id)ํ•จ์ˆ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

    function delBoard(id) {
                $.ajax({
                    type: "DELETE",
                    url: `/api/boards/${id}`,
    
                    success: function (response) {
                        alert("์‚ญ์ œ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")
                        window.location.assign("index.html");
                    }
                })
            }

    ํด๋ผ์ด์–ธํŠธ์—์„œ ์š”์ฒญ์„ ํ–ˆ์œผ๋‹ˆ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

     

    @DeleteMapping("api/boards/{id}")
        public Long deleteOneBoard(@PathVariable Long id) {
            boardRepository.deleteById(id);
            return id;
        }

     

    repository์—์„œ deleteById(id) ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋์ด๋‹ค.

     

    ์‚ญ์ œ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์ € ์ฝ”๋“œ๋“ค์ด ์‹คํ–‰์ด ๋˜๊ณ  ๋‹ค์‹œ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ต์„ ์ „ํ•ด์ฃผ๋Š”๋ฐ, ๋ทฐ์—์„œ๋Š” ์‚ญ์ œ ๋˜์—ˆ๋‹ค๋Š” alert์ฐฝ์ด ๋œจ๊ณ  index.html ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๊ฒŒ ๋œ๋‹ค.

     

    1) ํ…Œ์ŠคํŠธ

     

    ๊ฒŒ์‹œ๊ธ€์„ 2๊ฐœ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์•˜๋‹ค. ์ œ์ผ ์œ„์— ์žˆ๋Š” ๊ธ€์„ ์‚ญ์ œ ํ•ด๋ณด์ž.

    index.html

     

     

    ๊ฒŒ์‹œ๊ธ€์„ ํด๋ฆญํ•˜๊ณ  detail.html ํŽ˜์ด์ง€๋กœ ๋“ค์–ด์™€์„œ ์‚ญ์ œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋‹ˆ ์‚ญ์ œ๊ฐ€ ๋˜์—ˆ๋‹ค๊ณ  ํ•œ๋‹ค.

     

    detail.html

     

    ์ œ์ผ ์œ„์— ์žˆ๋˜ ๊ธ€์ด ์—†์–ด์ง„ ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ์‚ญ์ œ ํ›„ ์ด๋™ํ•œ index.html

     

     

     

    2. ์ˆ˜์ • ๊ธฐ๋Šฅ

     

    detail.html ์—์„œ ์ˆ˜์ • ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ rewrite.html ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค.

    ์ด๋™ํ•˜๋ฉด์„œ url์— ์ฟผ๋ฆฌ๋กœ ์•„์ด๋””๋ฅผ ๊ฐ™์ด ๋ณด๋‚ธ๋‹ค.

     

    ํŽ˜์ด์ง€๋Š” ์ด๋ ‡๊ฒŒ ๊ตฌ์„ฑํ•ด๋†จ๊ณ , ์ˆ˜์ • ๊ธฐ๋Šฅ์€ ๋งŒ๋“ค๋ฉด์„œ ์‹ค์ˆ˜๋ฅผ ๋งŽ์ด ํ•ด์„œ ์• ๋จน์€ ๋ถ€๋ถ„ ์ค‘์‹ฌ์œผ๋กœ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

     

    ๋จผ์ € ํด๋ผ์ด์–ธํŠธ์—์„œ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์š”์ฒญํ•˜๋Š” ๋ถ€๋ถ„ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

    $.ajax({
                    type: "PUT",
                    url: `/api/boards/${id}`,
    
                    contentType: "application/json",
                    data: JSON.stringify(data),
                    success: function (response) {
                        alert("์ˆ˜์ • ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
                        window.location.href="index.html";
                    }
                })

    ์ปจํŠธ๋กค๋Ÿฌ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์ž.

    @PutMapping("api/boards/{id}")
        public Long updateBoard(@PathVariable Long id, @RequestBody BoardRequestDto boardRequestDto) {
            boardService.update(id, boardRequestDto);
            return id;
        }

    ์—ฌ๊ธฐ์„œ ๋“ฑ์žฅํ•˜๋Š” @RequestBody.

    ์ฒ˜์Œ์— @RequestBody๋ฅผ ๋นผ๋จน๊ณ  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ์ฑ„๋กœ ์ฝ”๋“œ๋ฅผ ๋Œ๋ ธ๋‹ค. ์ด๋ ‡๊ฒŒ ์„œ๋ฒ„๋ฅผ ๋Œ๋ฆฌ๋ฉด ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚ ๊นŒ? ์ด๋ฏธ ๊ฒช์–ด๋ดค์ง€๋งŒ ๋‹ค์‹œ ํ•œ ๋ฒˆ ์•Œ์•„๋ณด์ž.

     

     

     

    1) @RequestBody

     

    ์ˆ˜์ •, ์‚ญ์ œ๋ฅผ ์ถ”๊ฐ€ํ•œ detail.html

     

     

    ์ˆ˜์ • ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด rewrite.html ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๊ฒŒ ๋˜๊ณ , rewrite.html์˜ ํŽ˜์ด์ง€๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

     

    edit ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด๋„ ์•„๋ฌด ์ผ๋„ ์ผ์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค.

    ์ธํ…”๋ฆฌ์ œ์ด ์ฝ˜์†”์„ ์‚ดํŽด๋ณด์ž.

     

    ์ธํ…”๋ฆฌ์ œ์ด ์ฝ˜์†”

    ๋‚œ๋ฆฌ๊ฐ€ ๋‚ฌ๋‹ค.

    ๋‹ค์‹œ @RequestBody๋ฅผ ์ž‘์„ฑํ•ด์ฃผ๊ณ  ์„œ๋ฒ„๋ฅผ ๋Œ๋ ค๋ณด์ž.

     

    rewrite.html

     

     

    index.html
    detail.html

     

    index.html,  rewrite.html,  detail.html ํŽ˜์ด์ง€ ๋ชจ๋‘ ์œ ๊ธฐ์ ์œผ๋กœ ์ž˜ ์—ฐ๊ฒฐ๋ผ์„œ ๊ฒฐ๊ณผ๋ฌผ์ด ๋‚˜์˜จ๋‹ค.

     

     

    @RequestBody๊ฐ€ ๋„๋Œ€์ฒด ๋ญ๊ธธ๋ž˜... ๋ผ๋Š” ์ƒ๊ฐ์„ ์•ˆํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค.

    @RequestBody๋Š” HTTP body์•ˆ์— JSON ํ˜•์‹์œผ๋กœ ๋„˜์–ด์˜จ ๋ฐ์ดํ„ฐ๋ฅผ Java Objectํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜์‹œ์ผœ์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

     

    ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ๋ณด์ž.

    function rewriteBoard(id) {
                let title = $('#inputTitle-re').val();
                let username = $('#inputUsername-re').val();
                let contents = $('#inputContents-re').val();
    
                let data = {'title':title, 'username':username, 'contents':contents};
    
                $.ajax({
                    type: "PUT",
                    url: `/api/boards/${id}`,
    
                    contentType: "application/json",
                    data: JSON.stringify(data),
                    success: function (response) {
                        console.log(response)
                        alert("์ˆ˜์ • ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
                        window.location.href="index.html";
                    }
                })
    
            }

     

    ํด๋ผ์ด์–ธํŠธ์—์„œ ์ปจํŠธ๋กค๋Ÿฌ์—๊ฒŒ data๋ฅผ ์ œ์ด์Šจ ํ˜•์‹์œผ๋กœ ๋„˜๊ฒจ์คฌ๋‹ค.

    ์ปจํŠธ๋กค๋Ÿฌ๋Š” ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์•ผ ํ•˜๋Š”๋ฐ, ์ด๋ฅผ java์—์„œ ๋ฐ›์œผ๋ ค๋ฉด java object๋กœ ๋ณ€ํ™˜์„ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

    ์ด๋Ÿฐ ์ž‘์—…์„ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด @RequestBody์ด๋‹ค. 

    ์š”์•ฝํ•˜์ž๋ฉด @RequestBody๋Š” http ์š”์ฒญ ๋‚ด์šฉ์„ ์ž๋ฐ” ๊ฐ์ฒด๋กœ ๋งคํ•‘ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

     

    @PutMapping("api/boards/{id}")
        public Long updateBoard(@PathVariable Long id, @RequestBody BoardRequestDto boardRequestDto) {
            boardService.update(id, boardRequestDto);
            return id;
        }

     

    @PathVariable ์ ์–ด์ฃผ๋Š” ๊ฑด ์•ˆ๊นŒ๋จน๋Š”๋ฐ, @RequestBody๋Š” ๋ช‡ ๋ฒˆ ๋นผ๋จน๊ณ  ์ ์–ด์„œ ์˜ค๋ฅ˜๊ฐ€ ๋งŽ์ด ๋‚ฌ๋‹ค.

    ์žŠ์ง€๋ง์ž.

     

     

     

    2) @Transactional

    @Transactional์„ ๋‹ฌ์•„์ฃผ์ง€ ์•Š์•˜์„ ๋• ์–ด๋–ค ์ผ์ด ๋ฐœ์ƒํ• ๊นŒ..?

     

    BoardService.java ํŒŒ์ผ์—์„œ @Transactional ์–ด๋…ธํ…Œ์ด์…˜์„ ๋นผ๋ฒ„๋ฆฌ๊ณ  ์„œ๋ฒ„๋ฅผ ๋‹ค์‹œ ๋Œ๋ ค๋ณด์ž.

     

    ๊ฒŒ์‹œ๊ธ€์„ ํ•˜๋‚˜ ๋“ฑ๋กํ–ˆ๋‹ค.

    index.html

     

    ๊ฒŒ์‹œ๊ธ€์„ ์ˆ˜์ •ํ•ด๋ณด์ž.

     

    rewrite.html

     

    ๊ฒŒ์‹œ๊ธ€์ด ์ˆ˜์ •๋˜์—ˆ๋‹ค๊ณ  ํ•œ๋‹ค. ์ˆ˜์ •๋˜์—ˆ๋‹ค๋Š” alert์ฐฝ์ด ๋–ด๊ณ , ํ™•์ธ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋‹ค์‹œ index.html ํŽ˜์ด์ง€๋กœ ๋Œ์•„๊ฐ€๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. 

     

    ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • ํ›„ ์ด๋™ํ•œ index.html

     

    ...?

    ๋ถ„๋ช…ํžˆ ์ˆ˜์ •์ด ๋๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ, ์ด๋™ํ•œ index.html ํŽ˜์ด์ง€์—๋Š” ์ˆ˜์ • ์ „ ๊ธ€์ด ๊ทธ๋Œ€๋กœ ๋ณด์ธ๋‹ค.

    @Transactional ์–ด๋…ธํ…Œ์ด์…˜์„ ๋นผ๋จน์€ ๋Œ€๊ฐ€์ด๋‹ค.

     

    @Transactional ์ด๋ž€ ๋ญ˜๊นŒ? ์™œ ์ˆ˜์ • ๊ธ€์ด ๋ฐ˜์˜์ด ์•ˆ๋˜๋Š” ๊ฑธ๊นŒ?

    ๊ตฌ๊ธ€๋งํ•˜๋ฉด์„œ ๊ณต๋ถ€ํ•ด๋ณด๋‹ˆ ํŠธ๋žœ์žญ์…˜์„ ๋จผ์ € ์•Œ์•„์•ผ ํ•˜๋Š”๋ฐ, ํŠธ๋žœ์žญ์…˜์ด๋ž€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝ์‹œํ‚ค๋Š” ์ž‘์—… ๋˜๋Š” ํ•œ๋ฒˆ์— ์ˆ˜ํ–‰๋˜์–ด์•ผ ํ•˜๋Š” ์—ฐ์‚ฐ๋“ค์„ ์˜๋ฏธํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

     

    @Transactional์€ ์„ ์–ธ์  ํŠธ๋žœ์žญ์…˜์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, xmlํŒŒ์ผ์—์„œ ์„ค์ •ํ•ด์ฃผ๊ฑฐ๋‚˜ ์ด๋ ‡๊ฒŒ ์–ด๋…ธํ…Œ์ด์…˜์„ ๋‹ฌ์•„์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

    ๊ทธ๋ฆฌ๊ณ  Spring์—์„œ JPA ๊ธฐ์ˆ ์„ ์“ธ ๋•Œ ๋นผ๋†“์„ ์ˆ˜ ์—†๋Š” ๊ธฐ๋Šฅ ์ค‘ ํ•˜๋‚˜๊ฐ€ @Transactional์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, transaction begin, commit์„ ์ž๋™์œผ๋กœ ์ˆ˜ํ–‰ํ•ด์ฃผ๊ณ , ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒ๋˜๋ฉด rollback์ฒ˜๋ฆฌ๋ฅผ ์ž๋™์œผ๋กœ ์ˆ˜ํ–‰ํ•ด์ค€๋‹ค๊ณ  ํ•œ๋‹ค.

     

     

    public Long update(Long id, BoardRequestDto boardRequestDto) {
            Board board = boardRepository.findById(id).orElseThrow(
                    () -> new IllegalArgumentException("์•„์ด๋””๊ฐ€ ์กด์žฌ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ต์…‰์…˜ ํ„ฐ์ง")
            );
            board.update(boardRequestDto);
            return board.getId();
        }

    @Transactional ์–ด๋…ธํ…Œ์ด์…˜์„ ๋‹ฌ์ง€ ์•Š๊ณ  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋‹ˆ ์ด ๋ฉ”์„œ๋“œ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์•„๋ฌด๋Ÿฐ ๊ด€๋ จ์ด ์—†๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ๋ผ๋‹ˆ, ์ € ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰์ด ๋ผ๋„ db์—๋Š” ์•„๋ฌด๋Ÿฐ ์˜ํ–ฅ์„ ๋ชป๋ฏธ์ณ์„œ ์—…๋ฐ์ดํŠธ๊ฐ€ ๋˜์ง€ ์•Š์•˜๋˜ ๊ฑด๊ฐ€...?

    ์•„๋‹ˆ๋ฉด ์‹คํ–‰์€ ๋๋Š”๋ฐ commit์„ ์•ˆํ•ด์ค˜์„œ ์—…๋ฐ์ดํŠธ๊ฐ€ ์•ˆ๋œ ๊ฑด๊ฐ€..? ์ด ๋ถ€๋ถ„์€ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค.

    (์•„์‹œ๋Š” ๋ถ„ ๊ณ„์‹œ๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.)

    ๊ฐ•์˜ ์ž๋ฃŒ์—๋Š” @Transactional์€ SQL ์ฟผ๋ฆฌ๊ฐ€ ์ผ์–ด๋‚˜์•ผ ํ•จ์„ ์Šคํ”„๋ง์—๊ฒŒ ์•Œ๋ ค์ค€๋‹ค๊ณ  ๋ผ์žˆ๋Š”๋ฐ, ์ด ๋ถ€๋ถ„์€ ์ข€ ๋” ํŒŒ๋ด์•ผ๊ฒ ๋‹ค.

     

     

    @Transactional์„ ๋‹ฌ๊ณ  ์„œ๋ฒ„ ์žฌ์‹œ์ž‘ํ•˜๋‹ˆ ์ˆ˜์ •์ด ์ž˜ ๋˜๊ณ , ๋ฐ˜์˜๋„ ์ž˜ ๋œ๋‹ค.

     @Transactional
        public Long update(Long id, BoardRequestDto boardRequestDto) {
            Board board = boardRepository.findById(id).orElseThrow(
                    () -> new IllegalArgumentException("์•„์ด๋””๊ฐ€ ์กด์žฌ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ต์…‰์…˜ ํ„ฐ์ง")
            );
            board.update(boardRequestDto);
            return board.getId();
        }

    ๋Œ“๊ธ€