H2是基于java的内存型的数据库,可以选择以内存模式或者文件形式存储,内存形式每次程序关闭数据库则销毁
maven依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
配置文件:
spring.datasource.url = jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=123456
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
#可以访问h2数据库
spring.h2.console.enabled=true
#代表访问的地址 localhost:8080/h2-console 访问h2数据库
spring.h2.console.path=/h2-console
#防止跟踪输出
spring.h2.console.settings.trace=false
#防止其他ip访问
spring.h2.console.settings.web-allow-others=false
配置文件中需要注意的是你可以选择是以内存模式或者文件模式存储数据
下面是文件形式的存储配置:
jdbc:h2:file:/data/demo
下面的逻辑是保持数据库的开启,不要关闭了界面就关闭数据库连接
DB_CLOSE_ON_EXIT=FALSE
最后提及一点,如果不设置账号密码,则默认 sa 空 为账号密码。

网友评论