一些 Spring Boot 小技巧、小知识点
初始化数据
我们在做测试的时候经常需要初始化导入一些数据,如何来处理呢?会有两种选择,一种是使用 Jpa,另外一种是 Spring JDBC 。两种方式各有区别下面来详细介绍。
使用 Jpa
在使用spring boot jpa的情况下设置spring.jpa.hibernate.ddl-auto的属性设置为 create or create-drop的时候,Spring Boot 启动时默认会扫描 classpath 下面(项目中一般是 resources 目录)是否有import.sql,如果有机会执行import.sql脚本。
使用 Spring JDBC
使用 Spring JDBC 需要在配置文件中添加以下配置
spring:
datasource:
schema: classpath:db/schema.sql
data: classpath:db/data.sql
sql-