-Question Using boot 2.5.3 version ,i try to work with h2 database;but something happend;i can't initialize it; here is my profile:
#配置数据库的连接
sql:
init:
mode: always
schema-locations: classpath:h2/schema.sql
data-locations: classpath:h2/data.sql
username: sa
password: 123456
platform: h2
encoding: UTF-8
continue-on-error: true
spring:
#启用热部署
devtools:
restart:
enabled: true
additional-exclude: WEB-INF/**
additional-paths: src/main/java #重启目录
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:h2:mem:db2
username: sa
password: 123456
driver-class-name: org.h2.Driver
hikari:
minimum-idle: 5
# 空闲连接存活最大时间,默认600000(10分钟)
idle-timeout: 180000
# 连接池最大连接数,默认是10
maximum-pool-size: 10
# 此属性控制从池返回的连接的默认自动提交行为,默认值:true
auto-commit: true
# 连接池名称
pool-name: MyHikariCP
# 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
max-lifetime: 1800000
# 数据库连接超时时间,默认30秒,即30000
connection-timeout: 30000
connection-test-query: SELECT 1
#schema: classpath:h2/schema.sql
#data: classpath:h2/data.sql
jpa:
database-platform: org.hibernate.dialect.H2Dialect
hibernate:
ddl-auto: update
properties:
hibernate:
show_sql: true
use_sql_commonents: true
format_sql: true
defer-datasource-initialization: true
h2:
console:
enabled: true
path: /h2
settings:
trace: true
web-allow-others: false
logging:
level:
root: info
when i run a junit test ,it showed an error
@SpringBootTest
public class SampleTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<User> userList = userMapper.selectList(null);
Assert.assertEquals(5, userList.size());
userList.forEach(System.out::println);
}
}
error
Then ,i know the database don't initialize well;
so ,i use the old datasource.schema
to initialize db and it work !
then...
So I think the new sql.init
may be not work well.
Comment From: mdeinum
Your property is named sql.init
, it must be in the spring
part so it is spring.sql.init
. Currently it is just being ignored.
spring:
sql:
init:
mode: always
schema-locations: classpath:h2/schema.sql
data-locations: classpath:h2/data.sql
username: a
password: 123456
platform: h2
encoding: UTF-8
continue-on-error: true
#启用热部署
devtools:
Comment From: SQLconnecter
i make a silly mistake,thanks for your help ,it can work If i use spring.sql.init
.