springBoot+jpa根據實體類註解生成SQL文件
- 2020 年 1 月 21 日
- 筆記
基於環境: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
生成腳本,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創建腳本?