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

时间:2018-03-20 23:55:11

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

我想在spring创建表之后导入DML脚本。 我正在与 function play(a, b) { document.getElementById("demo").innerHTML = a * b; } play(2,3); 文件作斗争,但我的应用看不到它。 我不知道为什么。当我将data.sql重命名为 data.sql 时,它可以正常工作,但它也应该可以与import.sql一起使用

有人知道为什么吗?

我的application.properties:

data.sql

我将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

当只有data.sql在src/main/resources

resources

仅限import.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' 中):

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)'

中输入spring.datasource.data=data.sql
application.properties

我可以在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.sql中看到import.sqltarget/classes/data.sql ...

4 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

如果有人遇到麻烦,这就是我所做的:

applications.properties:

Oracle设置

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>

帮助我添加最后两行的代码行还请记住,您必须具有一个分别名为“ 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

答案 2 :(得分:0)

如果将data.sql放在jar中,则在classpath或META-INF

之前添加其名称
spring.datasource.data=classpath:/data.sql

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

(我不是百分百肯定所以如果你尝试这两个解决方案并给我反馈意见会很棒)

答案 3 :(得分:0)

在选择naXa时为我工作:

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