SpringBoot整合liquibase

一. SpringBoot集成liquibase

项目集成liquibase作用

  1. 对数据库表字段进行版本控制

  2. 项目初始化部署时初始化数据库表和数据

①.导入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文件用于中指定数据库初始化脚本的位置

<?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>

④.将数据库表初始脚本init_table.xml和数据初始脚本init_data.xml放到项目目录下
在这里插入图片描述
脚本可以通过手写的方式或者通过liquibase自动生成;

启动项目如果第一次运行则会在数据库中创建表和数据
后面如果脚本中有新增表或者字段启动项目的时候也会自动创建生成

二. liquibase生成数据库表和数据的初始化脚本

liquibase有两种方式生成初始化脚本

方法一:在官网下载liquibase压缩包,使用原生的命令行指令来生成

下载liquibase压缩包,解压,将mysql连接jar包复制一份到此目录下
在这里插入图片描述

进入解压目录执行如下执行

根据数据库生成表结构文件

./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

根据数据库生成初始数据文件

./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

  • 数据库驱动取决于数据库
    –driver=com.mysql.cj.jdbc.Driver
  • mysql连接
    –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

方法二:使用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>
		<!--数据库连接-->
		<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>
		<!--执行的时候是否显示详细的参数信息-->
		<verbose>true</verbose>
		<!--连接非本地数据库是否弹出提示框-->
		<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
		<!--生成changelog文件内容-->
		<diffTypes>tables, views, columns, indexs,foreignkeys, primarykeys, uniqueconstraints, data</diffTypes>
	</configuration>
</plugin>

