내가 만들 토이 프로젝트의 이름은 “명언 전송기”이지만, 명언이라기 보다는 독서할 때 인상깊었던 문장을 기록하고 다시 읽기 위함이다.
따라서 데이터베이스 구성을 아래와 같이 진행할 예정이다.

Package 생성
DTO 코드를 작성하기에 앞서 DTO들을 모아둘 패키지를 생성한다.
Control(Command) + N

Package name: com.tarashin.quoteSender.dto
DTO 생성
먼저 BookDTO부터 만들어보자.
BookDTO라는 클래스를 생성한다.

DTO 코드 작성
/dto/bookDTO.java
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import org.springframework.data.annotation.CreatedDate;
import lombok.Data;
@Entity
@Data
@Table(name="book")
@NoArgsConstructor
public class BookDTO {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String title;
@NotNull
private String author;
@NotNull
private String publisher;
@CreatedDate
@NotNull
private Date createdAt;
// 생성자
public BookDTO(@NotNull String title, @NotNull String author, @NotNull String publisher) {
super();
this.title = title;
this.author = author;
this.publisher = publisher;
}
}
@Entity: DB의 테이블과 1:1 매칭을 위함
@Data: 롬복 애노테이션 (@Getter, @Setter, @ToString, @EqualsAndHashCode, @RequiredArgsConstructor 모두 포함)
@Table: DB 테이블 이름값 설정
@NoArgsConstructor: 아무런 인자값을 가지지 않는 생성자 생성
application.properties
에 spring.jpa.hibernate.ddl-auto=create
설정을 해주었기 때문에 위와 같이 코드를 작성하고 실행하면 DB에 테이블이 생성되었음을 확인할 수 있다.

Annotation 종류나 DTO에 대한 개념은 검색해보면 많이 나오니 참고하면 좋다.
참고
quoteDTO도 작성한다.
/dto/quoteDTO.java
@Entity
@Data
@Table(name="quote")
public class QuoteDTO {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String text;
@CreatedDate
@NotNull
private Date createdAt;
@ManyToOne
@JoinColumn
private BookDTO book;
}
코드를 작성하고 실행하면 마찬가지로 테이블이 생성되었음을 확인할 수 있다.
