SpringBoot整合liquibase的實現方法

這篇文章主要介紹了SpringBoot整合liquibase的實現方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨着小編來一起學習學習吧

LiquiBase 是一個用於數據庫重構和遷移的開源工具,通過日誌文件的形式記錄數據庫的變更,然後執行日誌文件中的修改,將數據庫更新或回滾到一致的狀態。它的目標是提供一種數據庫類型無關的解決方案,通過執行schema類型的文件來達到遷移。其有點主要有以下:

  • 支持幾乎所有主流的數據庫,如MySQL, PostgreSQL, Oracle, Sql Server, DB2等;
  • 支持多開發者的協作維護;
  • 日誌文件支持多種格式,如XML, YAML, JSON, SQL等;
  • 支持多種運行方式,如命令行、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;
 }

}

三、編寫配置文件

目錄結構:

src/main/resources 下新建一個文件夾: liquibase ,用來存放跟 liquibase 相關的文件。

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 標籤可以把一個文件夾下的所有 changelog 都加載進來。如果單個加載可以用 include

includeAll 標籤裏有兩個屬性: pathrelativeToChangelogFile

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 標籤裏是 file):指定要加載的文件或文件夾位置

relativeToChangelogFile :文件位置的路徑是否相對於 root changelog 是相對路徑,默認 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="項目狀態, 0招募中,1 進行中,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="項目創建者id"/>
   <column name="team_id" type="varchar(64)" encoding="utf8" remarks="項目所屬團隊id"/>
   <column name="create_time" type="bigint(64)" encoding="utf8" remarks="創建時間"/>
   <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="狀態。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="點贊狀態,0 取消點贊,1點贊"/>
   <column name="type" type="int(11)" remarks="類型 1點贊"/>
   <column name="create_time" type="bigint(64)" remarks="創建時間"/>
   <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,可以把以前的數據庫導出成 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>

直接把項目導出的數據庫文件 project.sql 通過 include 標籤引進來。

以上就是 SpringBoot 整合 Liquibase 的全部內容。希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章