Java笔记··By/蜜汁炒酸奶

springBoot+jpa根据实体类注解生成SQL文件

目录

基于环境:springBoot1.5+jpa2.1
在springBoot配置文件application.properties中添加如下属性即可:

spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create

spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=db/base/create.sql
1
2
3

生成脚本,jpa需要设置如下两项:

javax.persistence.schema-generation.scripts.action

创建sql脚本的方式。包含create, drop, drop-and-create, none,默认为none。

javax.persistence.schema-generation.scripts.create-target

是否在启动时将任何模式生成到脚本中。将处理在启动时加载元数据的所有类的模式(即在持久性单元中指定的类)。包含create, drop, drop-and-create, none,默认为none。

在springBoot中的文档$77.5 Configure JPA properties中有如下一句:

In addition all properties in spring.jpa.properties.* are passed through as normal JPA properties (with the prefix stripped) when the local EntityManagerFactory is created.

故,可直接使用上面最开始的两条语句达到在springBoot启动时用jpa生成SQL脚本文档。

参考资料

如何用现代的Spring Boot + Data JPA和Hibernate设置生成ddl创建脚本?

JPA Persistence Guide (v5.1)

预览
Loading comments...
1 条评论
  • W

    […] springBoot+jpa根据实体类注解生成SQL文件 […]

example
预览