当前位置: 代码网 > it编程>编程语言>Java > Liquibase结合SpringBoot使用实现数据库管理功能

Liquibase结合SpringBoot使用实现数据库管理功能

2024年12月21日 Java 我要评论
liquibase概述liquibase 是一个开源的数据库变更管理工具,用于跟踪、版本化、和管理数据库结构(如表、字段、索引等)的变更。它的目的是使数据库变更的过程更加透明、可控制、自动化,避免开发

liquibase概述

liquibase 是一个开源的数据库变更管理工具,用于跟踪、版本化、和管理数据库结构(如表、字段、索引等)的变更。它的目的是使数据库变更的过程更加透明、可控制、自动化,避免开发团队在多个环境中手动执行相同的数据库变更脚本。

liquibase 支持多种数据库(mysql、postgresql、oracle、sql server、h2 等),并能够通过 xml、yaml、json 或 sql 文件来定义数据库变更。

主要特点

  • 版本控制:将数据库变更与代码同步管理,避免手动更改数据库结构。
  • 自动化迁移:在不同环境(开发、测试、生产等)中自动应用数据库变更。
  • 可回滚性:liquibase 提供了回滚机制,可以回到之前的数据库版本。
  • 支持多种格式:支持 xml、yaml、json 等格式来描述变更。
  • 集成方便:liquibase 可以集成到 ci/cd 流程中,或者与 spring boot 等框架配合使用,轻松管理数据库版本。

工作机制

liquibase 使用一个名为 changelog 的文件来描述数据库的所有变更。这个文件记录了所有执行过的数据库变更集合(changesets)。每个 changeset 都有一个唯一的 id 和作者标识,用来追踪该变更。

liquibase 会通过 changelog 文件自动管理数据库的版本和变更。它会在每次应用变更时,通过一个 databasechangelog 表记录哪些变更已经应用过了

与springboot结合使用

由于我这边项目上使用的是xml方式,就用xml方式进行示例,其余方式的方法,大家感兴趣的可以自行前往官网查看文档。

传送门:liquibase documentation

引入依赖

<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> <!-- 或者根据需要使用适合的版本 -->
    </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

创建 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: 应用所有未执行的数据库变更。
liquibase rollback: 回滚数据库到指定的 changeset 或版本。
liquibase status: 查看当前数据库的变更状态。
liquibase generatechangelog: 根据现有数据库生成初始的 changelog 文件。

总结

liquibase 是一个强大的数据库管理工具,它帮助你通过自动化管理数据库的变更、版本控制、和回滚,简化了开发中的数据库迁移工作。通过在 spring boot 中集成 liquibase,可以更高效地管理数据库结构和版本,确保开发团队的协作更加流畅。在项目中,liquibase 可以和 git 等版本控制工具配合使用,确保数据库结构变更的透明性和可追溯性。

到此这篇关于liquibase结合springboot使用实现数据库管理的文章就介绍到这了,更多相关springboot数据库管理内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com