如何在数据库模式生成之后但在应用程序启动之前执行sql脚本 [英] How to execute sql script after db schema generation but before application startup
问题描述
我想从我的Java类生成DB结构
I want to generate DB structure from my Java classes
jpa.generate-ddl: true
jpa.ddl-auto: true
另外,我需要在应用程序启动之前运行SQL脚本,因为我有 @PostConstruct
方法,我使用这些数据。
Also, I need to run SQL script before application will up because I have @PostConstruct
methods where I use these data.
你能举一个例子来说明如何在Spring Boot中做到这一点吗?
Can you show an example how to do it in Spring Boot?
推荐答案
您只需在资源文件夹中放置一个名为 data.sql 的文件, .properties文件驻留。
All you need to do is place a file named data.sql in the resources folder where application.properties file reside.
此文件中的任何查询都将在应用程序启动之前和创建数据库之后执行。
Any query within this file will be executed before the start of the application and after the creation of your db.
要阅读更多信息,请查看此链接
http://www.baeldung.com/spring-boot-data-sql-and-schema-sql
To read more please check this link http://www.baeldung.com/spring-boot-data-sql-and-schema-sql
这篇关于如何在数据库模式生成之后但在应用程序启动之前执行sql脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!