首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在spring 2.3.1.RELEASE中禁用data.sql吗?

在spring 2.3.1.RELEASE中禁用data.sql吗?
EN

Stack Overflow用户
提问于 2020-06-16 03:53:57
回答 1查看 1K关注 0票数 1
代码语言:javascript
运行
复制
@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

代码语言:javascript
运行
复制
<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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
private int id;
private String name;
private String location;
private Date birthDate;

// getter and setter

我还在“资源”文件夹中有一个data.sql文件,其中包含创建和插入语句。如果我保留第1行(Thread.sleep(1000))注释并运行该项目,我将面临一个错误,但我能够通过h2控制台访问表人

代码语言:javascript
运行
复制
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]

我有两个场景

  1. 如果我保留第1行注释,并将data.sql文件重命名为schema.sql文件,那么一切都是完美的。我能查到为什么?
  2. 如果我保留data.sql文件而不重命名它,则必须取消第1行注释才能使应用程序正常工作。那是为什么?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-16 08:34:37

自SpringBoot2.3,JPA存储库初始化“延迟”.因此,run方法在初始化它之前运行(如果您不定义任何JPA存储库)。

如果设置

代码语言:javascript
运行
复制
spring.data.jpa.repositories.bootstrap-mode=default

application.properties上,按照您的预期运行。

另见:

javadoc说:

通过在DataSource上运行schema-*.sql和在DataSourceSchemaCreatedEvent上运行data-*.sql SQL脚本来处理InitializingBean#afterPropertiesSet()初始化。

DataSourceSchemaCreatedEvent说:

这在执行schema-*.sql文件或Hibernate初始化数据库时发生。

因此,在第一个场景中,data.sql是在JPA(Hibernate)初始化之后执行的,但它是“延迟的”。另一方面,schema.sql是在run()之前执行的,与早期版本相同。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62400654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档