Board 일괄추가,일괄삭제,일괄수정

HUGO·2022년 10월 7일
0

JPA

목록 보기
4/6

dto/BoardList.java 파일 만들기

package com.example.dto;

import java.util.List;

import com.example.entity.Board;

import lombok.Data;

@Data
public class BoardList {
    List<Board> list;
}

이는 controller랑 view 간에 전송하기위해 만들어짐 entity아님!

Controller/BoardController.java 에 코드 추가


@GetMapping(value="/updatebatch.do")
    public ModelAndView insertBatch(
            @RequestParam(name="chk")List<Long> chk){
        List<Board> list = bRepository.findAllById(chk);
        BoardList boardList = new BoardList();
        boardList.setList(list);
        return new ModelAndView("board_updatebatch", "obj", boardList);
    }

    @PostMapping(value="/updatebatch.do")
    public String updateBatchPOST(
            @ModelAttribute BoardList boardList){
        System.out.println(boardList.getList().toString());
        // 1. boardList의 목록을 이용하여 기존데이터 읽기

        // 2. 기본데이터에 받은 데이터로 변경하기

        // 일괄추가
        bRepository.saveAll(boardList.getList());
        return "redirect:/board/select.do";
    }



@GetMapping(value = "/insertbatch.do")
    public ModelAndView insertbatch(){


        // 3개 항목을 생성함
        // List 타입을 바로 못받음
        List<Board> list = new ArrayList<Board>();
        for(int i=0; i<3; i++){
            Board board = new Board();
            list.add(board);
        }

        // BoardList 에 추가
        BoardList boardList = new BoardList();
        boardList.setList(list);
        return new ModelAndView("board_insertbatch", "obj", boardList);
    }

    @PostMapping(value = "/insertbatch.do")
    public String insertBatchPOST(
            @ModelAttribute BoardList boardList){
        System.out.println(boardList.getList().toString()); 
        // 일괄추가 수행
        bRepository.saveAll(boardList.getList());
        return "redirect:/board/select.do";
    }


    @PostMapping(value = "/deletebatch.do")
    public String deletebatch(
        @RequestParam(name="chk") List<Long> chk){
            System.out.println(chk.toString());
        bRepository.deleteAllById(chk);
        return "redirect:/board/select.do";
    }

그리고 html로 출력 하는지 확인 해봄.

templates/board_insertbatch.html

<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>일괄추가</title>
</head>

<body>
    <form th:action="@{/board/insertbatch.do}" method="post">
        <th:block th:each="tmp, idx : ${obj.list}">
            <!-- list [0].title, list [0].content, list [0].writer, -->
            <!-- list [0].title, list [0].content, list [0].writer, -->
            <!-- list [0].title, list [0].content, list [0].writer, -->
            <!-- 총 controller에서 3번 돌림 -->
        <input type="text" th:name="|list[${idx.index}].title|" />
        <input type="text" th:name="|list[${idx.index}].content|" />
        <input type="text" th:name="|list[${idx.index}].writer|" />
         <br />
        </th:block>
        <input type="submit" value="일괄추가" />
    </form>  
      
</body>
</html>

templates/board_select.html에 추가할 코드들.

   <!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">  
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>게시판글쓰기</title>
</head>

<body>

    <a th:href="@{/board/insert.do}"><button>글쓰기</button></a>
    <input type="button" value="일괄삭제" th:onclick="|javascript:handleBatch(1)|" /> <!--여기 추가한 부분-->
    <a th:href="@{/board/insertbatch.do}"><button>일괄추가</button></a>
    <input type="button" value="일괄수정" th:onclick="|javascript:handleBatch(2)|" /><!--여기 추가한 부분-->
    <hr />

    <table border="1">
        <tr th:each="obj, idx : ${list}">
            <td><input type="checkbox" class="chk" th:value="${obj.no}" /></td>
            <td th:text="${obj.no}"></td>
            <td>
                <a th:href="@{/board/selectone.do(no=${obj.no})}" 
                    th:text="${obj.title}"></a> 
            </td>
            <td th:text="${obj.writer}"></td>
            <td th:text="${obj.hit}"></td>
            <td th:text="${obj.regdate}"></td>
            <td>
                <form th:action="@{/board/delete.do}" method="post" style="display:inline-block">
                    <input type="hidden" name="no" th:value="${obj.no}" />
                    <input type="submit" value="삭제" />
                </form>
                <form th:action="@{/board/update.do}" method="get" style="display:inline-block">
                    <input type="hidden" name="no" th:value="${obj.no}" />
                    <input type="submit" value="수정" />
                </form>
                <form th:action="@{/boardimage/insert.do}" method="get" style="display:inline-block">
                    <input type="hidden" name="no" th:value="${obj.no}" />
                    <input type="submit" value="이미지등록" />
                </form>
            </td>
        </tr>
    </table>


    <script>
        const handleBatch = (type) => {
            // form 태그 생성
            const form = document.createElement("form");
            if(type == 1) {
                form.action="[[@{/board/deletebatch.do}]]";
                form.method="post";
            }
            else if(type == 2) {
                form.action="[[@{/board/updatebatch.do}]]";
                form.method="get";
            }
            form.style.display="none";
            
            // 위의 체크박스 전체 가져오기
            const chk = document.getElementsByClassName("chk");
            for(let i=0;i<chk.length;i++){
                if(chk[i].checked){ //체크된 항목만 찾기
                    const check = document.createElement("input");
                    check.type="checkbox";
                    check.name="chk";
                    check.value = chk[i].value;
                    check.checked=true;

                    form.appendChild(check); // 체크된 항목만 form에 추가
                }
            }
            // form을 body에 추가
            document.body.appendChild(form);
            // form 전송
            form.submit();
        }
    </script>
</body>
</html>

templates/board_updatebatch.html에 추가할 코드들.

<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>게시판글쓰기</title>
</head>

<body>
    <form th:action="@{/board/updatebatch.do}" method="post">
        <th:block th:each="tmp, idx : ${obj.list}">
            <input type="text" th:name="|list[${idx.index}].no|" th:value="${tmp.no}" readonly />
            <input type="text" th:name="|list[${idx.index}].title|" th:value="${tmp.title}" />
            <input type="text" th:name="|list[${idx.index}].content|" th:value="${tmp.content}" />
            <input type="text" th:name="|list[${idx.index}].writer|" th:value="${tmp.writer}" />
            <br />
        </th:block>
        <input type="submit" value="일괄수정" />
    </form>
</body>
</html>

출력 확인.

profile
갓 신생아 개발자 이야기

0개의 댓글