我想在 spring 创建表后导入 DML 脚本。
我正在处理 data.sql
文件,但我的应用程序看不到它。
我不知道为什么。它在我将 data.sql
重命名为 import.sql
时起作用,但它也应该与 一起起作用
数据.sql.
有人知道为什么吗?
我的应用程序.properties:
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:postgresql://localhost:5432/yyy
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=org.postgresql.Driver
我把data.sql
放到了src/main/resources
当resources
中只有data.sql时:
2018-03-21 00:42:13.646 INFO 4740 --- [ main] o.h.t.schema.internal.SchemaCreatorImpl : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@eebc0db'
只有import.sql时(也在src/main/resources
中):
2018-03-21 00:48:57.023 INFO 16600 --- [ main] o.h.t.schema.internal.SchemaCreatorImpl : HHH000476: Executing import script 'ScriptSourceInputFromUrl(file:/C:/Users/Pawel/Desktop/Project/target/classes/import.sql)'
当我在 application.properties
中输入 spring.datasource.data=data.sql
时
Exception in thread "SimpleAsyncTaskExecutor-2" org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException: Property spring.datasource.data with value 'ServletContext resource [/data.sql]' is invalid: The specified resource does not exist.
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.getResources(DataSourceInitializer.java:169)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.getScripts(DataSourceInitializer.java:151)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.initSchema(DataSourceInitializer.java:114)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.onApplicationEvent(DataSourceInitializerInvoker.java:93)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.onApplicationEvent(DataSourceInitializerInvoker.java:37)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.lambda$multicastEvent$0(SimpleApplicationEventMulticaster.java:136)
at java.lang.Thread.run(Thread.java:745)
我可以在 target/classes/data.sql
中看到 data.sql
和 import.sql
,target/classes/import.sql
...
最佳答案
如果将 data.sql 放入 jar 中,则在其名称前加上类路径或 META-INF
spring.datasource.data=classpath:/data.sql
spring.datasource.data=/META-INF/data.sql
(我不是 100% 确定,所以如果您尝试这两种解决方案并给我反馈就太好了)
关于Spring Boot 不读取 data.sql 但它读取 import.sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49395998/