Spring(수업 정리 #9)bbs5

최동민·2022년 6월 4일
0

Spring 수업정리

목록 보기
13/47

boards , articles, comments DB생성



BbsBoardEntity
BbsArticleEntity
BbsCommentEntity
만들기

package dev.dmchoi.bbs.entities;

import dev.dmchoi.bbs.interfaces.IEntity;

public class BbsBoardEntity implements IEntity<BbsBoardEntity> {
    private int index; // 게시판 번호
    private String id; // notice
    private String name; // 공지사항 등 게시판 이름
    private int listLevel; //회원등급
    private int readLevel; // 읽을권한
    private int writeLevel; // 작성권한
    private int commentLevel; //댓글권한

    // 빈 생성자
    public BbsBoardEntity() {
    }
    //자기 자신 생성자
    public BbsBoardEntity(BbsBoardEntity bbsBoardEntity) {
        this.copyValuesOf(bbsBoardEntity);
    }
    //매개변수 모두 있는 생성자
    public BbsBoardEntity(int index, String id, String name, int listLevel, int readLevel, int writeLevel, int commentLevel) {
        this.index = index;
        this.id = id;
        this.name = name;
        this.listLevel = listLevel;
        this.readLevel = readLevel;
        this.writeLevel = writeLevel;
        this.commentLevel = commentLevel;
    }

    // 게터 세터
    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getListLevel() {
        return listLevel;
    }

    public void setListLevel(int listLevel) {
        this.listLevel = listLevel;
    }

    public int getReadLevel() {
        return readLevel;
    }

    public void setReadLevel(int readLevel) {
        this.readLevel = readLevel;
    }

    public int getWriteLevel() {
        return writeLevel;
    }

    public void setWriteLevel(int writeLevel) {
        this.writeLevel = writeLevel;
    }

    public int getCommentLevel() {
        return commentLevel;
    }

    public void setCommentLevel(int commentLevel) {
        this.commentLevel = commentLevel;
    }


    @Override
    public BbsBoardEntity clone() {
        BbsBoardEntity entity = new BbsBoardEntity();
        entity.index = this.index;
        entity.id = this.id;
        entity.name = this.name;
        entity.listLevel = this.listLevel;
        entity.readLevel = this.readLevel;
        entity.writeLevel = this.writeLevel;
        entity.commentLevel = this.commentLevel;
        return entity;
    }

    @Override
    public void copyValuesOf(BbsBoardEntity bbsBoardEntity) {
        this.index = bbsBoardEntity.index;
        this.id = bbsBoardEntity.id;
        this.name = bbsBoardEntity.name;
        this.listLevel = bbsBoardEntity.listLevel;
        this.readLevel = bbsBoardEntity.readLevel;
        this.writeLevel = bbsBoardEntity.writeLevel;
        this.commentLevel = bbsBoardEntity.commentLevel;
    }

//    @Override
//    public boolean equals(Object obj) {
//        if(!(obj instanceof BbsBoardEntity)) {
//            return false;
//        }
//        BbsBoardEntity bbsBoardEntity = (BbsBoardEntity) obj;
//        return this.index == bbsBoardEntity.index;
//    }
}
package dev.dmchoi.bbs.entities;

import dev.dmchoi.bbs.interfaces.IEntity;

import java.util.Date;

public class BbsArticleEntity implements IEntity<BbsArticleEntity> {
    private int index; // 게시글 번호
    private String boardId; // boardId = BbsBoardEntity.id / notice
    private String userEmail; // 게시글 작성자 이메일 / 1@sample.com 등
    private String title; // 게시글 제목
    private String content; // 게시글 내용
    private Date writtenAt; // 작성일자
    private int view; // 조회수

    // 빈 생성자
    public BbsArticleEntity() {
    }
    // 자기 자신 생성자
    public BbsArticleEntity(BbsArticleEntity bbsArticleEntity) {

        this.copyValuesOf(bbsArticleEntity);
    }

    // 매개변수 모두 있는 생성자
    public BbsArticleEntity(int index, String boardId, String user_email, String title, String content, Date writtenAt, int view) {
        this.index = index;
        this.boardId = boardId;
        this.userEmail = user_email;
        this.title = title;
        this.content = content;
        this.writtenAt = writtenAt;
        this.view = view;
    }
    // 게터 세터
    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getBoardId() {
        return boardId;
    }

    public void setBoardId(String boardId) {
        this.boardId = boardId;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getWrittenAt() {
        return writtenAt;
    }

    public void setWrittenAt(Date writtenAt) {
        this.writtenAt = writtenAt;
    }

    public int getView() {
        return view;
    }

    public void setView(int view) {
        this.view = view;
    }


    @Override
    public BbsArticleEntity clone() {
        BbsArticleEntity entity = new BbsArticleEntity();
        entity.index = this.index;
        entity.boardId = this.boardId;
        entity.userEmail = this.userEmail;
        entity.title = this.title;
        entity.content = this.content;
        entity.writtenAt = this.writtenAt;
        entity.view = this.view;

        return entity;
    }

    @Override
    public void copyValuesOf(BbsArticleEntity bbsArticleEntity) {
        this.index = bbsArticleEntity.index;
        this.boardId = bbsArticleEntity.boardId;
        this.userEmail = bbsArticleEntity.userEmail;
        this.title = bbsArticleEntity.title;
        this.content = bbsArticleEntity.content;
        this.writtenAt = bbsArticleEntity.writtenAt;
        this.view = bbsArticleEntity.view;

    }

//    @Override
//    public boolean equals(Object obj) {
//        if(!(obj instanceof BbsArticleEntity)) {
//            return false;
//        }
//        BbsArticleEntity bbsArticleEntity = (BbsArticleEntity) obj;
//        return this.index == bbsArticleEntity.index;
//    }
}
package dev.dmchoi.bbs.entities;

import dev.dmchoi.bbs.interfaces.IEntity;

import java.util.Date;

public class BbsCommentEntity implements IEntity<BbsCommentEntity> {

    private int index; // 댓글 번호
    private int articleIndex; // 게시글 번호 / articleIndex = articles.index
    private String userEmail; // 댓글작성자 이메일
    private String content; // 댓글 내용
    private Date writtenAt; // 작성일자

    // 빈 생성자
    public BbsCommentEntity() {
    }

    // 자기 자신 생성자
    public BbsCommentEntity(BbsCommentEntity bbsCommentEntity) {
        this.copyValuesOf(bbsCommentEntity);
    }

    //매개변수 모두 있는 생성자
    public BbsCommentEntity(int index, int articleIndex, String userEmail, String content, Date writtenAt) {
        this.index = index;
        this.articleIndex = articleIndex;
        this.userEmail = userEmail;
        this.content = content;
        this.writtenAt = writtenAt;
    }

    //게터 세터
    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getArticleIndex() {
        return articleIndex;
    }

    public void setArticleIndex(int articleIndex) {
        this.articleIndex = articleIndex;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getWrittenAt() {
        return writtenAt;
    }

    public void setWrittenAt(Date writtenAt) {
        this.writtenAt = writtenAt;
    }


    @Override
    public BbsCommentEntity clone() {
        BbsCommentEntity entity = new BbsCommentEntity();
        entity.index = this.index;
        entity.articleIndex = this.articleIndex;
        entity.userEmail = this.userEmail;
        entity.content = this.content;
        entity.writtenAt = this.writtenAt;
        return entity;
    }

    @Override
    public void copyValuesOf(BbsCommentEntity bbsCommentEntity) {
        this.index = bbsCommentEntity.index;
        this.articleIndex = bbsCommentEntity.articleIndex;
        this.userEmail = bbsCommentEntity.userEmail;
        this.content = bbsCommentEntity.content;
        this.writtenAt = bbsCommentEntity.writtenAt;
    }

//    @Override
//    public boolean equals(Object obj) {
//        if(!(obj instanceof BbsCommentEntity)) {
//            return false;
//        }
//        BbsCommentEntity bbsCommentEntity = (BbsCommentEntity) obj;
//        return this.index == bbsCommentEntity.index;
//    }


}

BbsBoardListVo 생성

BbsBoardListResult 생성

BbsController 에서 BbsBoardListVo 객체화

list/앞에 notice입력하면 콘솔에 그대로 찍힌다.

BbsDao 생성 후 selectBoard 추가

BbsService 생성 후 추가

BbsBoardListVo 변수 추가 그리고 getter,setter도 만든다.

BbsService에 내용 추가 및
맨 아래 SUCCESS에 int totalArticleCount; 추가

BbsDao 에서 selectArticleCount 추가

만약
ARTICLE_COUNT_PER_PAGE = 10
totalArticleCount = 101
minpage = 1
maxPage = (totalArticleCount / ARTICLE_COUNT_PER_PAGE + (totalArticleCount % ARTICLE_COUNT_PER_PAGE == 0 ? 0 : 1)
=> 11

BbsService 내용 추가

DB에서 SELECT 만들고

BbsDao 에서 selectArticle 추가

BbsBoardListVo 에서 추가 후
selectArticle에서 이 구문이 가능케 한다 (null이 안뜨게된다. )

  bbsBoardListVo.getArticles().add(article); 

최종적으로 BbsService 에서

BbsController에서

list.html에서 타임리프 추가

짠.

타임리프로 객체를 받아왔고 html이 동적이 되었다!

profile
코드를 두드리면 문이 열린다

0개의 댓글