Backend/Spring
[Spring] 댓글 삽입
Deeb
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을 수행한다
반응형