Deeb
디비의 DB
Deeb
전체 방문자
오늘
어제
  • 분류 전체보기 (243)
    • Frontend (63)
      • HTML & CSS (27)
      • JavaScript (17)
      • jQuery (8)
      • React (6)
    • Backend (98)
      • Java (19)
      • JDBC (2)
      • Servlet & JSP (13)
      • Spring (17)
      • Project (0)
      • 개발 공부 (11)
      • 문제 풀이 (8)
      • Algorithm (1)
      • DataBase (0)
      • Oracle (18)
      • Error (8)
    • Knou (1)
    • Review (14)
    • TIL (33)
    • 삽질기록 (8)
    • deebtionary (5)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 정의
  • 서평단
  • 한빛미디어
  • 정처기
  • 기초
  • 자바
  • 삭제
  • GIT
  • 2학기
  • 함수
  • 리액트
  • 방통대
  • 후기
  • DB
  • 에러
  • DBMS
  • 클래스
  • 책
  • 공부
  • css
  • Java
  • 추천
  • For
  • 배열
  • HTML
  • js
  • 다형성
  • CLASS
  • alter
  • 방송대

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Deeb

디비의 DB

Backend/Spring

[Spring] 댓글 삽입

2022. 1. 19. 17:47

reply.js

// 댓글 등록
function addReply() {
    
    // 게시글 번호(boardNo), 로그인한 회원 번호(loginMemberNo), 댓글 내용

    if(loginMemberNo == ""){ // 로그인이 되어 있지 않은 경우
        alert("로그인 후 이용해 주세요.");
  
    }else{ // 로그인한 경우



        // 댓글 미작성한 경우
        if( $("#replyContent").val().trim().length == 0  ){
            alert("댓글을 작성한 후 버튼을 클릭해주세요.");
            $("#replyContent").focus();
     
        }else{ // 댓글을 작성한 경우


            $.ajax({
                url : contextPath + "/reply/insert",
                data : {"memberNo" : loginMemberNo,  
                        "boardNo" : boardNo, 
                        "replyContent" : $("#replyContent").val()  },     
                type : "POST",
                success : function(result){
                    console.log(result);

                    if(result > 0){
                        alert("댓글 삽입 성공");
                        $("#replyContent").val(""); // 작성한 댓글 내용 삭제

                        selectReplyList(); // 댓글 조회 함수를 호출하여 댓글 화면 다시 만들기
                    }else{
                        alert("댓글 삽입 실패");

                    }

                },

                error : function(req, status, error){
                    console.log("댓글 삽입 실패");
                    console.log(req.responseText);
                }
            });

        }

    }

}

 

ReplyController

// 댓글 삽입
@RequestMapping(value = "insert", method = RequestMethod.POST)
public int insertReply(Reply reply) {

    return service.insertReply(reply);
}

ReplyService

/** 댓글 삽입 
 * @param reply
 * @return result
 */
int insertReply(Reply reply);

ReplyServiceImpl

// 댓글 삽입
@Override
public int insertReply(Reply reply) {
    // 크로스 사이트 방지, 개행문자 처리
    reply.setReplyContent( Util.XSS(reply.getReplyContent()));
    reply.setReplyContent( Util.changeNewLine(reply.getReplyContent()));

    return dao.insertReply(reply);
}

ReplyDAO

/** 댓글 삽입
 * @param reply
 * @return result
 */
public int insertReply(Reply reply) {
    return sqlSession.insert("replyMapper.insertReply", reply);
}

reply-mapper

<!-- 댓글 삽입  / resultType도 _int라서 생략 -->
<insert id="insertReply">
    INSERT INTO REPLY 
    VALUES( SEQ_REPLY_NO.NEXTVAL, #{replyContent}, DEFAULT,
    #{boardNo}, #{memberNo}, DEFAULT, 

        <if test="parentReplyNo == 0">
            NULL
        </if>
        <if test="parentReplyNo != 0">
            #{parentReplyNo}
        </if>
    )
</insert>

- mapper에서

#{parentReplyNo} 를 받아오는데!!

일반 댓글은 이게 0이들어가는데 댓글번호는 1부터 시작하는데 0이 들어가면 FK제약조건 위배가 되어버린다

fk- 부모컬럼의 값 + NULL -> 컬럼값이 0이니 NULL로 바꾸는 동적 SQL을 수행한다

 

 

반응형
저작자표시 비영리 변경금지 (새창열림)

'Backend > Spring' 카테고리의 다른 글

Git에서 프로젝트 받기 : Project from Git(with smart import)  (0) 2022.02.25
[Spring] 댓글 목록 조회  (0) 2022.01.19
[Spring] 댓글 테이블, 클래스 구조 생성  (0) 2022.01.19
[Spring] 게시판 글 삭제  (0) 2022.01.19
[Spring] 게시글 글 수정  (0) 2022.01.19
    'Backend/Spring' 카테고리의 다른 글
    • Git에서 프로젝트 받기 : Project from Git(with smart import)
    • [Spring] 댓글 목록 조회
    • [Spring] 댓글 테이블, 클래스 구조 생성
    • [Spring] 게시판 글 삭제
    Deeb
    Deeb

    티스토리툴바