댓글 기능 구현!!!을 도전해보았당 :)
먼저 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
'혼자서 개발새발' 카테고리의 다른 글
저장용) JPA & QueryDsl 쓰는 법 (0) | 2022.12.20 |
---|---|
Spring) 게시판 보완(이전 글, 다음 글, 내 글에만 수정, 삭제 보이기 등) (0) | 2022.11.23 |
Spring) 게시판 글 수정을 만들어보자! (0) | 2022.11.23 |
Spring) 게시판에서 글을 삭제(이승기 아님) 해보자! (0) | 2022.11.22 |
Spring) 게시판 쓰기를 만들어보자! (0) | 2022.11.22 |