Spring boot 在SpringBoot2中,如何自动生成数据库并将模式生成命令记录到文件中?

Spring boot 在SpringBoot2中,如何自动生成数据库并将模式生成命令记录到文件中?,spring-boot,maven,jpa,spring-data-jpa,spring-boot-2,Spring Boot,Maven,Jpa,Spring Data Jpa,Spring Boot 2,我正在使用SpringBoot2.1和Java11。我正在使用Maven构建我的工件。在本地运行时,我喜欢Spring JPA指令,它允许我自动创建数据库 spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.show-sql=true 我也喜欢指令,让我自动创建文件 s

我正在使用SpringBoot2.1和Java11。我正在使用Maven构建我的工件。在本地运行时,我喜欢Spring JPA指令,它允许我自动创建数据库

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
我也喜欢指令,让我自动创建文件

spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=update
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=update.sql
但是,当我在src/main/resources/application.properties中将这两者结合起来时

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.jpa.properties.javax.persistence.validation.mode=none

spring.datasource.url=jdbc:postgresql://${PG_DB_HOST:localhost}:5432/${PG_DB_NAME}

spring.datasource.username=${PG_DB_USER}
spring.datasource.password=${PG_DB_PASS}

spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=update
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=update.sql
似乎“spring.jpa.properties.javax.persistence”优先,并且我的模式更改不会针对数据库自动运行。是否有一种方法可以配置这两种情况,即将更改记录到文件中并自动针对我的数据库运行?

添加database.action with javax.persistence,如下所示,它将根据中解释的模型更新数据库模式

应用程序属性

spring.jpa.properties.javax.persistence.schema-generation.database.action=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.jpa.properties.javax.persistence.validation.mode=none

spring.datasource.url=jdbc:postgresql://${PG_DB_HOST:localhost}:5432/${PG_DB_NAME}

spring.datasource.username=${PG_DB_USER}
spring.datasource.password=${PG_DB_PASS}

spring.jpa.properties.javax.persistence.schema-generation.database.action=update
spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=update
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=update.sql
还建议使用
postgresql82方言
或根据您使用的版本更改(已弃用)
postgresqldialent
方言

应用程序属性

spring.jpa.properties.javax.persistence.schema-generation.database.action=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.jpa.properties.javax.persistence.validation.mode=none

spring.datasource.url=jdbc:postgresql://${PG_DB_HOST:localhost}:5432/${PG_DB_NAME}

spring.datasource.username=${PG_DB_USER}
spring.datasource.password=${PG_DB_PASS}

spring.jpa.properties.javax.persistence.schema-generation.database.action=update
spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=update
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=update.sql

我使用的是SpringBoot,我读到SpringBoot忽略persistence.xml--。是否有方法将该属性作为application.properties文件的一部分包含?是的,我为使用xml配置的人添加了该方法。由于您的项目由应用程序属性文件组成,您只需包括
spring.jpa.properties.javax.persistence.schema generation.database.action=update
,如答案中所述