08-02-2023, 03:41 PM
You can add a `spring.datasource.data` property to `application.properties` listing the sql files you want to run. Like this:
spring.datasource.data=classpath:accounts.sql, classpath:books.sql, classpath:reviews.sql
//or (depending on SB version)
spring.sql.init.data-locations=classpath:accounts.sql, classpath:books.sql, file:reviews.sql
The sql insert statements in each of these files will then be run, allowing you to keep things tidy.
If you put the files in the classpath, for example in `src/main/resources` they will be applied. Or replace `classpath:` with `file:` and use an absolute path to the file
If you want to run DDL type SQL then use:
```
spring.datasource.schema=classpath:create_account_table.sql
// depending on spring version
spring.sql.init.schema-locations=classpath:create_account_table.sql
```
**Edit:** these solutions are great to get you up and running quickly, however for a more production ready solution it would be worth looking at a framework such as [flyway][1], or [liquibase][2]. These frameworks integrate well with spring, and provide a quick, consistent, version-controlled means of initialising schema, and standing-data.
[1]:
spring.datasource.data=classpath:accounts.sql, classpath:books.sql, classpath:reviews.sql
//or (depending on SB version)
spring.sql.init.data-locations=classpath:accounts.sql, classpath:books.sql, file:reviews.sql
The sql insert statements in each of these files will then be run, allowing you to keep things tidy.
If you put the files in the classpath, for example in `src/main/resources` they will be applied. Or replace `classpath:` with `file:` and use an absolute path to the file
If you want to run DDL type SQL then use:
```
spring.datasource.schema=classpath:create_account_table.sql
// depending on spring version
spring.sql.init.schema-locations=classpath:create_account_table.sql
```
**Edit:** these solutions are great to get you up and running quickly, however for a more production ready solution it would be worth looking at a framework such as [flyway][1], or [liquibase][2]. These frameworks integrate well with spring, and provide a quick, consistent, version-controlled means of initialising schema, and standing-data.
[1]:
[To see links please register here]
[2]:[To see links please register here]