Spring boot 게시판 만들기 - entity 생성

YulHee Kim·2021년 10월 16일
0

개인으로 사이드 프로젝트를 개발하려한다.
사이드 프로젝트에 소 기능중 하나인 게시판을 시작으로 한번 스프링에 대해 공부해보겠다ㅎㅎ

⛳️ 게시판 만들기

- entity 구현하기

domain 패키지를 만들고 Board 패키지랑 클래스를 생성했다.

import lombok.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.time.LocalDateTime;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Board {

    @Id
    @GeneratedValue
    private Long id;

    @Column(length = 10, nullable = false)
    private String author;

    @Column(length = 100, nullable = false)
    private String title;

    @Column(columnDefinition = "TEXT", nullable = false)
    private String content;

    @CreatedDate
    @Column(updatable = false)
    private LocalDateTime createdDate;

    @LastModifiedDate
    private LocalDateTime modifiedDate;

    @Builder
    public Board(Long id, String author, String title, String content) {
        this.id = id;
        this.author = author;
        this.title = title;
        this.content = content;
    }
}

참고)
생성자 자동 생성
Lombok을 사용하면 생성자도 자동으로 생성할 수 있다.

  • @NoArgsConstructor : 파라미터가 없는 기본 생성자를 생성
  • @AllArgsConstructor : 모든 필드 값을 파라미터로 받는 생성자를 만들어줌
  • @RequiredArgsConstructor : final이나 @NonNull인 필드값만 파라미터로 받는 생성자를 만들어줌.

JPA Auditing 기능을 사용하기 위해 main 클래스에 @EnableJpaAuditing 어노테이션을 붙여주었다.

@EnableJpaAuditing
@SpringBootApplication
public class FitspoApplication {

	public static void main(String[] args) {
		SpringApplication.run(FitspoApplication.class, args);
	}

}

참고)
JPA Auditing r기능이란?
Java에서 ORM 기술인 JPA를 사용하여 도메인을 관계형 데이터베이스 테이블에 매핑할 때 공통적으로 도메인들이 가지고 있는 필드나 컬럼들이 존재한다. 대표적으로 생성일자, 수정일자, 식별자 같은 필드 및 컬럼이 있다.
그래서 JPA에서는 Audit이라는 기능을 제공하고 있다. Audit은 감시하다, 감사하다라는 뜻으로 Spring Data JPA에서 시간에 대해서 자동으로 값을 넣어주는 기능이다. 도메인을 영속성 컨텍스트에 저장하거나 조회를 수행한 후에 update를 하는 경우 매번 시간 데이터를 입력하여 주어야 하는데, audit을 이용하면 자동으로 시간을 매핑하여 데이터베이스의 테이블에 넣어주게 된다.

- Repository 구현하기

JpaRepository를 상속받는다.

board 패키지 안에 repository 패키지를 만들고 인터페이스를 만들었다.

import org.springframework.data.jpa.repository.JpaRepository;
import toypj.fitspo.domain.board.Board;

public interface BoardRepository extends JpaRepository<Board, Long> {
}

- DTO 구현하기

dto 패키지를 만들고 그 안에 클래스를 만들었다.

@Getter
@Setter
@ToString
@NoArgsConstructor
public class BoardDto {
    private Long id;
    private String author;
    private String title;
    private String content;
    private Long fileId;
    private LocalDateTime createdDate;
    private LocalDateTime modifiedDate;

    public Board toEntity() {
        Board build = Board.builder()
                .id(id)
                .author(author)
                .title(title)
                .content(content)
                .build();
        return build;
    }

    @Builder
    public BoardDto(Long id, String author, String title, String content, Long fileId, LocalDateTime createdDate, LocalDateTime modifiedDate) {
        this.id = id;
        this.author = author;
        this.title = title;
        this.content = content;
        this.fileId = fileId;
        this.createdDate = createdDate;
        this.modifiedDate = modifiedDate;
    }
}

참고한 블로그

profile
백엔드 개발자

0개의 댓글