data.sql 在 spring boot 2.3.1.RELEASE 中被禁用了吗?
Is data.sql disabled in spring boot 2.3.1.RELEASE?
@SpringBootApplication
public class DatabaseDemoApplication implements CommandLineRunner {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
PersonJbdcDao dao;
public static void main(String[] args) {
SpringApplication.run(DatabaseDemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
// Thread.sleep(1000); --------------------------------------------------> line 1
logger.info("All users -> {}", dao.findAll());
}
}
@Repository
public class PersonJbdcDao {
@Autowired
JdbcTemplate jdbcTemplate;
public List<Person> findAll() {
return jdbcTemplate.query("select * from person",
new BeanPropertyRowMapper<Person>(Person.class));
}
}
在pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
我正在使用 h2 数据库。以下是 application.properties
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
Person.class
private int id;
private String name;
private String location;
private Date birthDate;
// getter and setter
我在资源文件夹中还有一个 data.sql
文件,其中包含创建和插入语句。
如果我保留第 1 行 (Thread.sleep(1000)) 的评论和 运行 该项目,我将面临错误,但我可以通过 h2 控制台访问 table Person
java.lang.IllegalStateException: Failed to execute CommandLineRunner
Caused by: org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [select * from person]; nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PERSON" not found; SQL statement:
select * from person [42102-200]
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PERSON" not found; SQL statement:
select * from person [42102-200]
我有 2 个场景
- 如果我保留第 1 行的注释并将
data.sql
文件重命名为 schema.sql
文件,则一切正常。我可以访问这是为什么?
- 如果我保留
data.sql
文件而不重命名它,我必须取消注释第 1 行才能使应用程序正常工作。这是为什么?
自 Spring Boot 2.3,JPA repository initialization is "deferred"。
所以 run
方法 运行s 在初始化它之前(如果你没有定义任何 JPA 存储库)。
如果设置
spring.data.jpa.repositories.bootstrap-mode=default
application.properties
,运行 如您所料。
另见:
DataSourceInitializerInvoker
javadoc 说:
Bean to handle DataSource
initialization by running schema-*.sql
on InitializingBean#afterPropertiesSet()
and data-*.sql
SQL scripts on DataSourceSchemaCreatedEvent
.
,DataSourceSchemaCreatedEvent
表示:
This happens when schema-*.sql
files are executed or when Hibernate initializes the database.
因此,在第一种情况下,data.sql
在 JPA(Hibernate) 初始化后执行,但它是 "deferred"。
另一方面,schema.sql
在run()
之前执行,与早期版本相同。
@SpringBootApplication
public class DatabaseDemoApplication implements CommandLineRunner {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
PersonJbdcDao dao;
public static void main(String[] args) {
SpringApplication.run(DatabaseDemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
// Thread.sleep(1000); --------------------------------------------------> line 1
logger.info("All users -> {}", dao.findAll());
}
}
@Repository
public class PersonJbdcDao {
@Autowired
JdbcTemplate jdbcTemplate;
public List<Person> findAll() {
return jdbcTemplate.query("select * from person",
new BeanPropertyRowMapper<Person>(Person.class));
}
}
在pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
我正在使用 h2 数据库。以下是 application.properties
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
Person.class
private int id;
private String name;
private String location;
private Date birthDate;
// getter and setter
我在资源文件夹中还有一个 data.sql
文件,其中包含创建和插入语句。
如果我保留第 1 行 (Thread.sleep(1000)) 的评论和 运行 该项目,我将面临错误,但我可以通过 h2 控制台访问 table Person
java.lang.IllegalStateException: Failed to execute CommandLineRunner
Caused by: org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [select * from person]; nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PERSON" not found; SQL statement:
select * from person [42102-200]
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PERSON" not found; SQL statement:
select * from person [42102-200]
我有 2 个场景
- 如果我保留第 1 行的注释并将
data.sql
文件重命名为schema.sql
文件,则一切正常。我可以访问这是为什么? - 如果我保留
data.sql
文件而不重命名它,我必须取消注释第 1 行才能使应用程序正常工作。这是为什么?
自 Spring Boot 2.3,JPA repository initialization is "deferred"。
所以 run
方法 运行s 在初始化它之前(如果你没有定义任何 JPA 存储库)。
如果设置
spring.data.jpa.repositories.bootstrap-mode=default
application.properties
,运行 如您所料。
另见:
DataSourceInitializerInvoker
javadoc 说:
Bean to handle
DataSource
initialization by runningschema-*.sql
onInitializingBean#afterPropertiesSet()
anddata-*.sql
SQL scripts onDataSourceSchemaCreatedEvent
.
,DataSourceSchemaCreatedEvent
表示:
This happens when
schema-*.sql
files are executed or when Hibernate initializes the database.
因此,在第一种情况下,data.sql
在 JPA(Hibernate) 初始化后执行,但它是 "deferred"。
另一方面,schema.sql
在run()
之前执行,与早期版本相同。