본문 바로가기

Programming/Spring Boot

[Spring Boot] H2 Database 설치 및 접속

H2 DB

자바 기반의 오픈소스 관계형 데이터 베이스 관리 시스템(RDBMS)

Spring Boot가 지원하는 인메모리 데이터베이스

따로 설치가 필요 없으며, 어플리케이션을 재시작할 때마다 초기화

로컬 테스트 환경에서 많이 사용함

 

 

 

Spring Boot H2 DB 설치 및 접속하기 

1. gradle dependency 추가

     - build.gradle

dependencies {
	runtimeOnly 'com.h2database:h2'
}

 

 

2. application.properties 설정 추가

# h2 사용 여부
spring.h2.console.enabled=true

# h2 console 접속 path
spring.h2.console.path=/h2

# jpa sql 출력 여부
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.dialect.storage_engine=innodb

# h2 jdbc-url 고정
spring.datasource.hikari.jdbc-url=jdbc:h2:mem:testdb;MODE=MYSQL

spring.h2.console.path=/h2

h2 console 접속 주소 고정


spring.datasource.hikari.jdbc-url=jdbc:h2:mem:testdb;MODE=MYSQL

h2 JDBC URL jdbc:h2:mem:testdb로 고정

 

 

3. h2 console 접속

localhost:port번호/h2

 

 

3. h2 db table 생성

@Getter
@Builder
@Entity(name="member")
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MemberEntity implements UserDetails {
    // 회원 Id
    @Id
    @Column(length = 20)
    private String userId;
    
    // 비밀번호
    @NotNull
    @Column(length = 100)
    private String password;
    
    // 회원 이름
    @NotNull
    @Column(length = 60)
    private String name;
    
    // 주민등록번호
    @NotNull
    @Column(length = 30)
    private String regNo;
}

@Entity 어노테이션을 이용하여 h2 db에 table 생성

 

 

 

 

 

reference

     -  나는 nginx 설정이 정말 싫다구요

        https://atoz-develop.tistory.com/entry/H2-Database-설치-서버-실행-접속-방법

     -  [H2DB] H2 Database란? H2DB 설치 방법

        https://dololak.tistory.com/285

     -  스프링부트 + JPA + H2 데이터베이스

        https://youtu.be/WZDth-5KP5M