logo
GBase 8s
适配迁移
文章

SpringBoot 中的 Liquibase 适配GBase 8s数据库简单用例

GBase社区管理员
发表于2024-03-12 17:11:53473次浏览2个评论

1. 环境准备:

Springboot版本:2.7.10

适配GBase 8s的Liquibase版本:4.16.1

GBase 8s数据库版本:3.3.0_2

JDBC驱动版本:gbasedbtjdbc_3.5.0_2P20230714_8fe64f

2. 创建项目:

创建一个空白的springboot项目,添加Liquibase依赖。(文件名:liquibase-core-4.16.1-forGBase8s-3.3.0_2.jar)

在springboot配置文件src/main/resources/application.properties中添加数据库连接信息:

spring.datasource.url=jdbc:gbasedbt-sqli://[ip]:[port]/[database]:……
spring.datasource.username=gbasedbt
spring.datasource.password=xxxxxx
spring.datasource.driver-class-name=com.gbasedbt.jdbc.Driver

创建Liquibase配置类com.gbase.springbootdemo.config.LiquibaseConfig

package com.gbase.springbootdemo.config;

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. 添加changelog

创建目录src/main/resources/liquibase/changelogs/,存放所有 changelog 文件。

liquibase目录下的src/main/resources/liquibase/master.xml为liquibase的入口,通过 include 标签将其它的 changelog 文件引入进来:

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

完成后项目结构如下图所示:

4. 执行:

运行springboot应用,检查changelog是否被执行成功:

执行后目标数据库出现以下三个表:

其中databasechangelog和databasechangeloglock为Liquibase使用的表,用于历史记录和锁信息。

department表为changelog-create-table.xml内操作所创建的表。

附:changelog示例:changelog-create-table.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.1.xsd">

    <changeSet author="stiles" id="20210330-1">
        <createTable tableName="department" remarks="部门表">
            <column name="id" type="int" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
            <column name="active" type="boolean" defaultValueBoolean="true"/>
        </createTable>
    </changeSet>
    <changeSet author="stiles" id="20210330-2">
        <addColumn tableName="department">
            <column name="company" type="VARCHAR(100)"></column>
        </addColumn>
    </changeSet>

    <changeSet author="stiles" id="20210330-3">
        <insert tableName="department">
            <column name="id" valueNumeric="1"/>
            <column name="name" value="stiles_1"/>
            <column name="active" valueNumeric="1"/>
        </insert>
        <insert tableName="department">
            <column name="id" valueNumeric="2"/>
            <column name="name" value="stiles_2"/>
            <column name="active" valueNumeric="1"/>
        </insert>
        <insert tableName="department">
            <column name="id" valueNumeric="3"/>
            <column name="name" value="stiles_3"/>
            <column name="active" valueNumeric="0"/>
        </insert>
    </changeSet>
    <changeSet author="stiles" id="20210330-4">
        <update tableName="department">
            <column name="name" value="stiles_33"/>
            <where>name='stiles_3'</where>
        </update>
    </changeSet>
</databaseChangeLog>

评论

登录后才可以发表评论
liaosnet发表于 1 年前
liquibase-core-4.16.1-forGBase8s-3.3.0_2.jar
在哪里下载?
GBase用户16860发表于 6 个月前
启动后没有自动生成databasechangeloglock表是为什么