SpringBoot整合liquibase及l(fā)iquibase生成初始化腳本的方式
一. SpringBoot集成liquibase
項(xiàng)目集成liquibase作用
- 對(duì)數(shù)據(jù)庫(kù)表字段進(jìn)行版本控制
- 項(xiàng)目初始化部署時(shí)初始化數(shù)據(jù)庫(kù)表和數(shù)據(jù)
①.導(dǎo)入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ù)庫(kù)初始化腳本的位置
<?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ù)庫(kù)表初始腳本init_table.xml和數(shù)據(jù)初始腳本init_data.xml放到項(xiàng)目目錄下
腳本可以通過(guò)手寫的方式或者通過(guò)liquibase自動(dòng)生成;
啟動(dòng)項(xiàng)目如果第一次運(yùn)行則會(huì)在數(shù)據(jù)庫(kù)中創(chuàng)建表和數(shù)據(jù)
后面如果腳本中有新增表或者字段啟動(dòng)項(xiàng)目的時(shí)候也會(huì)自動(dòng)創(chuàng)建生成
二. liquibase生成數(shù)據(jù)庫(kù)表和數(shù)據(jù)的初始化腳本
liquibase有兩種方式生成初始化腳本
方法一:在官網(wǎng)下載liquibase壓縮包,使用原生的命令行指令來(lái)生成
下載liquibase壓縮包,解壓,將mysql連接jar包復(fù)制一份到此目錄下
進(jìn)入解壓目錄執(zhí)行如下執(zhí)行
根據(jù)數(shù)據(jù)庫(kù)生成表結(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ù)庫(kù)生成初始數(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ù)庫(kù)驅(qū)動(dòng)取決于數(shù)據(jù)庫(kù)
–driver=com.mysql.cj.jdbc.Driver
- mysql連接
–classpath=mysql-connector-java-8.0.17.jar
- 自定義生成的初始化腳本文件名
–changeLogFile=./init-data.xml
- 數(shù)據(jù)庫(kù)連接地址
–url=“jdbc:mysql://192.168.0.162:3306/hello_world”
- 數(shù)據(jù)庫(kù)用戶名密碼
-username=root
–password=123456
- 生成初始化表數(shù)據(jù)需要加上這個(gè)配置,生成表結(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ù)庫(kù)連接--> <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í)候是否顯示詳細(xì)的參數(shù)信息--> <verbose>true</verbose> <!--連接非本地?cái)?shù)據(jù)庫(kù)是否彈出提示框--> <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase> <!--生成changelog文件內(nèi)容--> <diffTypes>tables, views, columns, indexs,foreignkeys, primarykeys, uniqueconstraints, data</diffTypes> </configuration> </plugin>
如果只是生成數(shù)據(jù)庫(kù)表腳本,則將上面的diffTypes注釋起來(lái)或者去掉里面的data
如果只是生成數(shù)據(jù)腳本,則只留下data
如果要把數(shù)據(jù)表腳本和數(shù)據(jù)腳本生成到一個(gè)文件則保留上面的difftypes所有內(nèi)容
安裝好maven插件后maven插件中可以看如下圖的指令,點(diǎn)擊即可生成腳本文件
生成腳本如下
<?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="學(xué)生自增ID" type="INT"> <constraints primaryKey="true"/> </column> <column name="classId" remarks="班級(jí)ID" type="INT"/> <column name="userCode" remarks="用戶唯一碼" type="VARCHAR(20)"/> <column name="studentName" remarks="學(xué)生姓名" type="VARCHAR(20)"/> <column name="studentImageUrl" remarks="學(xué)生頭像地址" type="VARCHAR(200)"/> <column name="studentCode" remarks="學(xué)生學(xué)號(hào)" type="VARCHAR(50)"/> <column name="IDCard" remarks="身份證號(hào)" type="VARCHAR(50)"/> <column name="status" remarks="學(xué)生狀態(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)建時(shí)間" 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="班級(jí)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)建時(shí)間" 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
到此這篇關(guān)于SpringBoot整合liquibase的文章就介紹到這了,更多相關(guān)SpringBoot整合liquibase內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用Flyway進(jìn)行數(shù)據(jù)庫(kù)遷移的實(shí)現(xiàn)示例
- SpringBoot集成Flyway進(jìn)行數(shù)據(jù)庫(kù)版本遷移管理的步驟
- SpringBoot整合Flyway的方法(數(shù)據(jù)庫(kù)版本遷移工具)
- 淺談SpringBoot之開啟數(shù)據(jù)庫(kù)遷移的FlyWay使用
- SpringBoot整合Liquibase的示例代碼
- SpringBoot整合liquibase的實(shí)現(xiàn)方法
- Spring Boot 如何使用Liquibase 進(jìn)行數(shù)據(jù)庫(kù)遷移(操作方法)
相關(guān)文章
Java解析http協(xié)議字符串的方法實(shí)現(xiàn)
本文主要介紹了Java解析http協(xié)議字符串的方法實(shí)現(xiàn),我們探討了如何使用Java解析HTTP協(xié)議字符串,并將其封裝成了一個(gè)HttpRequest類,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09Java生成二維碼的兩種實(shí)現(xiàn)方式(基于Spring?Boot)
這篇文章主要給大家介紹了關(guān)于Java生成二維碼的兩種實(shí)現(xiàn)方式,文中的代碼基于Spring?Boot,本文基于JAVA環(huán)境,以SpringBoot框架為基礎(chǔ)開發(fā),文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07java 模仿拼多多紅包遞減算法的實(shí)現(xiàn)
這篇文章主要介紹了java 模仿拼多多紅包遞減算法的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02使用Java實(shí)現(xiàn)類似Comet風(fēng)格的web app
這篇文章主要介紹了使用Java實(shí)現(xiàn)類似Comet風(fēng)格的web app的方法,包括客戶端的響應(yīng)和XML解析等功能,需要的朋友可以參考下2015-11-11Java的MyBatis框架中關(guān)鍵的XML字段映射的配置參數(shù)詳解
將XML文件的schema字段映射到數(shù)據(jù)庫(kù)的schema是我們操作數(shù)據(jù)庫(kù)的常用手段,這里我們就來(lái)整理一些Java的MyBatis框架中關(guān)鍵的XML字段映射的配置參數(shù)詳解,需要的朋友可以參考下2016-06-06Spring AOP 對(duì)象內(nèi)部方法間的嵌套調(diào)用方式
這篇文章主要介紹了Spring AOP 對(duì)象內(nèi)部方法間的嵌套調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08java連接MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了java連接MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-06-06