SpringBoot整合liquibase的實(shí)現(xiàn)方法
LiquiBase 是一個(gè)用于數(shù)據(jù)庫重構(gòu)和遷移的開源工具,通過日志文件的形式記錄數(shù)據(jù)庫的變更,然后執(zhí)行日志文件中的修改,將數(shù)據(jù)庫更新或回滾到一致的狀態(tài)。它的目標(biāo)是提供一種數(shù)據(jù)庫類型無關(guān)的解決方案,通過執(zhí)行schema類型的文件來達(dá)到遷移。其有點(diǎn)主要有以下:
- 支持幾乎所有主流的數(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>
二、指定配置文件位置
在代碼中新建一個(gè) 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的位置,這里使用的一個(gè)master文件引用其他文件的方式
liquibase.setChangeLog("classpath:liquibase/master.xml");
liquibase.setContexts("development,test,production");
liquibase.setShouldRun(true);
return liquibase;
}
}
三、編寫配置文件
目錄結(jié)構(gòu):

src/main/resources 下新建一個(gè)文件夾: 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)簽可以把一個(gè)文件夾下的所有 changelog 都加載進(jìn)來。如果單個(gè)加載可以用 include 。
includeAll 標(biāo)簽里有兩個(gè)屬性: 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。
這里新建一個(gè) 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="項(xiàng)目id">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="project_name" type="varchar(255)" encoding="utf8" remarks="項(xiàng)目名字"/>
<column name="project_difficulty" type="float" encoding="utf8" remarks="項(xiàng)目難度"/>
<column name="category_id" type="varchar(64)" encoding="utf8" remarks="項(xiàng)目類型類目編號(hào)"/>
<column name="project_status" type="int(11)" encoding="utf8" remarks="項(xiàng)目狀態(tài), 0招募中,1 進(jìn)行中,2已完成,3失敗,4延期,5刪除"/>
<column name="project_desc" type="varchar(512)" encoding="utf8" remarks="項(xiàng)目簡介"/>
<column name="project_creater_id" type="varchar(64)" encoding="utf8" remarks="項(xiàng)目創(chuàng)建者id"/>
<column name="team_id" type="varchar(64)" encoding="utf8" remarks="項(xiàng)目所屬團(tuán)隊(duì)id"/>
<column name="create_time" type="bigint(64)" encoding="utf8" remarks="創(chuàng)建時(shí)間"/>
<column name="update_time" type="bigint(64)" encoding="utf8" remarks="更新時(shí)間"/>
</createTable>
</changeSet>
<changeSet id="20190713-02" author="solo">
<createTable tableName="project_category" remarks="項(xiàng)目類型表">
<column name="id" type="varchar(64)" remarks="項(xiàng)目類型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="項(xiàng)目點(diǎn)贊表">
<column name="id" type="varchar(64)" remarks="主鍵id">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="project_id" type="varchar(64)" remarks="項(xiàng)目id"/>
<column name="user_id" type="varchar(64)" remarks="點(diǎn)贊的用戶id"/>
<column name="status" type="int(11)" remarks="點(diǎn)贊狀態(tài),0 取消點(diǎn)贊,1點(diǎn)贊"/>
<column name="type" type="int(11)" remarks="類型 1點(diǎn)贊"/>
<column name="create_time" type="bigint(64)" remarks="創(chuàng)建時(shí)間"/>
<column name="update_time" type="bigint(64)" remarks="更新時(shí)間"/>
</createTable>
</changeSet>
<changeSet id="20190713-04" author="solo">
<createTable tableName="project_picture" remarks="項(xiàng)目圖片表">
<column name="id" type="varchar(64)" remarks="圖片id">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="project_id" type="varchar(64)" remarks="項(xiàng)目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>
如果你的項(xiàng)目一開始就用了 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>
直接把項(xiàng)目導(dǎo)出的數(shù)據(jù)庫文件 project.sql 通過 include 標(biāo)簽引進(jìn)來。
以上就是 SpringBoot 整合 Liquibase 的全部內(nèi)容。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Maven打包所有依賴到一個(gè)可執(zhí)行jar中遇到的問題
這篇文章主要給大家介紹了關(guān)于Maven打包所有依賴到一個(gè)可執(zhí)行jar中遇到的問題,將依賴打入jar包,由于maven管理了所有的依賴,所以將項(xiàng)目的代碼和依賴打成一個(gè)包對它來說是順理成章的功能,需要的朋友可以參考下2023-10-10
idea插件之mybatis log plugin控制臺(tái)sql的問題
這篇文章主要介紹了idea插件之mybatis log plugin控制臺(tái)sql,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
在2023idea中實(shí)現(xiàn)SpringBoot的IoC和AOP的方法
這篇文檔詳細(xì)介紹了如何在Spring Boot中實(shí)現(xiàn)IoC(控制反轉(zhuǎn))和AOP(面向切面編程),深入探討了AOP的基本概念,包括AOP的作用、優(yōu)勢以及實(shí)現(xiàn)方式,最后,它提到了AOP的注解,如@Aspect、@Pointcut、@Before、@After、@AfterReturning、@AfterThrowing和@Around2024-11-11
Java實(shí)現(xiàn)控制小數(shù)精度的方法
這篇文章主要介紹了Java實(shí)現(xiàn)控制小數(shù)精度的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Springboot啟動(dòng)后執(zhí)行方法小結(jié)
本文主要介紹了Springboot啟動(dòng)后執(zhí)行方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
SpringBoot2 整合FreeMarker實(shí)現(xiàn)頁面靜態(tài)化示例詳解
這篇文章主要介紹了SpringBoot2 整合FreeMarker實(shí)現(xiàn)頁面靜態(tài)化示例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
IDEA使用JDBC安裝配置jar包連接MySQL數(shù)據(jù)庫
這篇文章介紹了IDEA使用JDBC安裝配置jar包連接MySQL數(shù)據(jù)庫的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01

