繁体   English   中英

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

[英]Spring Boot dont read data.sql but it read import.sql

我想在 spring 创建表后导入 DML 脚本。 我正在处理data.sql文件,但我的应用程序没有看到它 我不知道为什么。 当我将data.sql重命名为import.sql它可以工作,但它也应该data.sql工作

有人知道为什么吗?

我的 application.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

当只有 data.sql 在resources

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)

我可以同时看到data.sqlimport.sqltarget/classes/data.sqltarget/classes/import.sql ...

如果将 data.sql 放在 jar 中,则在其名称前加上类路径或 META-INF

spring.datasource.data=classpath:/data.sql

spring.datasource.data=/META-INF/data.sql

(我不是 100% 确定,所以如果您尝试两种解决方案并给我反馈会很棒)

您必须通过注释您的行 spring.jpa.hibernate.ddl-auto=create-drop 并将其设置为验证来停用 Hibernate 加载(来自 import.sql)。 然后添加 spring.datasource.initialization:

spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto=validate

如果有人遇到这个问题,这就是我所做的:

应用程序属性:

甲骨文设置

spring.datasource.url = jdbc:oracle:thin:@localhost:1521:xe<br>
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver<br>
spring.datasource.username=xxxx<br>
spring.datasource.password=xxxx<br>
spring.jpa.hibernate.ddl-auto=none<br>
spring.datasource.initialization-mode=always<br>
spring.datasource.platform=oracle<br>

MYSQL 设置

spring.datasource.url = jdbc:mysql://localhost:3306/mysql<br>
spring.datasource.username = xxxx<br>
spring.datasource.password = xxxx<br>
spring.jpa.hibernate.ddl-auto=none<br>
spring.datasource.initialization-mode=always<br>
spring.datasource.platform=mysql<br>

帮助我的行是添加最后 2 行,还请记住,您必须有一个分别名为“data-oracle.sql”或“data-mysql.sql”的文件。

这是来源:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-spring-jdbc

当 naXa 的第一个选项时为我工作:

spring.datasource.data=classpath:/data.sql

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM