一. 概述
在日常開發(fā)多版本迭代中,更新版本一般都會涉及數(shù)據(jù)庫腳本更新,每次更新版本都要找出更新的腳本手動執(zhí)行,這種人工行為往往會出現(xiàn)漏執(zhí)行腳本問題。哪有沒有一種程序自動自動增量腳本的方式呢?Liquibase就能很好解決這種問題
二. springBoot整合Liquibase流程
2.1 引入依賴包
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>最新版本</version>
</dependency>
2.2 配置
2.2.1 單數(shù)據(jù)源模式
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>
<changeSet id="00000000000002" author="任未然">
<preConditions onFail="MARK_RAN">
<not>
<columnExists tableName="demo_table" columnName="name"/>
</not>
</preConditions>
<addColumn tableName="demo_table">
<column name="name" type="varchar(25)" remarks="名字"/>
</addColumn>
</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 '內(nèi)容'
)
comment 'demo';
2.2.1.2 yml配置
spring:
# liquibase配置
liquibase:
# 是否啟動
enabled: true
# 日志配置路徑
change-log: classpath:liquibase/master.xml
# 用于跟蹤更改歷史記錄的表的名稱
databaseChangeLogTable: databasechangelog
# 用于跟蹤并發(fā) Liquibase 使用情況的表的名稱
databaseChangeLogLockTable: databasechangeloglock
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 多數(shù)據(jù)源模式
2.2.2.1 配置資源文件
每個數(shù)據(jù)源配置都獨(dú)立配置一套資源文件

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() {
// 獲取對應(yīng)數(shù)據(jù)源
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() {
// 獲取對應(yīng)數(shù)據(jù)源
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;
}
}