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 执行更新前是否验证回滚
相关推荐
学Linux的语莫2 小时前
python项目打包为镜像
java·python·spring
秋刀鱼程序编程2 小时前
Java编程基础入门(四)---选择循环语句
java·开发语言·算法
一条咸鱼_SaltyFish2 小时前
WebFlux vs MVC:Gateway集成若依框架的技术选型之争
java·开发语言·微服务·gateway·mvc·开源软件·webflux
独自归家的兔3 小时前
Java反射之根:Class类生成机制深度剖析与最佳实践
java·开发语言
悟能不能悟3 小时前
Gson bean getxxx,怎么才能返回给前端
java·前端
Apex Predator3 小时前
本地库导入到nexus
java·服务器·前端
仍然.3 小时前
Java---反射、枚举、lambda表达式 和 泛型进阶
java·开发语言
我爱娃哈哈3 小时前
SpringBoot + MinIO + 阿里云 OSS:文件上传下载、分片断点续传全链路方案
spring boot·后端·阿里云
小北方城市网3 小时前
JVM 调优实战指南:从问题排查到参数优化
java·spring boot·python·rabbitmq·java-rabbitmq·数据库架构