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 Console:http://localhost:8090/h2-console
數據庫查詢
http://www.springboottutorial.com/spring-boot-and-h2-in-memory-database