美文网首页
SpringBoot 整合 Liquibase

SpringBoot 整合 Liquibase

作者: 任未然 | 来源:发表于2023-11-20 16:10 被阅读0次

一. 概述

在日常开发多版本迭代中,更新版本一般都会涉及数据库脚本更新,每次更新版本都要找出更新的脚本手动执行,这种人工行为往往会出现漏执行脚本问题。哪有没有一种程序自动自动增量脚本的方式呢?Liquibase就能很好解决这种问题

二. springBoot整合Liquibase流程

2.1 引入依赖包

<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
    <version>最新版本</version>
</dependency>

2.2 配置

2.2.1 单数据源模式

2.2.1.1 配置资源文件

image.png
1. master.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-3.5.xsd">

    <include file="liquibase/changelog/000_initial_schema.xml" />

</databaseChangeLog>
2. 000_inintial_schema.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-3.5.xsd">

    <changeSet id="00000000000001" author="任未然">
        <sqlFile path="liquibase/data/init_schema.sql"/>
    </changeSet>

</databaseChangeLog>
3. init_schema.sql
create table if not exists demo
(
    id          varchar(64) not null comment '主键id'
        primary key,
    name        varchar(32) null comment '名称',
    content     varchar(64) null comment '内容'
)
    comment 'demo';

2.2.1.2 yml配置

spring:
  # liquibase配置
  liquibase:
    change-log: classpath:liquibase/master.xml
    enabled: true

2.2.1.3 注册 liquibase bean

@Configuration
public class LiquibaseConfiguration{

    @Bean
    @ConfigurationProperties(prefix = "spring.liquibase")
    public LiquibaseProperties getLiquibaseProperties() {
        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;
    }
}

2.2.2 多数据源模式

2.2.2.1 配置资源文件

每个数据源配置都独立配置一套资源文件

image.png

2.2.2.2 yml配置

spring:
  datasource:
    datasource1:
      username: root
      password: 123456
      jdbc-url: jdbc:mysql://localhost:3306/admin
      driver-class-name: com.mysql.jdbc.Driver
      liquibase:
        change-log: classpath:liquibase/datasource1/master.xml
    datasource2:
      username: root
      password: 123456
      jdbc-url: jdbc:mysql://localhost:3306/web
      driver-class-name: com.mysql.jdbc.Driver
      liquibase:
        change-log: classpath:liquibase/datasource2/master.xml

2.2.2.3 注册 liquibase bean

@Configuration
public class LiquibaseConfiguration {


    /**
     * datasource1
     */
    @Bean
    public SpringLiquibase datasource1Liquibase() {
        // 获取对应数据源
        DataSource dataSource;
        SpringLiquibase liquibase = new SpringLiquibase();
        // Liquibase文件路径
        liquibase.setChangeLog("classpath:liquibase/datasource1/master.xml");
        liquibase.setDataSource(dataSource);
        liquibase.setShouldRun(true);
        liquibase.setResourceLoader(new DefaultResourceLoader());
        // 覆盖Liquibase changelog表名
        liquibase.setDatabaseChangeLogTable("datasource1_changelog_table");
        liquibase.setDatabaseChangeLogLockTable("datasource1_changelog_lock_table");
        return liquibase;
    }

    /**
     * datasource2
     */
    @Bean
    public SpringLiquibase datasource2Liquibase() {
        // 获取对应数据源
        DataSource dataSource;
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setChangeLog("classpath:classpath:liquibase/datasource2/master.xml");
        liquibase.setDataSource(dataSource);
        liquibase.setShouldRun(true);
        liquibase.setResourceLoader(new DefaultResourceLoader());
        liquibase.setDatabaseChangeLogTable("datasource2_changelog_table");
        liquibase.setDatabaseChangeLogLockTable("datasource2_changelog_lock_table");
        return liquibase;
    }
}

相关文章

网友评论

      本文标题:SpringBoot 整合 Liquibase

      本文链接:https://www.haomeiwen.com/subject/ssyvwdtx.html