Spring Boot

댓글 시스템 데이터베이스(MySQL) 설계 및 MyBatis를 이용한 댓글 수 조회

청춘고양이 2024. 6. 24. 11:07
반응형

데이터베이스 설계

댓글 시스템을 위한 데이터베이스 테이블을 설계할 때, 다음과 같은 요구 사항을 고려할 수 있습니다:

  • 댓글은 특정 게시물에 속할 수 있습니다.
  • 댓글은 다른 댓글에 대한 대댓글일 수 있습니다.
  • 댓글 작성자 정보가 포함됩니다.
  • 댓글에는 좋아요 수, 삭제 여부, 깊이 등의 정보가 포함됩니다.

아래는 댓글 테이블에 대한 SQL 생성 스크립트입니다.

CREATE TABLE IF NOT EXISTS comment (
    comment_id BIGINT AUTO_INCREMENT PRIMARY KEY,
    text_body VARCHAR(1000),
    parent_post_id BIGINT,
    parent_comment_id BIGINT,
    member_id BIGINT,
    likes BIGINT UNSIGNED,
    is_comment_for_comment BOOLEAN,
    deleted_time DATETIME,
    deleted_true BOOLEAN,
    depth INT,
    order_number BIGINT UNSIGNED,
    FOREIGN KEY (parent_post_id) REFERENCES post(id),
    FOREIGN KEY (parent_comment_id) REFERENCES comment(comment_id),
    FOREIGN KEY (member_id) REFERENCES member(id)
);

각 필드 설명

  • comment_id: 댓글의 고유 ID.
  • text_body: 댓글의 본문.
  • parent_post_id: 댓글이 달린 게시물의 ID.
  • parent_comment_id: 대댓글일 경우 부모 댓글의 ID.
  • member_id: 댓글 작성자의 ID.
  • likes: 댓글의 좋아요 수.
  • is_comment_for_comment: 대댓글 여부를 나타내는 불린 값.
  • deleted_time: 댓글이 삭제된 시간.
  • deleted_true: 댓글의 삭제 여부를 나타내는 불린 값.
  • depth: 댓글의 깊이.
  • order_number: 댓글의 순서.

Java에서 댓글 엔터티 정의

댓글 테이블에 대응하는 Java 엔터티 클래스를 정의합니다. BIGINT 타입은 Java의 long 타입과 매핑됩니다. null을 허용해야 하는 경우 Long 타입을 사용할 수 있습니다.

import java.time.LocalDateTime;

public class Comment {
    private long commentId;
    private String textBody;
    private Long parentPostId; // Nullable
    private Long parentCommentId; // Nullable
    private Long memberId; // Nullable
    private long likes;
    private boolean isCommentForComment;
    private LocalDateTime deletedTime;
    private boolean deletedTrue;
    private int depth;
    private long orderNumber;

    // Getter와 Setter 메서드...
}

댓글 저장 로직 예제

Comment 객체를 생성하고 저장할 때, 부모 댓글이 있는지 여부에 따라 다양한 필드를 설정할 수 있습니다.

public void saveComment(Comment comment, Post parentPost, Comment parentComment) {
    if (parentComment == null) { // 부모 댓글이 없을 때
        comment.setIsCommentForComment(false); // 대댓글이 아니다
        comment.setDepth(0); // 깊이는 0
        comment.setOrderNumber(parentPost.getCommentCount()); // 새로운 순서
    } else { // 부모 댓글이 존재할 때
        comment.setIsCommentForComment(true); // 대댓글이다
        comment.setDepth(parentComment.getDepth() + 1); // 깊이는 부모의 깊이 + 1
        comment.setOrderNumber(parentComment.getOrderNumber()); // 순서는 부모의 순서를 그대로 물려받는다.
    }
    // Comment 저장 로직...
}

전체 댓글 수 조회 (MyBatis)

MyBatis를 사용하여 전체 댓글 수를 조회하는 방법은 다음과 같습니다.

CommentMapper.xml

MyBatis XML 파일에 전체 댓글 수를 조회하는 쿼리를 작성합니다.

CommentMapper 인터페이스

Java 인터페이스에 매핑 메서드를 정의합니다.

// com.example.mapper.CommentMapper

public interface CommentMapper {

    // 전체 댓글 수 조회
    long countAllComments();
}

서비스 클래스에서 댓글 수 조회

서비스 클래스에서 CommentMapper를 주입받아 전체 댓글 수를 조회합니다.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommentService {

    private final CommentMapper commentMapper;

    @Autowired
    public CommentService(CommentMapper commentMapper) {
        this.commentMapper = commentMapper;
    }

    public long getTotalCommentCount() {
        return commentMapper.countAllComments();
    }
}

이렇게 하면, MyBatis를 사용하여 전체 댓글 수를 조회할 수 있습니다. 이를 통해 데이터베이스의 댓글 수를 효율적으로 관리하고 조회할 수 있습니다.

반응형