最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot整合liquibase的實現(xiàn)方法

 更新時間:2019年08月18日 11:06:15   作者:solocoder  
這篇文章主要介紹了SpringBoot整合liquibase的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

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

  • 支持幾乎所有主流的數(shù)據(jù)庫,如MySQL, PostgreSQL, Oracle, Sql Server, DB2等;
  • 支持多開發(fā)者的協(xié)作維護;
  • 日志文件支持多種格式,如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="項目狀態(tài), 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="項目創(chuàng)建者id"/>
   <column name="team_id" type="varchar(64)" encoding="utf8" remarks="項目所屬團隊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ù)庫導出成 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>

直接把項目導出的數(shù)據(jù)庫文件 project.sql 通過 include 標簽引進來。

以上就是 SpringBoot 整合 Liquibase 的全部內(nèi)容。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 基于Spring Security實現(xiàn)對密碼進行加密和校驗

    基于Spring Security實現(xiàn)對密碼進行加密和校驗

    我們在入門案例中,其實已經(jīng)是一個非常簡單的認證,但是用戶名是寫死的,密碼也需要從控制臺查看,很顯然實際中并不能這么做,下面的學習中,我們來實現(xiàn)基于內(nèi)存模型的認證以及用戶的自定義認證,密碼加密等內(nèi)容,需要的朋友可以參考下
    2024-07-07
  • springboot項目關閉swagger如何防止漏洞掃描

    springboot項目關閉swagger如何防止漏洞掃描

    這篇文章主要介紹了springboot項目關閉swagger如何防止漏洞掃描,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • 消息中間件詳解以及比較選擇

    消息中間件詳解以及比較選擇

    這篇文章主要介紹了消息中間件詳解以及比較選擇,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Java實現(xiàn)簡單版貪吃蛇游戲

    Java實現(xiàn)簡單版貪吃蛇游戲

    這篇文章主要為大家詳細介紹了Java實現(xiàn)簡單版貪吃蛇游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 使用IntelliJ IDEA查看類的繼承關系圖形(圖文詳解)

    使用IntelliJ IDEA查看類的繼承關系圖形(圖文詳解)

    這篇文章主要介紹了使用IntelliJ IDEA查看類的繼承關系圖形,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的工作或?qū)W習具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Spring JDBC的使用詳解

    Spring JDBC的使用詳解

    這篇文章主要介紹了Spring JDBC的使用詳解,幫助大家更好的理解和學習使用SpringBoot框架,感興趣的朋友可以了解下
    2021-05-05
  • 詳解Java中的BigDecimal

    詳解Java中的BigDecimal

    這篇文章主要介紹了Java中的BigDecimal的使用方法,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-09-09
  • SpringBoot框架打包體積簡化過程圖解

    SpringBoot框架打包體積簡化過程圖解

    這篇文章主要介紹了SpringBoot框架打包體積簡化過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • 在SpringBoot中如何利用Redis實現(xiàn)互斥鎖

    在SpringBoot中如何利用Redis實現(xiàn)互斥鎖

    當我們利用Redis存儲熱點數(shù)據(jù)時,突然就過期失效或者被刪除了,導致大量請求同時訪問數(shù)據(jù)庫,增加了數(shù)據(jù)庫的負載,為減輕數(shù)據(jù)庫的負載我們利用互斥鎖,本文重點介紹在SpringBoot中如何利用Redis實現(xiàn)互斥鎖,感興趣的朋友一起看看吧
    2023-09-09
  • JVM內(nèi)存溢出和內(nèi)存泄漏的區(qū)別及說明

    JVM內(nèi)存溢出和內(nèi)存泄漏的區(qū)別及說明

    這篇文章主要介紹了JVM內(nèi)存溢出和內(nèi)存泄漏的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02

最新評論

黄山市| 库伦旗| 柞水县| 成武县| 增城市| 新营市| 吉林市| 禄劝| 宝清县| 浠水县| 全州县| 田阳县| 湄潭县| 嘉禾县| 连城县| 江津市| 和田县| 昌都县| 确山县| 浦城县| 宜州市| 无棣县| 抚远县| 共和县| 耒阳市| 富川| 广安市| 梁山县| 原平市| 南部县| 河曲县| 周口市| 观塘区| 米易县| 阳曲县| 清丰县| 灵寿县| 濮阳县| 府谷县| 桐柏县| 永善县|