[Spring] Spring에서 테이블 바로 생성하기

세상을 바꾸는 개발자·2023년 3월 17일
0
  • application.yml에서 DB 연동을 해준 후
spring:
  thymeleaf:
    cache: false # 타임리프 캐시 끄기
    prefix: file:src/main/resources/templates/ # 타임리프 캐시 끄기(이 설정을 해야 꺼짐)
  devtools:
    livereload:
      enabled: true
    restart:
      enabled: true
  datasource:
    driver-class-name: org.mariadb.jdbc.Driver
    url: jdbc:mariadb://127.0.0.1:3306/<미리 생성한 DB 이름>?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=Asia/Seoul
    username: <DB username>
    password: <DB password>
  jpa:
    hibernate:
      ddl-auto: create # DB 테이블 자동생성(엔티티 클래스만 만들면 됨)



  • 엔티티 클래스 생성
    - 클래스 하나 생성 후, @Entity를 붙여주면 된다.
    • primary key 설정할 변수 위에 @Id를 붙여준다.
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class Article {
    @Id
    private long id;
    private String title;
    private String body;
}



  • DB에 들어가보면



  • 완료!

profile
초심 잃지 않기

0개의 댓글