Spring-boot使用schema.sql和data.sql填充H2数据库

use*_*431 10 h2 spring-jdbc spring-boot

我将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
Run Code Online (Sandbox Code Playgroud)

处理此文件并且控制台显示在/ myconsole但是不处理schema.sql和data.sql并且db是emplty.我在/ config和/ src/main/resouces下放置了schema.sql和data.sql文件

SQL语言指令是正确的,我可以使用控制台输入填充表.

另一个奇怪的事情是,即使我将db命名为spring.datasource.url = jdbc:h2:mem:mydb spring控制台加载另一个数据库testdb osjdeEmbeddedDatabaseFactory ---启动嵌入式数据库:url ='jdbc:h2:mem:testdb; DB_CLOSE_DELAY = -1; DB_CLOSE_ON_EXIT = false',用户名='sa'

如何正确加载H2数据库?提前致谢 ....

use*_*431 8

解决了这个问题.

Spring启动应用程序需要其拥有的jdbc依赖项

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我所拥有的非启动依赖性并不足够:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

如果没有"spring-boot-starter-jdbc"依赖关系,则不会处理文件"application.properties"中的"spring.datasource.url"设置.该文件实际上已处理,但不是jdbc设置.Spring启动会在内存中创建自己的testdb,在关闭应用程序后会破坏数据.