若依项目 springboot集成flyway自动创表

若依项目 springboot集成flyway自动创表

  1. 添加依赖
    在ruoyi-admin这个module里面添加flyway依赖

 <dependency>
      <groupId>org.flywaydb</groupId>
      <artifactId>flyway-core</artifactId>
  </dependency>
<!--   不是必须的 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.flywaydb</groupId>
                <artifactId>flyway-maven-plugin</artifactId>
                <version>5.2.1</version>
            </plugin>
        </plugins>
    </build>
    
  1. yml 配置flyway

spring:
    # 配置flyway数据版本管理
    flyway:
        enabled: true
        baseline-on-migrate: true
        clean-on-validation-error: false
        sql-migration-prefix: V
        sql-migration-suffixes: .sql
        locations: classpath:db/migration/mysql
        
  1. 配置sql脚本
    在若依项目中的sql目录下有两个初始化sql脚本,在ruoyi-admin模块下的"resources/db/migration/mysql",命名为:Vx.x.x__ xxx.sql数据库文件。
    在这里插入图片描述
    一般的springboot项目进行到这里,flyway配置就完成了,不过ruoyi项目到这里启动的话,还是会报错,主要是有三个地方用到了@PostConstruct注解,系统需要从数据库中加载配置信息,并且是构造bean后就执行,此时flaway的数据库配置加载还没执行,如果是第一次执行项目的话,数据库都还没有表结构信息,所以会报错。
    直接改若依项目项目启动是加载到缓存的配置的这三个地方的加载时机就行了。
    首先,注释掉三个地方的配置加载。
    ruoyi-systemcom.ruoyi.system.service.impl.SysConfigServiceImpl的参数缓存配置
    在这里插入图片描述
    ruoyi-systemcom.ruoyi.system.service.impl.SysDictTypeServiceImpl的字典信息缓存配置
    在这里插入图片描述
    ruoyi-quartzcom.ruoyi.quartz.service.impl.SysJobServiceImpl的定时任务配置
    在这里插入图片描述ruoyi-system中新增一个配置类com.ruoyi.system.config.RuntimeConfig,内容如下,在项目加载完成后flyaway加载完成后再执行这些参数的缓存配置。
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *
  * @author: 云诺
  * @date: 2021/6/25
  * @description: 将项目启动后flyway创建好表加载到缓存
 */
@Component
public class RuntimeConfig implements ApplicationListener<ContextRefreshedEvent> {

    private final static Logger LOGGER = LoggerFactory.getLogger(RuntimeConfig.class);

    @Autowired
    private SysConfigMapper configMapper;


    @Autowired
    private SysDictTypeMapper dictTypeMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private Scheduler scheduler;

    @Autowired
    private SysJobMapper jobMapper;

    /**
     * 项目启动时,初始化参数
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        LOGGER.info("init Param ...");
        this.initParam();
        LOGGER.info("init dict ...");
        this.initDict();
        try {
            LOGGER.info("init job ...");
            this.initJob();
        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (TaskException e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化定时任务信息到缓存
     *
     * @throws SchedulerException
     * @throws TaskException
     */
    public void initJob() throws SchedulerException, TaskException {
        scheduler.clear();
        List<SysJob> jobList = jobMapper.selectJobAll();
        for (SysJob job : jobList) {
            ScheduleUtils.createScheduleJob(scheduler, job);
        }
    }



    /**
     * 初始化参数到缓存
     */
    public void initParam() {
        List<SysConfig> configsList = configMapper.selectConfigList(new SysConfig());
        for (SysConfig config : configsList)
        {
            CacheUtils.put(getCacheName(), getCacheKey(config.getConfigKey()), config.getConfigValue());
        }
    }

    /**
     * 初始化字典到缓存
     */
    public void initDict() {
        List<SysDictType> dictTypeList = dictTypeMapper.selectDictTypeAll();
        for (SysDictType dictType : dictTypeList)
        {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(dictType.getDictType());
            DictUtils.setDictCache(dictType.getDictType(), dictDatas);
        }
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey)
    {
        return Constants.SYS_CONFIG_KEY + configKey;
    }
    
    /**
     * 获取cache name
     *
     * @return 缓存名
     */
    private String getCacheName()
    {
        return Constants.SYS_CONFIG_CACHE;
    }
}

到这里就可以正常启动项目了

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小诺大人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值