前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring框架集成liquibase

spring框架集成liquibase

作者头像
云捷易
修改2020-02-28 10:00:06
1.5K0
修改2020-02-28 10:00:06
举报
文章被收录于专栏:云捷易云捷易

Liquibase是一个开源数据库变更管理工具,帮助技术团队跟踪、版本管理和部署数据库结构变更。以下介绍如何在spring框架集成liquibase。

首先,在pom.xml文件中加入对于liquibase的依赖:

代码语言:javascript
复制
<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
    <version>3.8.4</version>
</dependency>

接下来定义bean:

代码语言:javascript
复制
<bean id="springLiquibase"
      class="liquibase.integration.spring.SpringLiquibase">
    <property name="dataSource" ref="dataSource"/>
    <property name="shouldRun" value="true"/>
    <property name="changeLog" value="classpath:db/changelog/db.changelog-master.xml"/>
</bean>

然后在resources文件夹下的db/changelog/文件夹创建db.changelog-master.xml,文件内容为:

代码语言:javascript
复制
<?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="db/changelog/REQ-0.xml"/>
</databaseChangeLog>

最后定义变更内容,保存在resources文件夹下的db/changelog/文件夹,文件名为REQ-0.xml,文件内容为:

代码语言:javascript
复制
<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="yourname" id="REQ-0">
        <createTable tableName="test_table">
            <column autoIncrement="true" name="id" type="INT">
                <constraints nullable="false" primaryKey="true"/>
            </column>
            <column name="table_name" type="varchar(50)">
            </column>
        </createTable>
    </changeSet>
</databaseChangeLog>

完成以上步骤后,启动应用时会自动执行上面定义的第一个变更。如果有新的变更需求,参照REQ-0的方式建立、引入变更文件。

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档