오늘은 spring boot 와 H2 DB 연동을 해보려고 한다.
보통 프로젝트 세팅을 할때 DB는 oracle, mysql 을 많이 해봤는데 H2 라는 DB는 처음이라 연동 도전해보려고 한다!
- pom.xml : h2 의존성 추가
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
- application.properties : h2 설정 config 입력
#H2 datasource 설정
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:tcp://localhost/~/(DB 이름)
spring.datasource.username=db 설치시 설정한 username
spring.datasource.password=db 설치시 설정한 password
#H2 jpa 설정
# H2 데이터베이스용 Hibernate dialect(JPA가 사용할 데이터베이스 플랫폼 지정)
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# 애플리케이션 시작시 엔티티 클래스를 기반으로 테이블 생성, 종료시 테이블 삭제
spring.jpa.hibernate.ddl-auto=create-drop
# 테이블 관련 SQL문 콘솔에 출력
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
#H2 console 설정
spring.h2.console.enabled=true
spring.h2.console.path=/h2
H2 연동은 여기서 끝! 실제로 스프링부트에서 사용하려면 Mybatis 연결 및 설정을 끝내야한다!
의존성과 application.properties 내용을 헷갈리셨던 분들에게도 도움이 될 수 있는 글이 될 수 있길
나는 maven 방식으로 세팅을 했지만 세팅을 찾아봤을때 gradle 방식도 동일하게 입력하면 연동된다!
'IT 개발 > BackEnd' 카테고리의 다른 글
[Eclipse/Spring] ModelAndView를 이용한 페이지 이동 방법 (1) | 2025.03.26 |
---|---|
[Eclipse/Spring] 프로젝트 세팅하기(JDK17/SPRING 6/tomcat 10) (2) | 2025.03.20 |
[SpringBoot] Spring Security 학습법 & 실무 적용 방법 (0) | 2025.03.07 |
[SpringBoot] JSP 파일 수정 후 새로고침시 적용 안될 경우 (3) | 2025.02.18 |
첫 프로젝트 환경 세팅하기 | 개발 초보의 시행착오 기록 (0) | 2025.02.18 |