프로그래밍/SpringBoot

SpringBoot JPA Entity 테이블 자동 생성

duburani 2023. 3. 10. 09:40

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

 

application.yml

spring:
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: create
    defer-datasource-initialization: true
  h2:
    console:
      enabled: true
      path: /h2-console
  # Database Setting Info (Database를 H2로 사용하기 위해 H2연결 정보 입력)
  datasource:
    driver-class-name: org.h2.Driver  # Database를 H2로 사용하겠다.
    url: jdbc:h2:~/test  # H2 접속 정보
    username: sa  # H2 접속 시 입력할 username 정보 (원하는 것으로 입력)
    password:  # H2 접속 시 입력할 password 정보 (원하는 것으로 입력)

 

 

yml 파일에서 하이버네이트 ddl create 까지 들어가줘야 테이블 생성되고 datasource 디비 정보까지 입력해줘야 한다.

맥에서는 mem:test 가 아니라 ~/test 이니까 틀리지 않았는지 확인해줄것!!

그리고 들여쓰기 중요하다고 한다. !!

 

 

콘솔에 create table 찍히고 http://localhost:8088/h2-console 들어가서 확인해보면 member 생성된 것 확인할 수 있다.