Spring.sql.init.data-locations 中 DML 失效
Jihongchang(讨论 | 贡献)2023年2月16日 (四) 05:38的版本
以
# DataSource Config
spring:
datasource:
url: jdbc:h2:mem:testdb
driver-class-name: org.h2.Driver
schema: classpath:db/schema-h2.sql
username: root
password: test
hikari:
leak-detection-threshold: 2000
register-mbeans: true
sql:
init:
schema-locations: classpath:db/schema-h2.sql
data-locations: classpath:db/data-h2.sql
h2:
console:
enabled: true
path: /h2-console
settings:
trace: true
jpa:
show-sql: true
为例,启动之后发现 db/data-h2.sql 里 insert 的数据不存在
这是因为 spring.sql.init.schema-locations
和 spring.sql.init.data-locations
是在 Spring Boot 2.5.0 之后才有的
而在 Spring Boot 2.5.0 版本之前是使用 spring.datasource.schema
和 spring.datasource.data
来配置数据库初始化的。