gpt4 book ai didi

java - 为什么 Spring Boot 2.0 应用程序不运行 schema.sql?

转载 作者:IT老高 更新时间:2023-10-28 13:49:15 25 4
gpt4 key购买 nike

当我使用 Spring Boot 1.5 时,在应用程序启动时,当设置了适当的配置时,Hibernate 会执行位于 /resources 文件夹中的 schema.sql 文件。在 Spring Boot 2.0 发布后,此功能不再起作用。我在文档中找不到有关此更改的任何信息。这是我的 application.properties 文件内容:

spring.datasource.url=...
spring.datasource.username=...
spring.datasource.password=...

#spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

Spring Boot 2.0 是否有一些变化或者这是一个错误/问题?

最佳答案

查看文档here .

In a JPA-based app, you can choose to let Hibernate create the schema or use schema.sql, but you cannot do both. Make sure to disable spring.jpa.hibernate.ddl-auto if you use schema.sql.

你有 spring.jpa.hibernate.ddl-auto=create-drop 这就是为什么 schema.sql 没有被执行。看起来这就是 Spring Boot 的工作方式。

编辑

我认为问题(不是真正的问题)是您的应用程序指向一个 mysql 实例。

current Spring Boot properties :

spring.datasource.initialization-mode=embedded # Initialize the datasource with available DDL and DML scripts.

默认值为 embedded - 例如仅当您正在运行和嵌入数据库时​​才进行初始化,例如 H2。

另见 Stephan here 的回答.他说:

Adding spring.datasource.initialization-mode=always to your project is enough.

所以尝试设置:

spring.datasource.initialization-mode=always

关于java - 为什么 Spring Boot 2.0 应用程序不运行 schema.sql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49438517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com