简体   繁体   English

将初始数据导入Spring Boot应用程序

[英]import initial data into spring boot app

According to the documentation http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html to import data on startup you can define these 2 properties inside application.properties: 根据文档http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html在启动时导入数据,您可以在application.properties中定义以下两个属性:

spring.datasource.data=data.sql
spring.datasource.schema=schema.sql

This works when you have both of these files, but i don't have schema. 当您同时拥有这两个文件时,此方法有效,但我没有架构。 I am generating db schema using hibernate 我正在使用休眠生成数据库模式

spring.jpa.hibernate.ddl-auto=create-drop

Previous workaround Importing Data with spring boot was to define file for schema.sql blank, but it doesn't work anymore. 先前的解决方法是使用Spring Boot导入数据,是为schema.sql空白定义文件,但此方法不再起作用。 I will get exception that file can't be blank or null . 我将得到文件不能为blank or null异常。

In spring application i simply defined 在春季应用中,我简单地定义了

<jdbc:initialize-database data-source="simpleDataSource">
    <jdbc:script location="classpath:dbscripts/data.sql"/>
</jdbc:initialize-database>

and it worked. 而且有效。

Is there a way to have same behaviour also in springBoot? springBoot中是否也有同样的行为?

只需删除schema.sql + config选项,Spring Boot应该会自动选择sata.sql

According to spring source code - data.sql will be invoked only if schema.sql file exist and it is not empty. 根据spring源代码-仅当schema.sql文件存在且不为空时,才会调用data.sql So if you only need scripts from data.sql add some simple query to schema.sql like 所以,如果你只从需要的脚本data.sql添加一些简单的查询schema.sql

SELECT 1;

Also in application.properties add 还要在application.properties添加

spring.datasource.initialization-mode=always

because default value is used only for embedded data sources. 因为默认值仅用于嵌入式数据源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM