SpringBoot整合liquibase的實現(xiàn)方法
LiquiBase 是一個用于數(shù)據(jù)庫重構(gòu)和遷移的開源工具,通過日志文件的形式記錄數(shù)據(jù)庫的變更,然后執(zhí)行日志文件中的修改,將數(shù)據(jù)庫更新或回滾到一致的狀態(tài)。它的目標(biāo)是提供一種數(shù)據(jù)庫類型無關(guān)的解決方案,通過執(zhí)行schema類型的文件來達(dá)到遷移。其有點主要有以下:
- 支持幾乎所有主流的數(shù)據(jù)庫,如MySQL, PostgreSQL, Oracle, Sql Server, DB2等;
- 支持多開發(fā)者的協(xié)作維護(hù);
- 日志文件支持多種格式,如XML, YAML, JSON, SQL等;
- 支持多種運(yùn)行方式,如命令行、Spring集成、Maven插件、Gradle插件等。
liquibase 官方文檔地址: http://www.liquibase.org/documentation/index.html
一、引入依賴
先在 pom 文件里引入依賴
<dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> </dependency>
二、指定配置文件位置
在代碼中新建一個 LiquibaseConfig 類,用于配置 Liquibase ,指定配置文件的位置。
import javax.sql.DataSource;
import liquibase.integration.spring.SpringLiquibase;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class LiquibaseConfig {
@Bean
public SpringLiquibase liquibase(DataSource dataSource) {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setDataSource(dataSource);
//指定changelog的位置,這里使用的一個master文件引用其他文件的方式
liquibase.setChangeLog("classpath:liquibase/master.xml");
liquibase.setContexts("development,test,production");
liquibase.setShouldRun(true);
return liquibase;
}
}
三、編寫配置文件
目錄結(jié)構(gòu):

