본문 바로가기
개발/Spring Boot

스프링부트(Spring Boot) 게시판 만들기 #10 - 게시판 게시글 삭제

by chansungs 2023. 9. 27.
728x90
반응형

환경

- intellij, 자바11, mysql, mybatis, jsp 

 

수정을 만들었으니 삭제를 만들어보겠습니다.!!!

삭제를 만들고 등록만 하면 이제... 진짜 기본적인 crud가 끝나는군요! 너무 행복합니다.

 

NoticeController

/** 게시판 삭제 */
@DeleteMapping("/delete/{noticeId}")
@ResponseBody
public int deleteNotice(@PathVariable("noticeId") int noticeId) {
    System.out.println("deleteNotice noticeId ==> " + noticeId);
    return noticeService.deleteNotice(noticeId);
}

 

 

NoticeService

public int deleteNotice(int noticeId) {
    return noticeMapper.deleteNotice(noticeId);
}

 

NoticeMapper

int deleteNotice(int noticeId);

 

Notice.xml

<!-- 게시판 삭제 -->
<delete id="deleteNotice" parameterType="int">
    delete from study.notice where notice_id = #{noticeId}
</delete>

 

 

noticeDetail.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <meta charset="UTF-8">
    <title>게시판 상세</title>
</head>
<body>

    <table border="1" cellspacing="0" cellpadding="0" width="90%">
        <tr width="90%">
            <td width="10%" align="center">글쓴이</td>
            <td width="50%">${notice.createId}</td>
        </tr>
        <tr>
            <td align="center">제목</td>
            <td><input type="text" name="title" id="title" style="width: 95%;" value="${notice.title}"></td>
        </tr>
        <tr>
            <td align="center">내용</td>
            <td><textarea name="content" id="content" style="width: 95%;height: 200px;">${notice.content}</textarea></td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <button type="button" onclick="deleteNotice();">삭제</button>
                <button type="button" onclick="updateNotice();">수정</button>
                <button type="button" onclick="noticeListMove();">목록</button>
            </td>
        </tr>
    </table>

</body>
</html>
<script>

    // 목록이동
    function noticeListMove() {
        location.href = "/notice";
    }

    // 수정
    function updateNotice() {

        const reqData = {
            noticeId : '${notice.noticeId}',
            title : document.querySelector("#title").value,
            content : document.querySelector("#content").value
        }

        console.log(reqData);

        $.ajax({
            type: 'post',
            url: '/notice/detail',
            data: JSON.stringify(reqData),
            dataType : "json",
            contentType:"application/json",
            success: function (resData) {
                console.log(resData);
                if (resData == 1) {
                    alert("수정완료!");
                    location.reload();
                }
            },
            error: function (err) {
                console.log(err);
            }
        });
    }

    // 삭제
    function deleteNotice() {

        $.ajax({
            type: 'DELETE',
            url: '/notice/delete/' + '${notice.noticeId}',
            success: function (resData) {
                console.log(resData)
                if (resData === 1) {
                    alert("삭제성공");
                    location.href = "/notice";
                }
            },
            error: function (xhr, status, error) {
                console.error(error);
                // 에러 처리
            }
        });

    }

</script>

 

 

 

 

 

728x90
반응형