SpringBoot整合Liquibase的示例代碼
SpringBoot整合Liquibase雖然不難但坑還是有一點(diǎn)的,主要集中在配置路徑相關(guān)的地方,在此記錄一下整合的步驟,方便以后自己再做整合時(shí)少走彎路,當(dāng)然也希望能幫到大家~
整合有兩種情況
- 在啟動(dòng)項(xiàng)目時(shí)自動(dòng)執(zhí)行腳本,若新添加了Liquibase腳本需要重啟項(xiàng)目才能執(zhí)行腳本
- 在不啟動(dòng)項(xiàng)目時(shí)也能通過插件或指令手動(dòng)讓它執(zhí)行腳本
整合要么只整合1,要么1、2一起整合
只整合2不整合1的話,項(xiàng)目啟動(dòng)時(shí)會(huì)生成liquibase相關(guān)的bean時(shí)報(bào)錯(cuò)
整合1
引入Maven依賴
這里導(dǎo)入了Liquibase的包和連接MySQL數(shù)據(jù)庫的包
<!-- https://mvnrepository.com/artifact/org.liquibase/liquibase-core --> <dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> <version>3.6.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
在SpringBoot配置文件中配置
配置中l(wèi)iquibase相關(guān)的只需要配置根changelog的位置,數(shù)據(jù)庫相關(guān)配置liquibase會(huì)自己去拿datasource下面的,注意url需要加上時(shí)區(qū)serverTimezone
spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/test_database?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai liquibase: change-log: classpath:/db/liquibaseChangeLogFile.xml
配置liquibaseChangeLogFile.xml
同樣需要注意xml文件的路徑,按照上述配置的話該文件在src/main/resources/db/liquibaseChangeLogFile.xml
<?xml version="1.0" encoding="utf-8"?> <databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd"> <!--relativeToChangelogFile="true"表示根據(jù)該changeLog的相對(duì)路徑尋找changeLog文件--> <includeAll path="changelog/" relativeToChangelogFile="true"/> </databaseChangeLog>
配置changeLog.xml
- 按照上述的配置的話,changeLog文件應(yīng)該放在
src/main/resources/db/changelog/
- 下面簡(jiǎn)單寫一個(gè)changelog來測(cè)試
<?xml version="1.0" encoding="utf-8"?> <databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd"> <changeSet id="20201212-01-2344" author="RR"> <createTable tableName="test_table"> <column name="id" type="VARCHAR(32)" remarks="表ID"> <constraints primaryKey="true"/> </column> <column name="user_name" type="VARCHAR(16)" remarks="用戶名"> <constraints nullable="false"/> <column name="age" type="TINYINT"> <column name="create_date" type="TIMESTAMP" remarks="創(chuàng)建日期" defaultValueComputed="CURRENT_TIMESTAMP"/> </createTable> </changeSet> </databaseChangeLog>
整合情況:
按照上面的流程配置完,在啟動(dòng)項(xiàng)目時(shí),它就會(huì)執(zhí)行未執(zhí)行過的Lisquibase腳本了~
整合2
有時(shí)候想不啟動(dòng)或不重啟項(xiàng)目時(shí)執(zhí)行Liquibase腳本,此時(shí)就應(yīng)該整合2
引入Maven插件
引入的插件版本和上面的依賴包的版本號(hào)保持一致,不過我也沒有考究過不一致會(huì)怎樣,有興趣的小伙伴可以試試 ,不過不一致看得不會(huì)難受嗎在這里的configuration
標(biāo)簽中,配置好要讀取的liquibase相關(guān)信息的配置文件,并且注意好路徑
<build> <plugins> <plugin> <groupId>org.liquibase</groupId> <artifactId>liquibase-maven-plugin</artifactId> <version>3.6.2</version> <configuration> <propertyFile>src/main/resources/liquibase.properties</propertyFile> <!--配置參數(shù),以禁用彈出的對(duì)話框,該對(duì)話框?qū)⒋_認(rèn)非本地?cái)?shù)據(jù)庫上的遷移--> <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase> </configuration> </plugin> </plugins> </build>
配置Liquibase.properties
注意配置文件的路徑應(yīng)跟在配置Maven插件時(shí)聲明的一致,即src/main/resources/liquibase.properties
配置好如下的五個(gè)屬性,注意url需要加上時(shí)區(qū)serverTimezone
注意changeLogFile項(xiàng)的路徑,它是一個(gè)相對(duì)路徑,該配置會(huì)在下面展示
#配置都整合在yml配置文件里了,若想不啟動(dòng)時(shí)執(zhí)行l(wèi)iquibase腳本該配置不能省?。。?! driver-class-name=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test_database?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username=root password=root changeLogFile=db/liquibaseChangeLogFile.xml
配置liquibaseChangeLogFile.xml
在整合1的時(shí)候就已經(jīng)配置好了~
配置changeLog.xml
按照上述的配置的話,changeLog文件應(yīng)該放在src/main/resources/db/changelog/
下面再寫一個(gè)liquibase腳本
<?xml version="1.0" encoding="utf-8"?> <databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd"> <changeSet id="20201213-01-1525" author="RR"> <insert tableName="test_table"> <column name="id" value="34f0186001df406fbb373845c579e6a6"/> <column name="user_name" value="小明"/> <column name="age" value="18"/> </insert> </changeSet> </databaseChangeLog>
測(cè)試整合情況
- 先mvn clean和install一下!!! 否則不能找到我們寫的xml文件
- 在maven插件中找到liquibase:update,雙擊即可~
補(bǔ)充:下面給大家分享一段示例代碼講解下spring boot 整合 liquibase
1.添加依賴
<dependency> ? ?<groupId>org.liquibase</groupId> ? ?<artifactId>liquibase-core</artifactId> ? ?<version>3.8.3</version> </dependency> <dependency> ? ?<groupId>org.liquibase</groupId> ? ?<artifactId>liquibase-maven-plugin</artifactId> ? ?<version>3.8.3</version> </dependency> <plugin> ? ?<groupId>org.liquibase</groupId> ? ?<artifactId>liquibase-maven-plugin</artifactId> ? ?<version>3.8.3</version> ? ?<configuration> ? ? ? <propertyFile>src/main/resources/db/liquibase.properties</propertyFile> ? ?</configuration> </plugin>
2.創(chuàng)建liquibase.properties 內(nèi)容如下
url=jdbc:postgresql://127.0.0.1:5432/ems_factorymodeldb username=name password=password driver=org.postgresql.Driver outputChangeLogFile=src/main/resources/liquibase-outputChangeLog.xml
3.創(chuàng)建 db.changelog-master.xml
<?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.8.xsd"> ? ? <!-- ?版本更新迭代 ?--> ? ? <include file="classpath:db/changelogs/liquibase-changeLog.xml" relativeToChangelogFile="false"></include> ? ? <!-- ?現(xiàn)有數(shù)據(jù) ?--> ? ? <includeAll path="./currentdatas" relativeToChangelogFile="true"/> ? ? <!-- ?數(shù)據(jù)初始化 腳本 ?--> ? ? <!-- <includeAll path="./defaultdatas" relativeToChangelogFile="true"/>--> </databaseChangeLog>
4.增加配置
# liquibase spring.liquibase.enabled=true spring.liquibase.change-log=classpath:db/db.changelog-master.xml
到此這篇關(guān)于SpringBoot整合Liquibase的文章就介紹到這了,更多相關(guān)SpringBoot整合Liquibase內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用Flyway進(jìn)行數(shù)據(jù)庫遷移的實(shí)現(xiàn)示例
- SpringBoot集成Flyway進(jìn)行數(shù)據(jù)庫版本遷移管理的步驟
- SpringBoot整合Flyway的方法(數(shù)據(jù)庫版本遷移工具)
- 淺談SpringBoot之開啟數(shù)據(jù)庫遷移的FlyWay使用
- SpringBoot整合liquibase及l(fā)iquibase生成初始化腳本的方式
- SpringBoot整合liquibase的實(shí)現(xiàn)方法
- Spring Boot 如何使用Liquibase 進(jìn)行數(shù)據(jù)庫遷移(操作方法)
相關(guān)文章
java實(shí)現(xiàn)支付寶支付接口的調(diào)用
本文主要介紹了java實(shí)現(xiàn)支付寶支付接口的調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07詳解java爬蟲jsoup解析多空格class數(shù)據(jù)
在本篇內(nèi)容中小編給大家分享了java爬蟲jsoup怎么解析多空格class數(shù)據(jù)的方法和技巧,需要的朋友們跟著學(xué)習(xí)下。2018-12-12spring/springboot整合dubbo詳細(xì)教程
今天教大家如何使用spring/springboot整合dubbo,文中有非常詳細(xì)的圖文介紹及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴有很好地幫助,需要的朋友可以參考下2021-05-05java創(chuàng)建excel示例(jxl使用方法)
Java Excel是一開放源碼項(xiàng)目,通過它Java開發(fā)人員可以讀取Excel文件的內(nèi)容、創(chuàng)建新的Excel文件、更新 已經(jīng)存在的Excel文件。下面是使用方法,包括去掉網(wǎng)格線、字體設(shè)置、單元格設(shè)置、對(duì)齊方式等設(shè)置2014-03-03SpringIOC DI循環(huán)依賴實(shí)例詳解
這篇文章主要介紹了SpringIOC——DI循環(huán)依賴,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03springboot配置druid多數(shù)據(jù)源的示例代碼
這篇文章主要介紹了springboot配置druid多數(shù)據(jù)源的示例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09java實(shí)現(xiàn)ftp上傳 如何創(chuàng)建文件夾
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)ftp上傳的相關(guān)資料,教大家如何創(chuàng)建文件夾?具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04java通過MySQL驅(qū)動(dòng)攔截器實(shí)現(xiàn)執(zhí)行sql耗時(shí)計(jì)算
本文主要介紹了java通過MySQL驅(qū)動(dòng)攔截器實(shí)現(xiàn)執(zhí)行sql耗時(shí)計(jì)算,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03