1. 程式人生 > >Spring Boot 使用 H2 記憶體資料庫

Spring Boot 使用 H2 記憶體資料庫

H2 is one of the popular in memory databases.

H2 is a relational database management system written in Java. It can be embedded in Java applications or run in the client-server mode.

新增H2 POM依賴

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

建立Student實體


@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Student {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private String passportNumber;
}

啟用H2 Web Console

# Enabling H2 Console
spring.h2.console.enabled=true

SpringBoot中H2 DataSource

配置

# DataSource Configuration
spring.datasource.url=jdbc:h2:mem:cib
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto = update
spring.jpa.show-sql = true

資料庫查詢
在這裡插入圖片描述

http://www.springboottutorial.com/spring-boot-and-h2-in-memory-database