본문 바로가기
혼자서 개발새발

Spring) 댓글 기능 구현 - DB Table, DTO, DAO, Service, Mapper 만들기

by 휴일이 2022. 11. 23.

 

 

댓글 기능 구현!!!을 도전해보았당 :)

 

먼저 DB Table

 

 

 

cno 코멘트 번호

no 게시글 번호

pcno 부모 댓글 번호(대댓글 위해)

comment 댓글 내용

id 댓글쓴이

reg_date 댓글 작성 시각

up_date 댓글 수정 시각

 

 

 

 

DTO

package com.holiday.hi.domain;

import java.util.Date;

public class CommentDto {
    private Integer cno; // 댓글 번호
    private Integer no; // 댓글이 달리는 글 번호
    private Integer pcno; // 부모 댓글 번호
    private String comment; // 댓글 내용
    private String id; // 아이디
    private Date reg_date; // 작성 시간
    private Date up_date; // 새로 작성한 시간

    public CommentDto(int no, int pcno, String comment, String id) {
        this.no = no;
        this.pcno = pcno;
        this.comment = comment;
        this.id = id;
    }

    public CommentDto() {}

    public Integer getCno() {
        return cno;
    }

    public void setCno(Integer cno) {
        this.cno = cno;
    }

    public Integer getNo() {
        return no;
    }

    public void setNo(Integer no) {
        this.no = no;
    }

    public Integer getPcno() {
        return pcno;
    }

    public void setPcno(Integer pcno) {
        this.pcno = pcno;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Date getReg_date() {
        return reg_date;
    }

    public void setReg_date(Date reg_date) {
        this.reg_date = reg_date;
    }

    public Date getUp_date() {
        return up_date;
    }

    public void setUp_date(Date up_date) {
        this.up_date = up_date;
    }

    @Override
    public String toString() {
        return "CommentDto{" +
                "cno=" + cno +
                ", no=" + no +
                ", pcno=" + pcno +
                ", comment='" + comment + '\'' +
                ", id='" + id + '\'' +
                ", reg_date=" + reg_date +
                ", up_date=" + up_date +
                '}';
    }
}

 

 

 

 


DAO

package com.holiday.hi.dao;


import com.holiday.hi.domain.CommentDto;

import java.util.List;

public interface CommentDao {

    CommentDto select(Integer no) throws Exception;
    List<CommentDto> selectAll() throws Exception;
    int insert(CommentDto commentDto) throws Exception;
    int update(CommentDto commentDto) throws Exception;
    int delete(Integer cno, Integer no, String id) throws Exception;
    int deleteAll(Integer no) throws Exception;
    int count(Integer no) throws Exception;
}

 

 

Service

package com.holiday.hi.service;

import com.holiday.hi.domain.CommentDto;

import java.util.List;

public interface CommentService {

    CommentDto getComment(Integer cno) throws Exception;
    List<CommentDto> commentAll() throws Exception;
    int write(CommentDto dto) throws Exception;
    int modify(CommentDto dto) throws Exception;
    int remove(Integer cno, Integer no, String id) throws Exception;
    int removeAll(Integer no) throws Exception;
    int getCount(Integer no) throws Exception;
}

 

 

Mapper

<?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="com.holiday.hi.dao.CommentMapper">
    <select id="select" parameterType="Integer" resultType="CommentDto">
        SELECT *
        FROM comment
        WHERE no = #{no}
    </select>

    <select id="selectAll" resultType="CommentDto">
        SELECT * FROM comment
    </select>

    <insert id="insert" parameterType="CommentDto">
        INSERT INTO comment
            (no, pcno, comment, id, reg_date, up_date)
        VALUES
            (#{no}, #{pcno}, #{comment}, #{id}, now(), now())
    </insert>

    <update id="update" parameterType="CommentDto">
        UPDATE comment
        SET comment = #{comment}, up_date = now()
        WHERE cno = #{cno} and id = #{id}
    </update>

    <delete id="delete" parameterType="map">
        DELETE FROM comment WHERE cno = #{cno} AND id = #{id} AND no = #{no}
    </delete>

    <delete id="deleteAll" parameterType="Integer">
        DELETE FROM comment WHERE no = #{no}
    </delete>

    <select id="count" parameterType="int" resultType="int">
        SELECT count(*) FROM comment
        WHERE  no = #{no}
    </select>

</mapper>

 

 

 

 

Mapper 테스트 완료

Dao 테스트 완료

Service 테스트 완료

728x90