Spring-boot使用schema.sql和data.sql填充H2数据库 [英] Spring-boot populate H2 database with schema.sql and data.sql
问题描述
我将Spring-boot设置为可与H2内存数据库一起使用
application.properties
文件位于/config
目录中
看起来,该文件已处理
I set up Spring-boot to work with H2 in-memory database
application.properties
file is in the /config
directory
and it looks like, this file is processed
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
下.
This file is processed and the console appears at /myconsole
But the schema.sql
and data.sql
are not processed and db is empty.
I placed schema.sql
and data.sql
files both under /config
and /src/main/resources
.
SQL语言说明正确无误,我可以使用控制台输入来填充表.
SQL language instructions are correct and I can populate the table using console input.
另一个奇怪的事情是 即使我将db命名为
Another strange thing is even though i name db as
spring.datasource.url=jdbc:h2:mem:mydb
spring控制台加载另一个数据库testdb
the spring console loads another database 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数据库?
How to load the H2 database correctly?
推荐答案
解决了该问题.
spring boot应用程序需要其自己的jdbc依赖项
The spring boot app requires its ownd jdbc dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
我所拥有的非引导依赖性还不够:
NON-boot dependency, that I had, is NOT enough alone:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
没有"spring-boot-starter-jdbc"依赖项 文件"application.properties"中的"spring.datasource.url"设置 未处理. 该文件实际上已处理,但jdbc设置未处理. Spring Boot将在内存中创建自己的testdb,这将破坏数据 关闭应用程序后.
Without the "spring-boot-starter-jdbc" dependency "spring.datasource.url" settings in file "application.properties" are not processed. That file is actually processed, but not the jdbc settings. Spring boot will create its own testdb in the memory, which destroys the data after closing the application.
这篇关于Spring-boot使用schema.sql和data.sql填充H2数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!