[Spring] JPA 연결

이다혜·2023년 11월 14일
0

Spring

목록 보기
4/27
post-thumbnail

출처 : 점프 투 스프링부트

우선 데이터베이스를 설치해야한다.

소규모 프로젝트의 경우 H2 데이터베이스를 사용한다.

데이터베이스 연결

h2 데이터베이스 설치

build.gradle 파일에 아래의 코드를 추가한다.

runtimeOnly 'com.h2database:h2'

데이터베이스 설정

application.properties에 아래의 코드를 추가한다.

# DATABASE
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:tcp://localhost/~/sbb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

그 후 url 위치에 sbb.mv.db 파일을 생성해야한다.

H2 콘솔을 통해 데이터베이스 접속

http://localhost:8080/h2-console

JPA 환경설정

build.gradle을 수정한다.

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

applicatoin.properties를 수정한다.

# JPA
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update

0개의 댓글