我设置 Spring-boot 以使用 H2 内存数据库application.properties
文件位于 /config
目录
看起来,这个文件被处理了
spring.datasource.url=jdbc:h2:mem:mydb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.path=/myconsole
spring.h2.console.enabled=true
spring.datasource.initialize=true
spring.datasource.schema=schema.sql
spring.datasource.data=data.sql
这个文件被处理,控制台出现在 /myconsole
但是schema.sql
和 data.sql
未处理且 db 为空。我放置了
schema.sql
和 data.sql
/config
下的文件和 /src/main/resources
.SQL 语言指令是正确的,我可以使用控制台输入来填充表。
另一个奇怪的事情是
即使我将 db 命名为
spring.datasource.url=jdbc:h2:mem:mydb
spring 控制台加载另一个数据库 testdb
o.s.j.d.e.EmbeddedDatabaseFactory --- Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
如何正确加载H2数据库?
最佳答案
解决了问题。
spring boot 应用程序需要自己的 jdbc 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
我所拥有的非引导依赖项是不够的:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
没有“spring-boot-starter-jdbc”依赖
文件“application.properties”中的“spring.datasource.url”设置
不被处理。
该文件实际上已处理,但未处理 jdbc 设置。
spring boot会在内存中创建自己的testdb,破坏数据
关闭应用程序后。
关于Spring-boot 使用 schema.sql 和 data.sql 填充 H2 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37788029/