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

啓動應用程序,訪問H2 Web Consolehttp://localhost:8090/h2-console
在這裏插入圖片描述

數據庫查詢
在這裏插入圖片描述


http://www.springboottutorial.com/spring-boot-and-h2-in-memory-database
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章