SpringBoot2通过sql脚本文件生成表时不成功

Easter79
• 阅读 710

SpringBoot2通过jdbc生成数据表应该参看 DataSourceInitializerInvoker 这个类了

SpringBoot2通过sql脚本文件生成表时不成功

SpringBoot2通过sql脚本文件生成表时不成功 SpringBoot2通过sql脚本文件生成表时不成功

/*
 * Copyright 2012-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure.jdbc;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;

/**
 * Bean to handle {@link DataSource} initialization by running {@literal schema-*.sql} on
 * {@link InitializingBean#afterPropertiesSet()} and {@literal data-*.sql} SQL scripts on
 * a {@link DataSourceSchemaCreatedEvent}.
 *
 * @author Stephane Nicoll
 * @see DataSourceAutoConfiguration
 */
class DataSourceInitializerInvoker
        implements ApplicationListener<DataSourceSchemaCreatedEvent>, InitializingBean {

    private static final Log logger = LogFactory
            .getLog(DataSourceInitializerInvoker.class);

    private final ObjectProvider<DataSource> dataSource;

    private final DataSourceProperties properties;

    private final ApplicationContext applicationContext;

    private DataSourceInitializer dataSourceInitializer;

    private boolean initialized;

    DataSourceInitializerInvoker(ObjectProvider<DataSource> dataSource,
            DataSourceProperties properties, ApplicationContext applicationContext) {
        this.dataSource = dataSource;
        this.properties = properties;
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() {
        DataSourceInitializer initializer = getDataSourceInitializer();
        if (initializer != null) {
            boolean schemaCreated = this.dataSourceInitializer.createSchema();
            if (schemaCreated) {
                initialize(initializer);
            }
        }
    }

    private void initialize(DataSourceInitializer initializer) {
        try {
            this.applicationContext.publishEvent(
                    new DataSourceSchemaCreatedEvent(initializer.getDataSource()));
            // The listener might not be registered yet, so don't rely on it.
            if (!this.initialized) {
                this.dataSourceInitializer.initSchema();
                this.initialized = true;
            }
        }
        catch (IllegalStateException ex) {
            logger.warn("Could not send event to complete DataSource initialization ("
                    + ex.getMessage() + ")");
        }
    }

    @Override
    public void onApplicationEvent(DataSourceSchemaCreatedEvent event) {
        // NOTE the event can happen more than once and
        // the event datasource is not used here
        DataSourceInitializer initializer = getDataSourceInitializer();
        if (!this.initialized && initializer != null) {
            initializer.initSchema();
            this.initialized = true;
        }
    }

    private DataSourceInitializer getDataSourceInitializer() {
        if (this.dataSourceInitializer == null) {
            DataSource ds = this.dataSource.getIfUnique();
            if (ds != null) {
                this.dataSourceInitializer = new DataSourceInitializer(ds,
                        this.properties, this.applicationContext);
            }
        }
        return this.dataSourceInitializer;
    }

}

View Code

SpringBoot2通过sql脚本文件生成表时不成功,控制台没有输出相关创建数据表的提示

在这篇博客找到了解决方法:https://blog.csdn.net/weixin_38306434/article/details/83379059

解决方法:(1) 在application配置文件指定执行sql(静态资源)的地方加上initialization-mode:always即可

(2)如果你配置文件没有指定执行文件的名称而是使用默认的schema.sql或者schema-all.sql的话就在配置文件中加上spring.datasource.initialization-mode=always

原理的话可能是这篇博客里面讲的:https://www.cnblogs.com/snoopy-GJT/p/10825019.html

因为SpringBoot在启动时,只有检测到spring.datasource.initialization-mode=ALWAYS配置,然后再检测spring.datasource.schema,且配置的sql角本命令不为空,才会去执行schema和spring.datasource.data。因此需要在scheme.sql中随便写一句sql语句。

所以在application.properties/application.yml文件中必须配置spring.datasource.initialization-mode=ALWAYS

点赞
收藏
评论区
推荐文章
Easter79 Easter79
3年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Easter79 Easter79
3年前
vue+element 实现动态数据表格及添加表格操作按钮
!(https://img2018.cnblogs.com/blog/1297179/201905/129717920190528171159526732092213.png)!(https://img2018.cnblogs.com/blog/1297179/201905/12971792019052817131741721302796
Easter79 Easter79
3年前
springboot2 配置 https
packagecn.xiaojf.aibus.configure;importorg.apache.catalina.Context;importorg.apache.catalina.connector.Connector;importorg.apache.coyote.http11.Http11Ni
Wesley13 Wesley13
3年前
vscode的eslint插件不起作用
最近在用vue进行开发,但是vsCode中的eslint插件装上之后不起作用1.vsCode打开“设置”,选择"settings.json"!(https://img2018.cnblogs.com/blog/921637/201905/921637201905161657075401304630667.png)2.输入一段脚本
Stella981 Stella981
3年前
Charles和mitmproxy代理设置
1.Charles安装:1\.windows安装证书!(https://img2018.cnblogs.com/blog/1430161/201905/1430161201905042125358371803905281.png)2.!(https://oscimg.oschina.net/oscne
Stella981 Stella981
3年前
SpringBoot2通过sql脚本文件生成表时不成功
SpringBoot2通过jdbc生成数据表应该参看 DataSourceInitializerInvoker这个类了!(https://img2018.cnblogs.com/blog/1242355/201905/124235520190516164424942334144530.png)!(https://oscimg.oschi
Stella981 Stella981
3年前
Django 关联对象操作及多表查询
关联表的数据操作一对多正向:如果这个模型有外键字段,通过这个模型外键进行操作叫正向!(https://img2018.cnblogs.com/blog/1633684/201905/163368420190507170738352121019600.png)增  通过属性复制!(https://img2018.cnblog
Wesley13 Wesley13
3年前
JOIN关联表中ON,WHERE后面跟条件的区别
SQL中on条件与where条件的区别      数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户。     在使用leftjion时,on和where条件的区别如下:1、on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。2、where条件是
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k