Importing Data with spring boot

后端 未结 5 1130
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-05 07:21

I have flyway and spring-boot working correctly, but I can\'t seem to wire up my spring.datasource.data correctly.

If I have a file src/main/resou

相关标签:
5条回答
  • 2021-01-05 07:28

    TL;DR

    Create a blank schema.sql if you want your data.sql to run.
    Also as stated in a comment it must execute one line such as `select 1` or `select 1 from dual`
    

    You said

    The only thing I can actually get to work is to copy one_project.sql to src/main/resources/schema.sql

    Which makes me think it's evident you don't have a schema.sql

    So just create a blank schema.sql and then it will run one_project.sql

    Source Code -https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializer.java

    As you can see it gathers the schema locations and if schema resources are empty then it doesn't continue to run the data.sql (this is at the top of the runSchemaScripts() method)

    0 讨论(0)
  • 2021-01-05 07:29

    Stuck at that quite long. My context: Spring Boot 2.2.6 + Hibernate 5.4 + script.sql in classpath(src/main/resources). To make script executed at application start I was need to add in application.properties:

    spring.datasource.initialization-mode=always
    spring.jpa.hibernate.ddl-auto=update
    spring.datasource.data=classpath:script.sql
    

    And remove all comments BEFORE the actual code and BETWEEN code in script.sql. Or if you need comments, add SELECT 1; on the next line after the line with comment. Because the next line after commented one seems to be ignored. No matter how many line breaks after line with comment you paste.

    0 讨论(0)
  • 2021-01-05 07:32

    Try using classpath*, like the following:

    spring.datasource.schema=classpath*:db/seeds/your_schema.sql
    spring.datasource.data=classpath*:db/seeds/one_project.sql
    
    0 讨论(0)
  • 2021-01-05 07:34

    The way I got it working was by using the following properties

     spring.datasource.data=classpath:prod.sql
     spring.datasource.url=jdbc:mysql://localhost:3306/DATABASENAME?useSSL=false
     spring.datasource.username=USERNAME
     spring.datasource.password=PASSWORD
     spring.datasource.initialization-mode=always 
    

    spring.datasource.initialization-mode=always seems to do the trick

    0 讨论(0)
  • 2021-01-05 07:50

    As i can see it, Spring Boot executes the data scripts if one of the following conditions is true:

    • The schema.sql script is present and the initialization is enabled (spring.datasource.initialize=true)
    • If JPA and Hibernate is used and autoconfigured with Spring Boot: The property hibernate.hbm2ddl.auto is present (the value doesn't matter, you can give it an empty string or just "validate") and the initialization is enabled (spring.datasource.initialize=true).
    0 讨论(0)
提交回复
热议问题