【问题标题】:import initial data into spring boot app将初始数据导入 Spring Boot 应用程序
【发布时间】:2017-04-20 20:46:51
【问题描述】:

根据文档 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

当您拥有这两个文件但我没有架构时,此方法有效。我正在使用休眠生成数据库架构

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

以前的解决方法Importing Data with spring boot 是为 schema.sql 定义文件空白,但它不再起作用。我会得到文件不能是blank or null的异常。

在 spring 应用程序中我只是简单地定义了

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

它成功了。

有没有办法在 springBoot 中也有相同的行为?

【问题讨论】:

  • 嗨,这个问题有什么好运气吗?你解决了吗?
  • @pmanolov 是的,只需删除 application.properties 中的属性

标签: database hibernate spring-boot import spring-data-jpa


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    根据spring源代码-data.sql只有在schema.sql文件存在且不为空的情况下才会被调用。因此,如果您只需要来自data.sql 的脚本,请向schema.sql 添加一些简单的查询,例如

    SELECT 1;
    

    也在application.properties添加

    spring.datasource.initialization-mode=always
    

    因为默认值仅用于嵌入式数据源。

    【讨论】:

      猜你喜欢
      • 2021-08-15
      • 2018-09-07
      • 1970-01-01
      • 2018-02-14
      • 2018-11-13
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      相关资源
      最近更新 更多