首页 文章

Spring Boot schema.sql - 重新启动时删除db模式

提问于
浏览
1

嗨,我正在使用Spring Boot 1.5.9版 .

当使用Spring Boot为mysql数据库初始化 schema.sql 时,它工作正常,数据库模式已成功创建 . 但是在重新启动应用程序时,这个 schema.sql 脚本再次执行,应用程序无法启动,因为表已经存在 .

我在 application.properties 中尝试了 spring.jpa.hibernate.ddl-auto=create-drop 选项,但它没有任何效果(可能因为它只适用于我没有使用的Hibernate实体)

如果数据库不在内存中,有没有办法让Spring Boot每次重启时都从_565483重新创建模式?

GitHub:https://github.com/itisha/spring-batch-demo/tree/database-input

3 回答

  • 4

    根据documentation,您可以通过将 spring.datasource.continue-on-error 属性设置为 true 来忽略异常

    Spring Boot默认启用Spring JDBC初始化程序的快速失败功能,因此如果脚本导致异常,则应用程序将无法启动 . 您可以使用spring.datasource.continue-on-error来调整它 .

    甚至将 spring.datasource.initialize 设置为 false 将其关闭

    您还可以通过将spring.datasource.initialize设置为false来禁用初始化 .

  • 0

    解决方法可能是,更改schema.sql中的create语句

    CREATE TABLE test .....
    

    CREATE TABLE IF NOT EXISTS test ...
    

    使用IF NOT EXISTS语句

  • 3

    关闭自动模式创建以避免冲突:在application.properties中添加此行

    spring.jpa.hibernate.ddl-auto=none
    

相关问题