广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot集成flyway自动创表的详细配置
  • 520
分享到

springboot集成flyway自动创表的详细配置

2024-04-02 19:04:59 520人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

Flayway是一款数据库版本控制管理工具,,支持数据库版本自动升级,Migrations可以写成sql脚本,也可以写在java代码里;不仅支持Command Line和java a

Flayway是一款数据库版本控制管理工具,,支持数据库版本自动升级,Migrations可以写成sql脚本,也可以写在java代码里;不仅支持Command Line和java api ,也支持Build构建工具和Spring Boot,也可以在分布式环境下能够安全可靠安全地升级数据库,同时也支持失败恢复。

Flyway最核心的就是用于记录所有版本演化和状态的MetaData表,Flyway首次启动会创建默认名为SCHEMA_VERSION的元素局表。 表中保存了版本,描述,要执行的sql脚本等;

在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>

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

配置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;


@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();
        }
    }

    
    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);
        }
    }

    
    private String getCacheKey(String configKey)
    {
        return Constants.SYS_CONFIG_KEY + configKey;
    }
    
    
    private String getCacheName()
    {
        return Constants.SYS_CONFIG_CACHE;
    }
}

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

以上就是springboot集成flyway自动创表的详细内容,更多关于springboot自动创表的资料请关注编程网其它相关文章!

--结束END--

本文标题: springboot集成flyway自动创表的详细配置

本文链接: https://www.lsjlt.com/news/129182.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作