1. 패키지 구조 변경

- 게시판 패키지 생성 : 게시판 컨트롤러 - 사용자 패키지 생성 : 사용자 컨트롤러
2. User 엔티티 만들기
테이블을 생성할 때 자바 객체로 만들 수 있다. 이때 @Entity를 사용한다.
package shop.mtcoding.blog.user;
import jakarta.persistence.*;
import lombok.Data;
import java.time.LocalDateTime;
// user로 테이블명을 만들면, 키워드여서 안만들어질 수 있다. _tb 컨벤션 지키자.
@Table(name="user_tb")
@Data
@Entity //중요함!
public class User { // use_tb의 내용 DB에 담기
@Id // PK
@GeneratedValue(strategy = GenerationType.IDENTITY) // auto_increment
private int id;
@Column(unique=true) // 유니크 설정
private String username;
@Column(length = 60, nullable = false) // 길이 조정, 널 불가
private String password;
private String email;
// 카멜 표기법으로 만들면 DB는 created_at 으로 만들어진다. (언더스코어 기법)
private LocalDateTime createdAt;
}
아직 테이블이 만들어지지 않음.
왜 테이블 생성 쿼리가 안보이지?

3. yml에 테이블 생성 설정하기
[ application-dev.yml ]
server:
servlet:
encoding:
charset: utf-8
force: true
session:
timeout: 30m
port: 8080
spring:
mustache:
servlet:
expose-session-attributes: true
expose-request-attributes: true
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:test;MODE=MySQL
username: sa
password:
h2:
console:
enabled: true
jpa:
hibernate:
ddl-auto: create
show-sql: true
properties:
hibernate:
format_sql: true
복붙하면 안 되고… 수기로 입력하니까 테이블이 콘솔창에 뜬다…
띄어쓰기나 그런 문제가 있는 듯 ㅜㅜ


4. DB로 확인하기

스프링이 실행될 때 전체 스캔 → @Entity 검색 → 있는 전체 클래스 찾기
→ 테이블 생성 쿼리 생성하고 실행
Share article