溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot如何整合liquibase及l(fā)iquibase生成初始化腳本

發(fā)布時間:2022-02-10 14:31:43 來源:億速云 閱讀:1363 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹SpringBoot如何整合liquibase及l(fā)iquibase生成初始化腳本,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

一. SpringBoot集成liquibase

項目集成liquibase作用

  1. 對數(shù)據(jù)庫表字段進行版本控制

  2. 項目初始化部署時初始化數(shù)據(jù)庫表和數(shù)據(jù)

①.導入pom依賴

<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
</dependency>

②.配置application.yml文件,指定master.xml

spring:
  liquibase:
    change-log: classpath:liquibase/master.xml #指定master.xml文件的位置

不同spring版本配置方式不一樣
具體看源碼LiquibaseProperties中配置

SpringBoot如何整合liquibase及l(fā)iquibase生成初始化腳本

③.新建master.xml文件用于中指定數(shù)據(jù)庫初始化腳本的位置

<?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.4.xsd">
    <include file="classpath:liquibase/change_log/init_table.xml" relativeToChangelogFile="false"/>
    <include file="classpath:liquibase/change_log/init_data.xml" relativeToChangelogFile="false"/>
</databaseChangeLog>

④.將數(shù)據(jù)庫表初始腳本init_table.xml和數(shù)據(jù)初始腳本init_data.xml放到項目目錄下

SpringBoot如何整合liquibase及l(fā)iquibase生成初始化腳本

腳本可以通過手寫的方式或者通過liquibase自動生成;

啟動項目如果第一次運行則會在數(shù)據(jù)庫中創(chuàng)建表和數(shù)據(jù)
后面如果腳本中有新增表或者字段啟動項目的時候也會自動創(chuàng)建生成

二. liquibase生成數(shù)據(jù)庫表和數(shù)據(jù)的初始化腳本

liquibase有兩種方式生成初始化腳本

方法一:在官網(wǎng)下載liquibase壓縮包,使用原生的命令行指令來生成

下載liquibase壓縮包,解壓,將mysql連接jar包復制一份到此目錄下

SpringBoot如何整合liquibase及l(fā)iquibase生成初始化腳本

進入解壓目錄執(zhí)行如下執(zhí)行

根據(jù)數(shù)據(jù)庫生成表結(jié)構(gòu)文件

./liquibase --driver=com.mysql.cj.jdbc.Driver --classpath=mysql-connector-java-8.0.17.jar --changeLogFile=./init-data.xml --url="jdbc:mysql://192.168.0.162:3306/hello_world" --username=root --password=123456 --diffTypes=data generateChangeLog

根據(jù)數(shù)據(jù)庫生成初始數(shù)據(jù)文件

./liquibase --driver=com.mysql.cj.jdbc.Driver --classpath=mysql-connector-java-8.0.17.jar --changeLogFile=./init-table.xml --url="jdbc:mysql://192.168.0.162:3306/hello_world" --username=root --password=123456 generateChangeLog
  • 數(shù)據(jù)庫驅(qū)動取決于數(shù)據(jù)庫

&ndash;driver=com.mysql.cj.jdbc.Driver

  • mysql連接

&ndash;classpath=mysql-connector-java-8.0.17.jar

  • 自定義生成的初始化腳本文件名

&ndash;changeLogFile=./init-data.xml

  • 數(shù)據(jù)庫連接地址

&ndash;url=“jdbc:mysql://192.168.0.162:3306/hello_world”

  • 數(shù)據(jù)庫用戶名密碼

-username=root
&ndash;password=123456

  • 生成初始化表數(shù)據(jù)需要加上這個配置,生成表結(jié)構(gòu)則不加

-diffTypes=data

方法二:使用Maven插件

