【Liquibase】与springboot整合

318 阅读1分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第7天,点击查看活动详情

准备

前提是已经有一个springboot的工程,然后进行liquibasespringboot整合

maven依赖

可以选择先引入maven依赖。

<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
    <version>4.4.3</version>
</dependency>

spring配置

添加完依赖后要添加配置,设置开启liquibase,并设置加载change-log的主路径。

这里没有配置数据库信息,那liquibase怎么关联到数据库执行更改呢? 答案是liquibase会查找spring配置的datasource,以这个作为默认的数据库连接,执行对数据库更改的操作。

spring:
    application:
        name: demo
    liquibase:
        enabled: true
        change-log: classpath:liquibase/liquibase.xml

changelog文件

增加changelog主文件,主文件就是上面配置路径的文件,在里面使用<include>标签来引用做更改的配置信息。

<?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.5.xsd">

    
    <include file="liquibase/changelog/User.xml"
        relativeToChangelogFile="false" />
</databaseChangeLog>

增加changelog具体表操作文件,这个是用户示例表,对表创建的操作类型。

<?xml version="1.0" encoding="UTF-8"?>
<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 http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd
                        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">



    <changeSet id="202208171532-01" author="wyr">
        <createTable tableName="user"
            remarks="用户表">
            <column name="id" type="varchar(64)" >
                <constraints primaryKey="true" nullable="false" />
            </column>
            <column name="name" type="varchar(256)" remarks="姓名">
                <constraints nullable="false" />
            </column>
            <column name="sex" remarks="性别" type="tinyint(1)">
                <constraints nullable="false" />
            </column>
            <column name="birthday" type="varchar(128)" remarks="生日">
                <constraints nullable="false" />
            </column>
        </createTable>
    </changeSet>
</databaseChangeLog>

启动

配置结束,直接在项目启动运行,就会在数据库中创建这张表了,因为是第一次启动,创建user表之前会创建DATABASECHANGELOGDATABASECHANGELOGLOCK两张表,并把这次执行的信息写入到DATABASECHANGELOG中。

如果我的user表如果想增加一个字段,该怎么做?

继续在User.xml这个文件里面,增加一个<changeSet>,如下:

 <changeSet id="202208171846-01" author="wyr">
        <addColumn tableName="user">
            <column name="account" type="varchar(64)" afterColumn="birthday" remarks="账号信息">
            </column>
        </addColumn>
 </changeSet>

再次重启springboot工程即可,其他的对表做修改也可同样使用<changeSet>,只是其中的标签不同。 对表的常用标签有如下这些:

  • createTable
  • addColumn
  • createIndex
  • createView
  • renameColumn
  • setTableRemarks
  • modifyDataType
  • sql

小结

以上就是liquibasespringboot中的使用过程,常用的操作包括了创建表、新增字段、新增索引,还有更改数据。