Liquibase結(jié)合SpringBoot使用實現(xiàn)數(shù)據(jù)庫管理功能
Liquibase概述
Liquibase 是一個開源的數(shù)據(jù)庫變更管理工具,用于跟蹤、版本化、和管理數(shù)據(jù)庫結(jié)構(gòu)(如表、字段、索引等)的變更。它的目的是使數(shù)據(jù)庫變更的過程更加透明、可控制、自動化,避免開發(fā)團隊在多個環(huán)境中手動執(zhí)行相同的數(shù)據(jù)庫變更腳本。
Liquibase 支持多種數(shù)據(jù)庫(MySQL、PostgreSQL、Oracle、SQL Server、H2 等),并能夠通過 XML、YAML、JSON 或 SQL 文件來定義數(shù)據(jù)庫變更。
主要特點
- 版本控制:將數(shù)據(jù)庫變更與代碼同步管理,避免手動更改數(shù)據(jù)庫結(jié)構(gòu)。
- 自動化遷移:在不同環(huán)境(開發(fā)、測試、生產(chǎn)等)中自動應用數(shù)據(jù)庫變更。
- 可回滾性:Liquibase 提供了回滾機制,可以回到之前的數(shù)據(jù)庫版本。
- 支持多種格式:支持 XML、YAML、JSON 等格式來描述變更。
- 集成方便:Liquibase 可以集成到 CI/CD 流程中,或者與 Spring Boot 等框架配合使用,輕松管理數(shù)據(jù)庫版本。
工作機制
Liquibase 使用一個名為 changelog 的文件來描述數(shù)據(jù)庫的所有變更。這個文件記錄了所有執(zhí)行過的數(shù)據(jù)庫變更集合(changeSets)。每個 changeSet 都有一個唯一的 ID 和作者標識,用來追蹤該變更。
Liquibase 會通過 changelog 文件自動管理數(shù)據(jù)庫的版本和變更。它會在每次應用變更時,通過一個 DATABASECHANGELOG 表記錄哪些變更已經(jīng)應用過了
與SpringBoot結(jié)合使用
由于我這邊項目上使用的是xml方式,就用xml方式進行示例,其余方式的方法,大家感興趣的可以自行前往官網(wǎng)查看文檔。
引入依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.21.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version> <!-- 或者根據(jù)需要使用適合的版本 -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
</project>配置文件
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
maximum-pool-size: 10
liquibase:
change-log: classpath:liquibase/platform/changeSet.xml
enabled: true創(chuàng)建 Liquibase 變更集文件
<?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.1.xsd">
<include file="liquibase/platform/change/change01.xml"/>
</databaseChangeLog>具體的變更集文件
<?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.1.xsd">
<changeSet id="1" author="bob">
<createTable tableName="department">
<column name="id" type="int">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="varchar(50)">
<constraints nullable="false"/>
</column>
<column name="active" type="boolean" defaultValueBoolean="true"/>
</createTable>
</changeSet>
</databaseChangeLog>常用命令
liquibase update: 應用所有未執(zhí)行的數(shù)據(jù)庫變更。
liquibase rollback: 回滾數(shù)據(jù)庫到指定的 changeSet 或版本。
liquibase status: 查看當前數(shù)據(jù)庫的變更狀態(tài)。
liquibase generateChangeLog: 根據(jù)現(xiàn)有數(shù)據(jù)庫生成初始的 changeLog 文件。
總結(jié)
Liquibase 是一個強大的數(shù)據(jù)庫管理工具,它幫助你通過自動化管理數(shù)據(jù)庫的變更、版本控制、和回滾,簡化了開發(fā)中的數(shù)據(jù)庫遷移工作。通過在 Spring Boot 中集成 Liquibase,可以更高效地管理數(shù)據(jù)庫結(jié)構(gòu)和版本,確保開發(fā)團隊的協(xié)作更加流暢。在項目中,Liquibase 可以和 Git 等版本控制工具配合使用,確保數(shù)據(jù)庫結(jié)構(gòu)變更的透明性和可追溯性。
到此這篇關(guān)于Liquibase結(jié)合SpringBoot使用實現(xiàn)數(shù)據(jù)庫管理的文章就介紹到這了,更多相關(guān)SpringBoot數(shù)據(jù)庫管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA Ultimate2020.2版本配置Tomcat詳細教程
這篇文章主要介紹了IDEA Ultimate2020.2版本配置Tomcat教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Java自定義類數(shù)組報null的相關(guān)問題及解決
這篇文章主要介紹了Java自定義類數(shù)組報null的相關(guān)問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
使用Mybatis時SqlSessionFactory對象總是報空指針
本文主要介紹了使用Mybatis時SqlSessionFactory對象總是報空指針,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-09-09
maven如何動態(tài)統(tǒng)一修改版本號的方法步驟
這篇文章主要介紹了maven如何動態(tài)統(tǒng)一修改版本號的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12

