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 执行更新前是否验证回滚
相关推荐
yxlalm4 小时前
Spring AI+RAG 01-项目背景与技术选型
java·人工智能·spring
程序员清风5 小时前
Java 后端高并发设计:线程池、限流、熔断与降级
java·数据库·oracle
码事漫谈5 小时前
多人共用一个 key,缓存命中率会不会因此降低?
后端
Seoyoneh5 小时前
Agentic Workflow编排架构:云客服从“被动响应”迈向“主动执行”的技术实现
java·开发语言·架构
海南java第二人5 小时前
对外 API 如何保证幂等性?插入数据 / 上传表格场景全方案汇总
java·幂等性
tryxr6 小时前
Chat2Excel 文件服务模块上传文件功能开发
java·项目·oss·easyexcel·文件服务
Tangyuewei6 小时前
Java 写 Agent:模型只是组件
java·开发语言
考虑考虑6 小时前
docker compose环境变量替换
运维·后端·自动化运维
彧azz7 小时前
Java学习语法篇:变量
java·学习
武雄(小星Ai)7 小时前
飞书API上传26MB文件偶发失败:错误码9499与空响应体排坑实录
后端·api·排坑实录