<plugin>
	<groupId>org.liquibase</groupId>
	<artifactId>liquibase-maven-plugin</artifactId>
	<version>3.4.2</version>
	<configuration>
		<changeLogFile>${basedir}/src/main/resources/liquibase/change_log/changelog.xml</changeLogFile>
		<!--changelog文件生成位置-->
		<outputChangeLogFile>${basedir}/src/main/resources/liquibase/change_log/changelog.xml</outputChangeLogFile>
		<!--數(shù)據(jù)庫連接-->
		<driver>com.mysql.jdbc.Driver</driver>
		<url>jdbc:mysql://192.168.0.30:3306/school</url>
		<username>qj</username>
		<password>123456</password>
		<!--輸出文件編碼-->
		<outputFileEncoding>UTF-8</outputFileEncoding>
		<!--執(zhí)行的時候是否顯示詳細的參數(shù)信息-->
		<verbose>true</verbose>
		<!--連接非本地數(shù)據(jù)庫是否彈出提示框-->
		<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
		<!--生成changelog文件內(nèi)容-->
		<diffTypes>tables, views, columns, indexs,foreignkeys, primarykeys, uniqueconstraints, data</diffTypes>
	</configuration>
</plugin>

如果只是生成數(shù)據(jù)庫表腳本,則將上面的diffTypes注釋起來或者去掉里面的data
如果只是生成數(shù)據(jù)腳本,則只留下data
如果要把數(shù)據(jù)表腳本和數(shù)據(jù)腳本生成到一個文件則保留上面的difftypes所有內(nèi)容

安裝好maven插件后maven插件中可以看如下圖的指令,點擊即可生成腳本文件

SpringBoot如何整合liquibase及l(fā)iquibase生成初始化腳本

生成腳本如下

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
          
    <changeSet author="404997819 (generated)" id="1590732067909-4">
        <createTable tableName="t_student">
            <column autoIncrement="true" name="studentId" remarks="學生自增ID" type="INT">
                <constraints primaryKey="true"/>
            </column>
            <column name="classId" remarks="班級ID" type="INT"/>
            <column name="userCode" remarks="用戶唯一碼" type="VARCHAR(20)"/>
            <column name="studentName" remarks="學生姓名" type="VARCHAR(20)"/>
            <column name="studentImageUrl" remarks="學生頭像地址" type="VARCHAR(200)"/>
            <column name="studentCode" remarks="學生學號" type="VARCHAR(50)"/>
            <column name="IDCard" remarks="身份證號" type="VARCHAR(50)"/>
            <column name="status" remarks="學生狀態(tài) 1:在讀 0:畢業(yè) -1:轉(zhuǎn)校" type="VARCHAR(5)"/>
            <column name="flag" remarks="是否刪除 1:正常顯示,-1:表示刪除" type="VARCHAR(10)"/>
            <column name="createDate" remarks="創(chuàng)建時間" type="datetime"/>
        </createTable>
    </changeSet>
    
    <changeSet author="404997819 (generated)" id="1590732067909-6">
        <createTable tableName="t_teacherRelation">
            <column autoIncrement="true" name="teacherRelationId" remarks="主鍵自增ID" type="INT">
                <constraints primaryKey="true"/>
            </column>
            <column name="classId" remarks="班級ID" type="INT"/>
            <column name="teacherId" remarks="教師ID" type="INT"/>
            <column name="teacherType" remarks="教師類型 1:班主任" type="VARCHAR(10)"/>
            <column name="flag" remarks="是否刪除 1:正常顯示,-1:表示刪除" type="VARCHAR(10)"/>
            <column name="createDate" remarks="創(chuàng)建時間" type="datetime"/>
        </createTable>
    </changeSet>
   
    <changeSet author="404997819 (generated)" id="1590732067909-10">
        <createIndex indexName="Reft_userinfo88" tableName="t_api_logs">
            <column name="apiToken"/>
        </createIndex>
    </changeSet>
</databaseChangeLog>

以上是“SpringBoot如何整合liquibase及l(fā)iquibase生成初始化腳本”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI