侧边栏壁纸
博主头像
王旭阳个人博客博主等级

工欲善其事,必先利其器

  • 累计撰写 121 篇文章
  • 累计创建 28 个标签
  • 累计收到 25 条评论

目 录CONTENT

文章目录

Springboot整合Flyway自动管理数据库变更

wxy
wxy
2022-12-09 / 0 评论 / 11 点赞 / 701 阅读 / 7740 字
温馨提示:
本文最后更新于 2023-08-31,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

以若依项目为例(v3.8.4)

1、 pom.xml(ruoyi) 引入依赖管理

    <dependencyManagement>
        <dependencies>
 			<dependency>
                <groupId>org.flywaydb</groupId>
                <artifactId>flyway-core</artifactId>
                <version>${flyway.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

2、 pom.xml(ruoyi-admin).xml引入依赖

    <dependencies>
    <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
    </dependencies>

3、 resources目录下新建db/migration目录 来存放sql脚本

wxy_2022-12-09_10-46-07

版本1.0.0中放入初始化sql脚本
版本1.0.1中放入测试脚本

# 创建flyway_test 并插入一条数据
DROP TABLE IF EXISTS `cpt_monitor`.`flyway_test`;
CREATE TABLE `cpt_monitor`.`flyway_test`
(
    `id`          int      NOT NULL AUTO_INCREMENT,
    `update_time` datetime NULL,
    PRIMARY KEY (`id`)
);

insert into `cpt_monitor`.`flyway_test`
values (1, now());

4、 在application.yml中配置

spring
  flyway:
    # 是否启用flyway
    enabled: true
    # 编码格式,默认UTF-8
    encoding: UTF-8
    # 当迁移发现数据库非空且存在没有元数据的表时,自动执行基准迁移,新建schema_version表
    baseline-on-migrate: true

配置好数据库链接,空数据库即可。正常情况下此时启动 便会在数据中执行初始化sql 和 测试sql 两个版本按顺序,并自动生成flyway_schema_history表来记录版本;

5、 若依项目(v3.8.4)还需要额外增加配置

主要是有两个地方用到了@PostConstruct注解,系统需要从数据库中加载配置信息,并且是构造bean后就执行,此时flaway的数据库配置加载还没执行,如果是第一次执行项目的话,数据库都还没有表结构信息,所以会报错。
此时可以考虑重写flyway自动配置,通过@Bean的方式添加,不采用默认的自动化配置,控制表信息的加载时机。更方便的,直接改这两个地方的加载时机就行了。

1 、找到这两个配置类,注释掉初始化;

  • ruoyi-system中com.ruoyi.system.service.impl.SysDictTypeServiceImpl的字典信息缓存配置
  • ruoyi-system中com.ruoyi.system.service.impl.SysConfigServiceImpl的redis参数缓存配置
    SysDictTypeServiceImpl.java
 /**
     * 项目启动时,初始化字典到缓存
     */
/*
    @PostConstruct
    public void init() {
        loadingDictCache();
    }
*/

SysConfigServiceImpl.java

    /**
     * 项目启动时,初始化参数到缓存
     */
/*    @PostConstruct
    public void init() {
        loadingConfigCache();
    }*/

2. 在若依system模块中新建conf/RuntimeConfig

package com.ruoyi.system.config;

import com.ruoyi.system.service.impl.SysConfigServiceImpl;
import com.ruoyi.system.service.impl.SysDictTypeServiceImpl;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * @Description 兼容flyway  把 项目初始化内容 改到这里初始化(改变加载时机)
 * ruoyi-system中com.ruoyi.system.service.impl.SysDictTypeServiceImpl的字典信息缓存配置
 * ruoyi-system中com.ruoyi.system.service.impl.SysConfigServiceImpl的redis参数缓存配置
 * 参考 https://blog.csdn.net/qq_41885819/article/details/114970148
 * @Author wxyShine
 * @Date 2022/12/9 9:37
 * @Email wxy@wxy97.com
 */
@Component
public class RuntimeConfig implements ApplicationListener<ContextRefreshedEvent> {

    private final SysConfigServiceImpl sysConfigServiceImpl;
    private final SysDictTypeServiceImpl sysDictTypeServiceImpl;

    public RuntimeConfig(SysConfigServiceImpl sysConfigServiceImpl,
                         SysDictTypeServiceImpl sysDictTypeServiceImpl) {
        this.sysConfigServiceImpl = sysConfigServiceImpl;
        this.sysDictTypeServiceImpl = sysDictTypeServiceImpl;
    }

    /**
     * 项目启动时,初始化参数
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        sysConfigServiceImpl.loadingConfigCache();
        sysDictTypeServiceImpl.loadingDictCache();
    }

}

此时已经整合完成。

配置Maven插件来使用Flyway

在pom.xml(ruoyi)中增加如下

 <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>
            <plugin>
                <groupId>org.flywaydb</groupId>
                <artifactId>flyway-maven-plugin</artifactId>
                <version>5.2.4</version>
                <configuration>
                    <url>jdbc:mysql://localhost:3306/name</url>
                    <user>root</user>
                    <password>123456</password>
                    <locations>
                        <location>classpath:db/migration</location>                  
                    </locations>
                </configuration>
            </plugin>

配置好之后即可使用maven插件来进行操作
wxy_2022-12-09_10-59-55

11

评论区