【发布时间】:2017-08-22 03:54:00
【问题描述】:
当使用 Spring Boot JPA (Hibernate) 时,使用一些种子数据(在本例中为 MySQL)引导数据库的最佳方法是什么?例如,在 Grails 中,您可以使用 bootstrap.groovy 文件来完成此操作。对于最佳实践中的最佳解决方法,我无法达成真正的共识。
【问题讨论】:
标签: spring hibernate grails spring-boot spring-data-jpa
当使用 Spring Boot JPA (Hibernate) 时,使用一些种子数据(在本例中为 MySQL)引导数据库的最佳方法是什么?例如,在 Grails 中,您可以使用 bootstrap.groovy 文件来完成此操作。对于最佳实践中的最佳解决方法,我无法达成真正的共识。
【问题讨论】:
标签: spring hibernate grails spring-boot spring-data-jpa
您可以使用Liquibase 或Flyway 方法。
或以下Spring Boot 属性:
spring.datasource.data= # Data (DML) script resource references.
spring.datasource.schema= # Schema (DDL) script resource references.
另一种解决方案是创建自己的 Bootstrap bean 并在应用启动之前运行它,看看 Spring Boot 如何运行 Flyway 或 Liquibase 作为参考。
【讨论】:
您可以简单地在resources 文件夹中创建一个文件,将其命名为schema.sql,然后将您的 MySQL 数据库脚本放入其中。当您的应用程序启动时,schema.sql 将自动加载
【讨论】: