Spring Boot 不读取 data.sql 但它读取 import.sql

标签 spring hibernate spring-boot spring-data-jpa

我想在 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.sqlimport.sqltarget/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/

相关文章:

java - MockMvc 和流端点 - 流关闭后的附加字节

java - 实例化ResponseException以进行测试时,未处理的IOException

spring - 通过Logstash在Elasticsearch中导入MySQL条目

java - spring-orm 3.2 需要不同的 JtaPlatform 包,而不是 hibernate-core 4.3

Hibernate 双向多对一

spring-boot - 如何在基于Gradle的Spring Boot 2.0.0应用程序中检测开发和生产环境?

java - 我们可以在 Spring Boot 中更改 application.properties 文件中的某个键的值吗?

java - SpringSecurity - SecurityConfiguration.java - HttpSecurity - 不工作

spring - 如何在类注解@controller 中获取Spring WebContext

sql-server - 并发 - Spring + Hibernate + SQL Server