欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot2.0以上无法执行sql脚本

程序员文章站 2022-04-30 15:29:52
...
schema:
  - classpath:department.sql
# springboot2.0以上版本需要添加上这个才能执行sql本
initialization-mode: always

原理:

DataSourceInitializer执行创建函数时增加了类型判断

void initSchema() {
    List<Resource> scripts = this.getScripts("spring.datasource.data", this.properties.getData(), "data");
    if (!scripts.isEmpty()) {
        if (!this.isEnabled()) {
            logger.debug("Initialization disabled (not running data scripts)");
            return;
        }
    
        String username = this.properties.getDataUsername();
        String password = this.properties.getDataPassword();
        this.runScripts(scripts, username, password);
    }
}
private boolean isEnabled() {
    DataSourceInitializationMode mode = this.properties.getInitializationMode();
    if (mode == DataSourceInitializationMode.NEVER) {
        return false;
    } else {
        return mode != DataSourceInitializationMode.EMBEDDED || this.isEmbedded();
    }
}

 

相关标签: spring boot