SpringBoot 集成 Liquibase

概述

Liquibase是一个数据库修改的解决方案,使您能够从开发到生产更快、更安全地修改和发布数据库变更。

Liquibase有多种使用方式, 直接集成到SpringBoot中是比较简单的一种, 它会在SpringBoot启动时自动检测运行, 省略了升级时手动运行Liquibase的步骤.

更详细的描述请查看官网

一、 引入依赖

xml 复制代码
<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
    <version>4.28.0</version>
</dependency>

二、 单数据源配置

1. (可选) application.yml中添加liquibase配置
yml 复制代码
spring:
  liquibase:
    enabled: true
    change-log: classpath:/liquibase/changelog-master.xml
2. 注册JavaBean

其实也可以不添加配置, 直接使用JavaBean配置,

复制代码
import liquibase.integration.spring.SpringLiquibase;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class LiquibaseConfiguration {
    
    @Bean
    @ConfigurationProperties(prefix = "spring.liquibase")
    public LiquibaseProperties getLiquibaseProperties() {
    	// 不修改配置文件的写法
    	LiquibaseProperties liquibaseProperties = new LiquibaseProperties();
        if (StringUtils.equals(liquibaseProperties.getChangeLog(), "classpath:/db/changelog/db.changelog-master.yaml")) {
            liquibaseProperties.setChangeLog("classpath:liquibase/changelog-master.xml");
        }
        return liquibaseProperties;
        // 修改配置文件的写法
        // return new LiquibaseProperties();
    }

    @Bean
    public SpringLiquibase liquibase(DataSource dataSource) {
        LiquibaseProperties liquibaseProperties = getLiquibaseProperties();
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setChangeLog(liquibaseProperties.getChangeLog());
        liquibase.setDataSource(dataSource);       
        liquibase.setShouldRun(true);
        return liquibase;
    }
}
3. 添加changelog-master.xml和changelog-1.0.xml

changelog-master.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog 
  		xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.28.xsd">

    <include file="classpath:/liquibase/changelog/changelog-1.0.xml"/>
</databaseChangeLog>

changelog-1.0.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
		http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.28.xsd">
  
    <changeSet id="createTable t_book" author="xiegb">
        <createTable tableName="t_book">
            <column name="id" type="int(8)" remarks="主键">
                <constraints primaryKey="true"/>
            </column>
            <column name="name" type="varchar(200)" remarks="书名"/>
        </createTable>
    </changeSet>
</databaseChangeLog>
三、 多数据源配置
1. application.yml
yml 复制代码
# 多数据源配置
spring:
  datasource:
    admin:
      username: root
      password: root
      jdbc-url: jdbc:mysql://localhost:3306/admin
      driver-class-name: com.mysql.jdbc.Driver
      liquibase:
        change-log: classpath:liquibase/admin/changelog-master.xml
    web:
      username: root
      password: root
      jdbc-url: jdbc:mysql://localhost:3306/web
      driver-class-name: com.mysql.jdbc.Driver
      liquibase:
        change-log: classpath:liquibase/web/changelog-master.xml
2. 注册JavaBean
java 复制代码
@Configuration
public class LiquibaseConfiguration {

    /**
     *  admin数据库
     */
    @Bean
    public SpringLiquibase adminLiquibase(AdminDataSource dataSource) {
        SpringLiquibase liquibase = new SpringLiquibase();
        // Liquibase文件路径
        liquibase.setChangeLog("classpath:liquibase/admin/changelog-master.xml");
        liquibase.setDataSource(dataSource);
        liquibase.setShouldRun(true);
        liquibase.setResourceLoader(new DefaultResourceLoader());
        // 覆盖Liquibase changelog表名
        liquibase.setDatabaseChangeLogTable("changelog_admin");
        liquibase.setDatabaseChangeLogLockTable("changelog_lock_admin");
        return liquibase;
    }
    