如果只是生成数据库表脚本,则将上面的diffTypes注释起来或者去掉里面的data
如果只是生成数据脚本,则只留下data
如果要把数据表脚本和数据脚本生成到一个文件则保留上面的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="学生状态 1:在读 0:毕业 -1:转校" type="VARCHAR(5)"/>
            <column name="flag" remarks="是否删除 1:正常显示,-1:表示删除" type="VARCHAR(10)"/>
            <column name="createDate" remarks="创建时间" 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="创建时间" 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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot可以很方便地与Liquibase集成,以实现数据库版本控制和迁移。下面是整合步骤: 1. 添加Liquibase依赖 在pom.xml文件中添加Liquibase依赖: ``` <dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> <version>3.8.9</version> </dependency> ``` 2. 配置Liquibase 在application.properties文件中添加Liquibase配置: ``` #Liquibase配置 spring.liquibase.change-log=classpath:/db/changelog/db.changelog-master.xml spring.liquibase.enabled=true spring.liquibase.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC spring.liquibase.user=root spring.liquibase.password=root ``` 其中,change-log属性指定Liquibase的changelog文件路径,url、user和password属性指定数据库连接信息。 3. 创建changelog文件 在resources/db/changelog目录下创建db.changelog-master.xml文件,用于定义数据库版本控制和迁移的变更集合。 例如: ``` <?xml version="1." 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"> <changeSet id="1" author="liquibase"> <createTable tableName="person"> <column name="id" type="bigint" autoIncrement="true"> <constraints primaryKey="true" nullable="false"/> </column> <column name="name" type="varchar(255)"> <constraints nullable="false"/> </column> <column name="age" type="int"/> </createTable> </changeSet> </databaseChangeLog> ``` 4. 运行应用程序 运行Spring Boot应用程序,Liquibase将自动检测数据库版本并执行相应的变更集。 以上就是Spring Boot整合Liquibase的步骤。 ### 回答2: Spring Boot是一款非常流行的Java框架,它为开发人员提供了一种简单、快速并且有效的方法来开发和部署Web应用程序。而Liquibase是一款专门用于管理数据库变更的工具,它提供了一种简单、可靠的方式来进行数据库的合并、迁移和关系升级等操作。本篇文章将会介绍如何使用Spring Boot和Liquibase进行数据库管理和变更。 1. 配置Liquibase 在pom.xml文件中,引入Liquibase的依赖。 ```xml <dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> <version>[版本号]</version> </dependency> ``` 接下来,在application.properties文件中配置Liquibase ```properties spring.liquibase.enabled=true spring.liquibase.change-log=classpath:/db/changelog/db.changelog-master.xml spring.datasource.url=jdbc:mysql://localhost:3306/[数据库名]?useUnicode=true&characterEncoding=UTF8&allowMultiQueries=true&autoReconnect=true&failOverReadOnly=false&maxReconnects=5 spring.datasource.username=[用户名] spring.datasource.password=[密码] spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 以上配置可根据实际情况自定义修改。其中,spring.liquibase.enabled=true表示启用Liquibase;spring.liquibase.change-log指定Liquibase的master changelog文件;spring.datasource.*为数据源配置。 2. 编写Liquibase脚本 下面编写一个简单的Liquibase脚本,用于创建一个users表。 ```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.3.xsd"> <changeSet id="create-users-table" author="张三"> <createTable tableName="users"> <column name="id" type="bigserial" autoIncrement="true"> <constraints primaryKey="true"/> </column> <column name="username" type="varchar(50)"/> <column name="password" type="varchar(50)"/> <column name="email" type="varchar(50)"/> </createTable> </changeSet> </databaseChangeLog> ``` 以上脚本使用Liquibase的XML格式编写,用于创建一个名为users的表和其对应的字段。其中,id字段为自增主键,其他字段均为varchar类型。 3. 启动应用 现在,只需运行应用,并等待Liquibase执行创建表的脚本即可。Liquibase会在启动时检查数据库中是否存在对应的表,如果表不存在则会进行创建。如果已存在对应的表,则不会进行任何操作。通过运行时日志,可以看到Liquibase在检查和执行脚本的相关信息。 4. 变更数据库 当需要对数据库进行修改时,只需修改Liquibase脚本即可。Liquibase会自动检测修改并进行相应的变更,无需手动执行SQL语句。当需要更改数据表时,只需编写修改表结构的Liquibase脚本,例如新增一列。 ```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.3.xsd"> <changeSet id="add-email-to-users-table" author="张三"> <addColumn tableName="users"> <column name="email" type="varchar(50)"/> </addColumn> </changeSet> </databaseChangeLog> ``` 运行应用,Liquibase会自动检测到该变更操作,并进行相应的变更。 总结 通过Spring Boot整合Liquibase,我们可以轻松地将数据库管理和版本控制集成到应用中,从而提高开发效率、减少错误和风险。Liquibase提供了丰富的脚本语法和插件支持,为数据库管理带来了更多可能性。 ### 回答3: Spring Boot 是一种开箱即用的微服务框架,可以让开发者快速搭建并部署应用程序,而 Liquibase 是一个开源的数据库版本控制和迁移工具,可以帮助开发者管理数据库的变化与版本迭代。在实际的项目开发中,为了方便在集成开发环境(IDE)中更好地管理数据库的版本变更和迁移,通常需要 Spring Boot 整合 Liquibasespringboot整合liquibase)。 Spring Boot 整合 Liquibase 可以让开发者利用 Liquibase 强大的功能实现持续集成和部署,达到自动化管理数据库变更的效果,同时让开发者更加高效地进行开发。下面我们来了解一下 Spring Boot 整合 Liquibase 的具体实现步骤: ## 1. 引入依赖 在 pom.xml 文件中加入以下依赖: ```xml <dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> <version>4.5.0</version> </dependency> ``` ## 2. 编写 Liquibase 配置文件 在 resources 目录下新建 liquibase 目录,并在该目录下新建 liquibase.properties 文件。该文件包含了一些 Liquibase 的配置信息,例如: ```properties changeLogFile=classpath:db/changelog/changelog-master.xml url=jdbc:mysql://localhost:3306/springdemo username=root password=root driver=com.mysql.cj.jdbc.Driver ``` ## 3. 编写 Liquibase changelog 在 resources 目录下新建 db/changelog 目录,并在该目录下新建一个 changelog-master.xml 文件,该文件描述了数据库的变更历史以及变更脚本: ```xml <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-4.0.xsd"> <changeSet id="1" author="dbo"> <createTable tableName="person"> <column name="id" type="bigint" autoIncrement="true"> <constraints primaryKey="true" nullable="false"/> </column> <column name="name" type="varchar(255)"> <constraints nullable="false"/> </column> <column name="age" type="int"/> </createTable> </changeSet> </databaseChangeLog> ``` ## 4. 启动 Spring Boot 应用 当我们启动 Spring Boot 应用时,Liquibase 将会自动执行 changelog 中定义的变更脚本。例如,我们在命令行执行 mvn spring-boot:run 启动 Spring Boot,则会执行以下操作: 1. 根据 liquibase.properties 中的配置信息,创建与数据库的连接; 2. 根据 changelog-master.xml 中的定义,检查数据库的变更历史; 3. 根据 changelog-master.xml 中的定义,执行数据库的变更脚本,将其应用到数据库中; 4. 关闭与数据库的连接。 总之,Spring Boot 整合 Liquibase 提供了一个方便易用的数据库管理工具,可以帮助开发者更好地管理数据库变更的历史和版本,提高开发效率并保证数据完整性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值