본문 바로가기
개발/Spring Boot

스프링부트(Spring Boot) 게시판 만들기 #9 - 게시판 상세 업데이트

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

환경

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

 

지난번 포스팅에 상세를 만들었으니 상세에 대한 업데이트를 만들어보겠습니다.

 

우선 수정을 하면 수정시간을 넣어주는 컬럼을 추가하겠습니다.

ALTER table study.notice ADD update_at timestamp;

 

 

업데이트 소스를 만들어보겠습니다!!

 

NoticeController

/** 게시판 수정 */
@RequestMapping(value = "/detail", method = RequestMethod.POST)
@ResponseBody
public int updateDetail(@RequestBody NoticeRequest noticeRequest) {
    System.out.println("updateDetail noticeRequest ==> " + noticeRequest);
    return noticeService.updateDetail(noticeRequest);
}

 

NoticeService

public int updateDetail(NoticeRequest request) {
    return noticeMapper.updateDetail(request);
}

 

NoticeMapper

int updateDetail(NoticeRequest request);

 

NoticeRequest

package com.example.springbasic.notice.dto.request;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

@Getter
@ToString
public class NoticeRequest {

    private int noticeId;
    private String title;
    private String content;
    private String createId;
    private String createAt;
    private String updateAt;

}

 

 

Notice.xml

<!-- 게시판 수정 -->
<update id="updateDetail" parameterType="com.example.springbasic.notice.dto.request.NoticeRequest">
    update study.notice
    set
        title = #{title}
        ,content = #{content}
        ,update_at = CURRENT_TIMESTAMP()
    where 1=1
        and notice_id = #{noticeId}
</update>

 

 

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="noticeDetail();">수정</button>
                <button type="button" onclick="noticeListMove();">목록</button>
            </td>
        </tr>
    </table>

</body>
</html>
<script>

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

    // 수정
    function noticeDetail() {

        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);
            }
        });


    }

</script>

 

 

 

 

 

자세한 내용은 깃헙을 확인하시기 바랍니다.

 

감사합니다.

 

 

 

728x90
반응형