SpringBoot 整合 liquibase

LiquiBase是一个用于数据库重构和迁移的开源工具,经过日志文件的形式记录数据库的变动,而后执行日志文件中的修改,将数据库更新或回滚到一致的状态。它的目标是提供一种数据库类型无关的解决方案,经过执行schema类型的文件来达到迁移。其有点主要有如下:html

  • 支持几乎全部主流的数据库,如MySQL, PostgreSQL, Oracle, Sql Server, DB2等;
  • 支持多开发者的协做维护;
  • 日志文件支持多种格式,如XML, YAML, JSON, SQL等;
  • 支持多种运行方式,如命令行、Spring集成、Maven插件、Gradle插件等。

liquibase 官方文档地址:www.liquibase.org/documentati…java

1、引入依赖

先在 pom 文件里引入依赖spring

<dependency>
  <groupId>org.liquibase</groupId>
  <artifactId>liquibase-core</artifactId>
</dependency>
复制代码

2、指定配置文件位置

在代码中新建一个 LiquibaseConfig 类,用于配置 Liquibase,指定配置文件的位置。sql

import javax.sql.DataSource;
import liquibase.integration.spring.SpringLiquibase;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LiquibaseConfig {

  @Bean
  public SpringLiquibase liquibase(DataSource dataSource) {
    SpringLiquibase liquibase = new SpringLiquibase();
    liquibase.setDataSource(dataSource);
    //指定changelog的位置,这里使用的一个master文件引用其余文件的方式
    liquibase.setChangeLog("classpath:liquibase/master.xml");
    liquibase.setContexts("development,test,production");
    liquibase.setShouldRun(true);
    return liquibase;
  }

}
复制代码

3、编写配置文件

目录结构:数据库

src/main/resources 下新建一个文件夹:liquibase,用来存放跟 liquibase 相关的文件。bash

master.xml

而后在 liquibase 文件夹下新建 master.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-3.1.xsd">

    <includeAll path="liquibase/changelogs/" relativeToChangelogFile="false"/>

</databaseChangeLog>
复制代码

includeAll 标签能够把一个文件夹下的全部 changelog 都加载进来。若是单个加载能够用 includeui

includeAll 标签里有两个属性:pathrelativeToChangelogFileurl

Attribute Description
file Name of the file to import required
relativeToChangelogFile Is the file path relative to the root changelog file rather than to the classpath. Defaults to "false" since 1.9

path (在 include 标签里是 file):指定要加载的文件或文件夹位置spa

relativeToChangelogFile :文件位置的路径是否相对于 root changelog 是相对路径,默认 false,即相对于 classpath 是相对路径。

changelog

另在 liquibase 文件夹下新建 changelogs 文件夹用来存放 changelog。

这里新建一个 changelog-1.0.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-3.1.xsd">

    <changeSet id="20190713-01" author="solo">
        <createTable tableName="project_info">
            <column name="project_id" type="varchar(64)" encoding="utf8" remarks="项目id">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="project_name" type="varchar(255)" encoding="utf8" remarks="项目名字"/>
            <column name="project_difficulty" type="float" encoding="utf8" remarks="项目难度"/>
            <column name="category_id" type="varchar(64)" encoding="utf8" remarks="项目类型类目编号"/>
            <column name="project_status" type="int(11)" encoding="utf8" remarks="项目状态, 0招募中,1 进行中,2已完成,3失败,4延期,5删除"/>
            <column name="project_desc" type="varchar(512)" encoding="utf8" remarks="项目简介"/>
            <column name="project_creater_id" type="varchar(64)" encoding="utf8" remarks="项目建立者id"/>
            <column name="team_id" type="varchar(64)" encoding="utf8" remarks="项目所属团队id"/>
            <column name="create_time" type="bigint(64)" encoding="utf8" remarks="建立时间"/>
            <column name="update_time" type="bigint(64)" encoding="utf8" remarks="更新时间"/>
        </createTable>
    </changeSet>
    
    <changeSet id="20190713-02" author="solo">
        <createTable tableName="project_category" remarks="项目类型表">
            <column name="id" type="varchar(64)" remarks="项目类型id">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(255)" remarks="类目类型名称"/>
            <column name="status" type="int(11)" remarks="状态。1正常,2删除"/>
            <column name="remark" type="varchar(255)" remarks="备注"/>
        </createTable>
    </changeSet>

    <changeSet id="20190713-03" author="solo">
        <createTable tableName="project_like_user" remarks="项目点赞表">
            <column name="id" type="varchar(64)" remarks="主键id">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="project_id" type="varchar(64)" remarks="项目id"/>
            <column name="user_id" type="varchar(64)" remarks="点赞的用户id"/>
            <column name="status" type="int(11)" remarks="点赞状态,0 取消点赞,1点赞"/>
            <column name="type" type="int(11)" remarks="类型 1点赞"/>
            <column name="create_time" type="bigint(64)" remarks="建立时间"/>
            <column name="update_time" type="bigint(64)" remarks="更新时间"/>
        </createTable>
    </changeSet>

    <changeSet id="20190713-04" author="solo">
        <createTable tableName="project_picture" remarks="项目图片表">
            <column name="id" type="varchar(64)" remarks="图片id">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="project_id" type="varchar(64)" remarks="项目id"/>
            <column name="picture_url" type="varchar(64)" remarks="图片地址"/>
            <column name="picture_url_32" type="varchar(64)" remarks="图片地址32位"/>
            <column name="picture_url_64" type="varchar(64)" remarks="图片地址64位"/>
        </createTable>
    </changeSet>

</databaseChangeLog>
复制代码

若是你的项目一开始就用了 liquibase,那能够像上面这样写,把建表语句都写在 changelog 里。

若是一开始没用,后期想引入 liquibase,能够把之前的数据库导出成 sql,而后引入 sql 文件。方式以下:

<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/changelogs/project.sql" relativeToChangelogFile="false"/>

</databaseChangeLog>
复制代码

直接把项目导出的数据库文件 project.sql 经过 include 标签引进来。

若是 <include> 的方式 sql 文件报错,能够换种方式引入,用 <sqlFile> 标签

<?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="solo">
        <sqlFile path="classpath:/liquibase/changelogs/project.sql" encoding="UTF-8" />
    </changeSet>
</databaseChangeLog>
复制代码

以上就是 SpringBoot 整合 Liquibase 的所有内容。

若是有疑问或好的建议,能够加 WX 交流:douglas1840

相关文章
相关标签/搜索