美文网首页liquibase全面介绍
2. liquibase和springboot

2. liquibase和springboot

作者: 飞行员舒克_ed03 | 来源:发表于2020-09-02 14:33 被阅读0次

spring-boot已经集成了liquibase。
liquibase包中也做了对spring项目的适配。
在spring-boot的spring-boot-autoconfigure包中有LiquibaseAutoConfiguration的类。
从这个类可以发现:liquibase的配置均以spring.liquibase开头。
我们就以一个springboot的项目说起应该如何配置。

1、pom中引入maven依赖

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

2、application.properties中加入配置

spring.liquibase.change-log=classpath:changelog-master.xml

其中 changelog-master.xml 是主配置文件,这个配置是告诉spring主配置文件在什么位置。

3、根据上述配置在resources目录下创建changelog-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.8.xsd">
    <include file="changelog-1.xml" relativeToChangelogFile="true" />
</databaseChangeLog>

主配置文件的作用是将其他的配置文件集中到一起。不同的版本用不同的文件,这个更容易区分。

4、我们再根据主配置文件的内容,在相同的目录下创建名为changelog-1.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.8.xsd">

    <changeSet id="a_table" author="9546459@qq.com">
        <comment>create table a_table</comment>
        <createTable tableName="a_table" remarks="这是一张表">
            <column name="id" type="VARCHAR(48)" remarks="其中名为id的列" >
                <constraints primaryKey="true" nullable="false" />
            </column>
            <column name="content" type="VARCHAR" remarks="其中名为content的列" />
        </createTable>
        <rollback>
            <dropTable tableName="a_table" />
        </rollback>
    </changeSet>

    <changeSet id="1-tag" author="9546459@qq.com">
        <tagDatabase tag="1.0" />
    </changeSet>

changeSet在上一节已经说过。其中id和author是必填字段。comment是对changeSet工作的说明。
第一个changeSet是创建一张a_table的表,主键是id。
第二个是打个标签。
后面我们看看运行后的效果。接下来我们说说liquibase如何使用数据库连接池。

5、 liquibase使用的数据库连接

如果项目中已经配置了数据库连接池,那liquibase自动使用数据库连接池。如果未配置数据库连接,那还需要配置liquibase的数据库连接。同样是在application.properties中配置

spring.liquibase.user
spring.liquibase.password
spring.liquibase.url

配完这些liquibase的配置就结束了。

6、下面让我们看看运行之后的效果吧

启动spring项目后,我们查看数据库。
数据库里多了3张表


3张数据库表
其中databasechangelog表内容如下表
id author filename dateexecuted orderexecuted exectype md5sum description comments tag liquibase contexts labels deployment_id
a_table 9546459@qq.com classpath:changelog-1.xml 2020-09-02 14:16:56 1 EXECUTED 8:c89dba457e91aa5098939a8a9351ece7 createTable tableName=a_table create table a_table 3.9.0 9027416248
1-tag 9546459@qq.com classpath:changelog-1.xml 2020-09-02 14:16:56 2 EXECUTED 8:3c7be0136474b1608ff678d78f1a3738 tagDatabase 1.0 3.9.0 9027416248

2个changeSet分别生成2条记录,说下其中的md5sum字段,这个是根据changeSet的内容经过md5算出来的。是防止changeSet的被内容篡改设计的。changeSet的执前会计算当前md5sum值,并与记录表中的md5sum值比较是否一致,如一致则跳过不执行(说明这条changeSet已经执行过了),如不一致则报错。

另外一张表是databasechangeloglock内容如下表
id locked lockgranted lockedby
1 false

这张即是锁表。保证集群和数据库安全。

下一节我们讲一讲liquibase使用技巧及最佳实践。

相关文章

网友评论

    本文标题:2. liquibase和springboot

    本文链接:https://www.haomeiwen.com/subject/cjiisktx.html