【springboot】springboot如何自动创建数据库表 使用自定义配置节点

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * 中间表创建策略配置
 * @author qkj
 */
@ConfigurationProperties(prefix = "spring.activiti")
@Data
public class CustomTableProperties {
    private boolean customTableRecreate = false;
}

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * 中间表创建策略 配置类
 * @author qkj
 */
@Configuration
// 使得配置类生效
@EnableConfigurationProperties(CustomTableProperties.class)
// 配置类作用于配置文件中
@ConditionalOnProperty(name = "spring.activiti.custom-table-recreate",matchIfMissing = false)

public class CustomTableBeanConfiguration {

}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.DatabasePopulator;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Description: 自动创建中间表表配置类 (activiti内置表会自动创建 这里是创建我们的中间表)
 * date: 2022/3/24 10:39
 *
 * @author: qkj
 * @since JDK 11
 */
@Configuration

public class CustomTableConfiguration {

    @Autowired
    private CustomTableProperties customTableProperties;

    @Value("classpath:db/create/activiti.mysql.create.history.details.sql")
    private Resource createSql;

    @Value("classpath:db/recreate/activiti.mysql.recreate.history.details.sql")
    private Resource reCreateSql;


    @Bean
    public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) throws SQLException {
        final DataSourceInitializer initializer = new DataSourceInitializer();
        // 设置数据源
        initializer.setDataSource(dataSource);

        ResultSet tables = dataSource.getConnection().getMetaData().getTables(null, null, "act_history_details_business", null);
        // 表已存在且不需要重建
        if (tables.next() && !customTableProperties.isCustomTableRecreate()) {
            return initializer;
        }

        initializer.setDatabasePopulator(databasePopulator());
        return initializer;
    }

    private DatabasePopulator databasePopulator() {
        final ResourceDatabasePopulator popular = new ResourceDatabasePopulator();
        if (!customTableProperties.isCustomTableRecreate()) {
            popular.addScripts(createSql);
        } else {
            popular.addScripts(reCreateSql);
        }
        return popular;
    }

}

资源路径:(里面存放建表语句)
在这里插入图片描述

yml配置:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孟秋与你

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值