src/main/resources 下新建一個文件夾: liquibase ,用來存放跟 liquibase 相關(guān)的文件。
master.xml
然后在 liquibase 文件夾下新建 master.xml 作為主文件。
<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"> <includeAll path="liquibase/changelogs/" relativeToChangelogFile="false"/> </databaseChangeLog>
includeAll 標(biāo)簽可以把一個文件夾下的所有 changelog 都加載進(jìn)來。如果單個加載可以用 include 。
includeAll 標(biāo)簽里有兩個屬性: path 和 relativeToChangelogFile 。
| Attribute | Description |
|---|---|
| file | Name of the file to import required |
| relativeToChangelogFile | Is the file path relative to the root changelog file rather than to the classpath. Defaults to "false" since 1.9 |
path (在 include 標(biāo)簽里是 file):指定要加載的文件或文件夾位置
relativeToChangelogFile :文件位置的路徑是否相對于 root changelog 是相對路徑,默認(rèn) false,即相對于 classpath 是相對路徑。
changelog
另在 liquibase 文件夾下新建 changelogs 文件夾用來存放 changelog。
這里新建一個 changelog-1.0.xml
<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">
<changeSet id="20190713-01" author="solo">
<createTable tableName="project_info">
<column name="project_id" type="varchar(64)" encoding="utf8" remarks="項目id">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="project_name" type="varchar(255)" encoding="utf8" remarks="項目名字"/>
<column name="project_difficulty" type="float" encoding="utf8" remarks="項目難度"/>
<column name="category_id" type="varchar(64)" encoding="utf8" remarks="項目類型類目編號"/>
<column name="project_status" type="int(11)" encoding="utf8" remarks="項目狀態(tài), 0招募中,1 進(jìn)行中,2已完成,3失敗,4延期,5刪除"/>
<column name="project_desc" type="varchar(512)" encoding="utf8" remarks="項目簡介"/>
<column name="project_creater_id" type="varchar(64)" encoding="utf8" remarks="項目創(chuàng)建者id"/>
<column name="team_id" type="varchar(64)" encoding="utf8" remarks="項目所屬團(tuán)隊id"/>
<column name="create_time" type="bigint(64)" encoding="utf8" remarks="創(chuàng)建時間"/>
<column name="update_time" type="bigint(64)" encoding="utf8" remarks="更新時間"/>
</createTable>
</changeSet>
<changeSet id="20190713-02" author="solo">
<createTable tableName="project_category" remarks="項目類型表">
<column name="id" type="varchar(64)" remarks="項目類型id">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="varchar(255)" remarks="類目類型名稱"/>
<column name="status" type="int(11)" remarks="狀態(tài)。1正常,2刪除"/>
<column name="remark" type="varchar(255)" remarks="備注"/>
</createTable>
</changeSet>
<changeSet id="20190713-03" author="solo">
<createTable tableName="project_like_user" remarks="項目點贊表">
<column name="id" type="varchar(64)" remarks="主鍵id">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="project_id" type="varchar(64)" remarks="項目id"/>
<column name="user_id" type="varchar(64)" remarks="點贊的用戶id"/>
<column name="status" type="int(11)" remarks="點贊狀態(tài),0 取消點贊,1點贊"/>
<column name="type" type="int(11)" remarks="類型 1點贊"/>
<column name="create_time" type="bigint(64)" remarks="創(chuàng)建時間"/>
<column name="update_time" type="bigint(64)" remarks="更新時間"/>
</createTable>
</changeSet>
<changeSet id="20190713-04" author="solo">
<createTable tableName="project_picture" remarks="項目圖片表">
<column name="id" type="varchar(64)" remarks="圖片id">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="project_id" type="varchar(64)" remarks="項目id"/>
<column name="picture_url" type="varchar(64)" remarks="圖片地址"/>
<column name="picture_url_32" type="varchar(64)" remarks="圖片地址32位"/>
<column name="picture_url_64" type="varchar(64)" remarks="圖片地址64位"/>
</createTable>
</changeSet>
</databaseChangeLog>
如果你的項目一開始就用了 liquibase,那可以像上面這樣寫,把建表語句都寫在 changelog 里。
如果一開始沒用,后期想引入 liquibase,可以把以前的數(shù)據(jù)庫導(dǎo)出成 sql,然后引入 sql 文件。方式如下:
<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"> <include file="liquibase/changelogs/project.sql" relativeToChangelogFile="false"/> </databaseChangeLog>
直接把項目導(dǎo)出的數(shù)據(jù)庫文件 project.sql 通過 include 標(biāo)簽引進(jìn)來。
以上就是 SpringBoot 整合 Liquibase 的全部內(nèi)容。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Spring Security實現(xiàn)對密碼進(jìn)行加密和校驗
我們在入門案例中,其實已經(jīng)是一個非常簡單的認(rèn)證,但是用戶名是寫死的,密碼也需要從控制臺查看,很顯然實際中并不能這么做,下面的學(xué)習(xí)中,我們來實現(xiàn)基于內(nèi)存模型的認(rèn)證以及用戶的自定義認(rèn)證,密碼加密等內(nèi)容,需要的朋友可以參考下2024-07-07
springboot項目關(guān)閉swagger如何防止漏洞掃描
這篇文章主要介紹了springboot項目關(guān)閉swagger如何防止漏洞掃描,本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-05-05
使用IntelliJ IDEA查看類的繼承關(guān)系圖形(圖文詳解)
這篇文章主要介紹了使用IntelliJ IDEA查看類的繼承關(guān)系圖形,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的工作或?qū)W習(xí)具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
在SpringBoot中如何利用Redis實現(xiàn)互斥鎖
當(dāng)我們利用Redis存儲熱點數(shù)據(jù)時,突然就過期失效或者被刪除了,導(dǎo)致大量請求同時訪問數(shù)據(jù)庫,增加了數(shù)據(jù)庫的負(fù)載,為減輕數(shù)據(jù)庫的負(fù)載我們利用互斥鎖,本文重點介紹在SpringBoot中如何利用Redis實現(xiàn)互斥鎖,感興趣的朋友一起看看吧2023-09-09
JVM內(nèi)存溢出和內(nèi)存泄漏的區(qū)別及說明
這篇文章主要介紹了JVM內(nèi)存溢出和內(nèi)存泄漏的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02

