SpringBoot整合liquibase及l(fā)iquibase生成初始化腳本的方式
一. SpringBoot集成liquibase
項目集成liquibase作用
- 對數(shù)據(jù)庫表字段進行版本控制
- 項目初始化部署時初始化數(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中配置

③.新建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放到項目目錄下

腳本可以通過手寫的方式或者通過liquibase自動生成;
啟動項目如果第一次運行則會在數(shù)據(jù)庫中創(chuàng)建表和數(shù)據(jù)
后面如果腳本中有新增表或者字段啟動項目的時候也會自動創(chuàng)建生成
二. liquibase生成數(shù)據(jù)庫表和數(shù)據(jù)的初始化腳本
liquibase有兩種方式生成初始化腳本
方法一:在官網(wǎng)下載liquibase壓縮包,使用原生的命令行指令來生成
下載liquibase壓縮包,解壓,將mysql連接jar包復制一份到此目錄下

進入解壓目錄執(zhí)行如下執(zhí)行
根據(jù)數(shù)據(jù)庫生成表結構文件
./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ù)庫驅動取決于數(shù)據(jù)庫
–driver=com.mysql.cj.jdbc.Driver
- mysql連接
–classpath=mysql-connector-java-8.0.17.jar
- 自定義生成的初始化腳本文件名
–changeLogFile=./init-data.xml
- 數(shù)據(jù)庫連接地址
–url=“jdbc:mysql://192.168.0.162:3306/hello_world”
- 數(shù)據(jù)庫用戶名密碼
-username=root
–password=123456
- 生成初始化表數(shù)據(jù)需要加上這個配置,生成表結構則不加
-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文件內容-->
<diffTypes>tables, views, columns, indexs,foreignkeys, primarykeys, uniqueconstraints, data</diffTypes>
</configuration>
</plugin>如果只是生成數(shù)據(jù)庫表腳本,則將上面的diffTypes注釋起來或者去掉里面的data
如果只是生成數(shù)據(jù)腳本,則只留下data
如果要把數(shù)據(jù)表腳本和數(shù)據(jù)腳本生成到一個文件則保留上面的difftypes所有內容
安裝好maven插件后maven插件中可以看如下圖的指令,點擊即可生成腳本文件

生成腳本如下
<?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:轉校" 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>參考文章如下
http://blog.jiunile.com/%E6%95%B0%E6%8D%AE%E5%BA%93%E7%89%88%E6%9C%AC%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B7liquibase.html
https://blog.csdn.net/cover1231988/article/details/78124673?utm_source=blogxgwz5
https://www.cnblogs.com/tonyq/p/8039770.html
https://www.jianshu.com/p/07a45b6722fd
到此這篇關于SpringBoot整合liquibase的文章就介紹到這了,更多相關SpringBoot整合liquibase內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java解析http協(xié)議字符串的方法實現(xiàn)
本文主要介紹了Java解析http協(xié)議字符串的方法實現(xiàn),我們探討了如何使用Java解析HTTP協(xié)議字符串,并將其封裝成了一個HttpRequest類,具有一定的參考價值,感興趣的可以了解一下2023-09-09
Java生成二維碼的兩種實現(xiàn)方式(基于Spring?Boot)
這篇文章主要給大家介紹了關于Java生成二維碼的兩種實現(xiàn)方式,文中的代碼基于Spring?Boot,本文基于JAVA環(huán)境,以SpringBoot框架為基礎開發(fā),文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-07-07
使用Java實現(xiàn)類似Comet風格的web app
這篇文章主要介紹了使用Java實現(xiàn)類似Comet風格的web app的方法,包括客戶端的響應和XML解析等功能,需要的朋友可以參考下2015-11-11
Java的MyBatis框架中關鍵的XML字段映射的配置參數(shù)詳解
將XML文件的schema字段映射到數(shù)據(jù)庫的schema是我們操作數(shù)據(jù)庫的常用手段,這里我們就來整理一些Java的MyBatis框架中關鍵的XML字段映射的配置參數(shù)詳解,需要的朋友可以參考下2016-06-06
java連接MySQL數(shù)據(jù)庫實現(xiàn)代碼
這篇文章主要為大家詳細介紹了java連接MySQL數(shù)據(jù)庫實現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-06-06