    /**
     *  web数据库
     */
    @Bean
    public SpringLiquibase webLiquibase(WebDataSource dataSource) {
      SpringLiquibase liquibase = new SpringLiquibase();
      liquibase.setChangeLog("classpath:liquibase/web/changelog-master.xml");
      liquibase.setDataSource(dataSource);
      liquibase.setShouldRun(true);
      liquibase.setResourceLoader(new DefaultResourceLoader());
      liquibase.setDatabaseChangeLogTable("changelog_web");
      liquibase.setDatabaseChangeLogLockTable("changelog_lock_web");
      return liquibase;
    }
}
3. changelog-master.xml文件和changelog-1.0.xml
admin/changelog-master.xml
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.28.xsd">

    <include file="classpath:/liquibase/admin/changelog/changelog-1.0.xml"/>
</databaseChangeLog>
web/changelog-master.xml
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.28.xsd">

    <include file="classpath:/liquibase/web/changelog/changelog-1.0.xml"/>
</databaseChangeLog>

changelog-1.0.xml文件同但数据源, 这里不再重复

四. 配置项描述

配置项 默认值 注释
spring.liquibase.change-log classpath:/db/changelog/db.changelog-master.yaml changeLogFile 配置路径
spring.liquibase.check-change-log-location true 是否检查 changelog 配置路径存在
spring.liquibase.contexts 只有指定的 context 的 changelog 才会被执行,多个 context 之间以逗号分隔
spring.liquibase.default-schema 默认数据库
spring.liquibase.liquibase-schema 用于存储 liquibase 对象的数据库
spring.liquibase.liquibase-tablespace 用于 liquibase 对象的表空间
spring.liquibase.database-change-log-table DATABASECHANGELOG 存储数据库改变记录执行情况的表名
spring.liquibase.database-change-log-lock-table DATABASECHANGELOGLOCK 存储当前使用 liquibase 的用户信息表名
spring.liquibase.drop-first false 是否先删除表
spring.liquibase.enabled true 是否启用 liquibase
spring.liquibase.user liquibase 使用的数据库用户名,不指定时使用 spring.datasource 中的
spring.liquibase.password liquibase 使用的数据库用户密码,不指定时使用 spring.datasource 中的
spring.liquibase.url liquibase 使用的数据库url,不指定时使用 spring.datasource 中的
spring.liquibase.labels 指定标签的才会运行,多个标签以逗号分隔
spring.liquibase.parameters changelog 参数
spring.liquibase.rollback-file 当执行升级时写回滚 SQL 的文件
spring.liquibase.test-rollback-on-update 执行更新前是否验证回滚
相关推荐
Sweet锦26 分钟前
不调 Python,不装向量库:我用纯 Java 写了一套以图搜图引擎
java·人工智能·开源·图搜索
打工仔折腾 AI1 小时前
LLaMA 1 到 LLaMA 3 架构演进拆解:从 RoPE、GQA 到词表扩张
人工智能·后端·python·深度学习·langchain·llama
如意猴1 小时前
【C++】007 C/C++ 内存管理机制、malloc与new的区别及模板初阶
java·c++·算法
可乐鸡翅yeah_1 小时前
HLS 分片过期清理,直播旧 TS 分片磁盘爆满问题处理
java·后端·spring·m3u8·m3u8在线·音视频在线播放
张彦峰ZYF2 小时前
加了锁不等于扛得住争用:synchronized、显式锁与 CAS 的语义分层、争用代价账本与选型判据
后端·同步设施·内置锁的四种状态与单向升级·可重入的由来与四条硬边界·显式锁的状态字段与等待队列·读写锁与邮戳锁·一次同步的开销账本
专业程序开发源2 小时前
SSM校园拍摄交流服务平台36936-计算机课程设计、毕业设计
java·spring boot·后端·python·elasticsearch·php·课程设计
Gopher_HBo2 小时前
zap采样器与性能优化内幕
后端
变量探索SEQVEC2 小时前
我埋了 8 个假文件,看谁会上钩:12 天 502 次扫描实录
后端
用户094248568032 小时前
第23章:OpenJDK逃逸分析、标量替换与锁优化
java·jvm
llqbzllll2 小时前
HashMap 的核心结构:从一次 put 看到扩容、桶迁移与树化边界
后端