LiquiBase概述及(spring boot 入門配置)
阿新 • • 發佈:2019-02-13
官網:http://www.liquibase.org/
Liquibase是一個用於跟蹤、管理和應用資料庫變化的開源的資料庫重構工具。它將所有資料庫的變化(包括結構和資料)都儲存在XML檔案中,便於版本控制。
Liquibase具備如下特性:
- 不依賴於特定的資料庫,目前支援包括Oracle/Sql Server/DB2/MySql/Sybase/PostgreSQL/Caché等12種資料庫,這樣在資料庫的部署和升級環節可幫助應用系統支援多資料庫。
- 提供資料庫比較功能,比較結果儲存在XML中,基於該XML你可用Liquibase輕鬆部署或升級資料庫。
- 以XML儲存資料庫變化,其中以作者和ID唯一標識一個變化(ChangSet),支援資料庫變化的合併,因此支援多開發人員同時工作。
- 在資料庫中儲存資料庫修改歷史(DatabaseChangeHistory),在資料庫升級時自動跳過已應用的變化(ChangSet)。
- 提供變化應用的回滾功能,可按時間、數量或標籤(tag)回滾已應用的變化。通過這種方式,開發人員可輕易的還原資料庫在任何時間點的狀態。
- 可生成資料庫修改文件(HTML格式)
- 提供資料重構的獨立的IDE和Eclipse外掛。
LiquiBase的spring boot 入門配置
pom.xml
<!-- https://mvnrepository.com/artifact/org.liquibase/liquibase-core -->
<dependency >
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>3.5.3</version>
</dependency>
增加 LiquibaseConfig.java
package com.aop8.config;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import liquibase.integration.spring.SpringLiquibase;
@Configuration
public class LiquibaseConfig {
@Bean
public SpringLiquibase liquibase(DataSource dataSource) {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setDataSource(dataSource);
liquibase.setChangeLog("classpath:config/liquibase/master.xml");
liquibase.setContexts("development,test,production");
liquibase.setShouldRun(true);
return liquibase;
}
}
增加 master.xml
路徑:src/main/resources 目錄下 config/liquibase/master.xml
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<include file="classpath:config/liquibase/changelog/201712022057_add_entity_Base.xml" relativeToChangelogFile="false"/>
</databaseChangeLog>
201712022057_add_entity_Base.xml
路徑:
src/main/resources 目錄下
config/liquibase/changelog/201712022057_add_entity_Base.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.1.xsd">
<property name="now" value="now()" dbms="mysql"/>
<changeSet id="20171202205" author="WATER">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="tb_member"/>
</not>
</preConditions>
<createTable tableName="tb_member">
<column name="id" type="bigint" autoIncrement="true" remarks="主鍵" >
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="mobile" type="varchar(50)" remarks="手機號" >
<constraints nullable="false" />
</column>
<column name="real_name" type="varchar(50)" remarks="姓名" >
<constraints nullable="false" />
</column>
<column name="introduce" type="varchar(255)" remarks="說明" >
<constraints nullable="false" />
</column>
<column name="status" type="varchar(10)" remarks="賬號狀態" />
<column name="created_by" type="varchar(50)" remarks="建立人" >
<constraints nullable="false"/>
</column>
<column name="created_date" type="timestamp" defaultValueDate="${now}" remarks="建立時間" >
<constraints nullable="false"/>
</column>
<column name="last_modified_by" type="varchar(50)"/>
<column name="last_modified_date" type="timestamp"/>
</createTable>
</changeSet>
</databaseChangeLog>