Spring/강의

[📙 숙련 Spring] 3-4. Entity 설계 실습

가지코딩 2025. 5. 21. 01:22

📙 목차

  1. 요구사항
  2. 프로젝트 준비
  3. BaseEntity 설계 및 적용하기
  4. 회원(Member) Entity 설계하기
  5. 게시글(Board) Entity 설계하기
  6. 연관관계 설정하기

1. 요구사항

게시판 프로젝트

회원(Member)
로그인에 사용할 아이디 : 필수, 유일
로그인에 사용할 비밀번호 : 필수
나이
생성 시간
수정 시간

게시글(Board)
제목 : 필수
내용 : 긴 텍스트
생성 시간
수정 시간

연관관계
한명의 회원은 여러개의 게시글을 작성할 수 있다. (단방향)

2. 프로젝트 준비

프로젝트 생성

 

 

Database 준비

  • board 스키마 생성

 

 

JPA 관련 설정

# application.properties

# DataSource 설정
spring.datasource.url=jdbc:mysql://localhost:3306/board
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# Hibernate 설정
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

3. BaseEntity 설계 및 적용하기

BaseEntity 클래스

필드명 타입 설명
createdAt LocalDateTime 생성 시간
updatedAt LocalDateTime 수정 시간

 

 

BaseEntity Entity 구현

/* /entity/BaseEntity.java */

@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {

    @CreatedDate
    @Column(updatable = false)
//    @Temporal(TemporalType.TIMESTAMP) 생략가능
    private LocalDateTime createdAt;

    @LastModifiedDate
    private LocalDateTime modifiedAt;
}

 

 

@EnableJpaAuditing 적용

/* BoardApplication.java */

@EnableJpaAuditing		// 추가
@SpringBootApplication
public class BoardApplication {
    public static void main(String[] args) {
        SpringApplication.run(JpaBoardApplication.class, args);
    }
}

4. 회원(Member) Entity 설계하기

Member 클래스

필드명 타입 설명
id Long 식별자
username String 로그인에 사용할 아이디
password String 로그인에 사용할 비밀번호
age Integer 나이
createdAt LocalDateTime 생성 시간
updatedAt LocalDateTime 수정 시간

 

 

Member Entity 구현

@Entity
@Table
public class Member extends BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, unique = true)
    private String username;

    @Column(nullable = false)
    private String password;

    private Integer age;
}

 

 

Application 실행 및 결과 확인


5. 게시글(Board) Entity 설계하기

Board 클래스

필드명 타입 설명
id Long 식별자
title String 제목
contents String 내용
createdAt LocalDateTime 생성 시간
updatedAt LocalDateTime 수정 시간

 

 

Board Entity 구현

@Entity
@Table(name = "board")
public class Board extends BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
		
    @Column(nullable = false)
    private String title;

    @Column(columnDefinition = "longtext")
    private String contents;

}

 

 

Application 실행 및 결과 확인


6. 연관관계 설정하기

한명의 회원은 여러개의 게시글을 작성할 수 있다. (N:1 단방향)

 

 

Board Entity

@Entity
@Table(name = "board")
public class Board extends BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String title;

    @Column(columnDefinition = "longtext")
    private String contents;

    @ManyToOne
    @JoinColumn(name = "member_id")
    private Member member;

}

 

 

Application 실행 및 결과 확인

 

 


실습 코드

https://github.com/gajicoding/spring-jpa-board/tree/v1.1.0

 

GitHub - gajicoding/spring-jpa-board

Contribute to gajicoding/spring-jpa-board development by creating an account on GitHub.

github.com