게시판에 댓글을 추가해본다.
대댓글은 나중에 해보고 우선 댓글부터..
우선 model 객체를 하나 만든다.
model 패키지에 Comment.java 로 하나만들었음.
package com.example.post.model; import lombok.Data; @Data public class Comment { private String commentId; private String contents; private String author; private String postId; private String date; }
댓글id (번호 자동생성),
contesnt : 댓글내용
author : 작성자
postId : 댓글이 작성된 글 번호
date : 작성시각
Comment.postId 와 Post.num 을 외래키로 참조할수있겠다.
나는 까먹고안함
그리고 controller 를 하나 작성한다.
package com.example.post.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.example.post.model.Comment; import com.example.post.service.CommentService; import jakarta.servlet.http.HttpServletRequest; @Controller @RequestMapping("/Comment") public class CommentController { @Autowired CommentService commentService; ModelAndView mav = new ModelAndView(); @PostMapping("/Write") public ModelAndView writeComment(@ModelAttribute Comment comment) { commentService.writeComment(comment); mav.setViewName("redirect:/Content?num="+comment.getPostId()); return mav; } @GetMapping("/Delete") public ModelAndView deletePost(HttpServletRequest request) { commentService.deleteComment(request.getParameter("commentId")); mav.setViewName("redirect:/Content?num="+request.getParameter("postId")); return mav; } @PostMapping("/Modify") public boolean modifyPost(@RequestBody Comment comment) { return true; } }
클래스 자체에 RequestMapping 어노테이션을 통해 /Comment 아래로 들어오는 url 들을 처리하기로 하였다.
IP:Port/Comment/Write 는 댓글 쓰기,
IP:Port/Write 는 글쓰기 로 보면 된다.
댓글 검색이나 리스트같은 기능은 구현하지 않을 것이므로 Read 기능은 제외하고 만들었음.
Service 도 기존 Post Service 내용와 크게 다르지 않다.
package com.example.post.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.post.dao.CommentDao; import com.example.post.model.Comment; @Service public class CommentService { @Autowired CommentDao commentDao; public void writeComment(Comment comment) { commentDao.writeComment(comment); } public List<Comment> selectCommentByPostId(String num) { return commentDao.selectCommentByPostId(num); } public void deleteComment(String commentId) { commentDao.deleteComment(commentId); } }
dao도 이름만다르지 구조 내용 똑같음
다만 PostMapper 가 아니라 CommentMapper 로 지정했다.
package com.example.post.dao; import java.util.List; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.example.post.model.Comment; @Repository public class CommentDao { @Autowired private SqlSessionTemplate sqlSession; public void writeComment(Comment comment) { sqlSession.insert("CommentMapper.insertComment", comment); } public List<Comment> selectCommentByPostId(String num) { return sqlSession.selectList("CommentMapper.selectCommentByPostId", Integer.parseInt(num)); } public void deleteComment(String commentId) { sqlSession.delete("CommentMapper.deleteComment", Integer.parseInt(commentId)); } public void deleteCommentByPostId(String postId) { sqlSession.delete("CommentMapper.deleteCommentByPostId", Integer.parseInt(postId)); } }
resource/mapper 아래 새로운 파일을 하나 더 만들었다.
이름은 mapper-comment.xml 로 만들었음.
mapper namespace 를 CommentMapper 로 지어서 위에 dao 도 CommentMapper 로 맞춰주었다.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="CommentMapper"> <insert id="insertComment" parameterType="com.example.post.model.Comment"> INSERT INTO COMMENTS1(Contents, Author, PostId) VALUES (#{contents}, #{author}, #{postId}) </insert> <select id="selectCommentByPostId" parameterType="int" resultType="com.example.post.model.Comment"> SELECT * FROM COMMENTS1 WHERE POSTID = #{num} </select> <delete id="deleteComment" parameterType="int"> DELETE FROM COMMENTS1 WHERE commentId = #{commentId} </delete> <delete id="deleteCommentByPostId" parameterType="int"> DELETE FROM COMMENTS1 WHERE postId = #{postId} </delete> </mapper>
SQL 내용은 CRUD 로 특이한것은 없음.
다만 selectCommentByPostId 라는 쿼리는 CommentController (Service) 에서 호출하지 않고 Post 쪽에서 호출하게끔 함.
왜냐면 게시글을 띄울때 아래 덧글도 보여주려고
기존 Post 쪽도 수정을해야하는데 다음 게시물에