美文网首页
springboot 集成 liquibase 管理数据库版本

springboot 集成 liquibase 管理数据库版本

作者: 爱_别离 | 来源:发表于2019-07-19 10:21 被阅读0次

项目有时候去要项目启动就生成数据库表 或多人开发维护版的时候 用这个比较方便管理
自己去找liquibase gva坐标

package com.tlsq.service.flowable.config;

import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseConnection;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.DatabaseException;
import liquibase.resource.ClassLoaderResourceAccessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 配置类(注释不想修改了 之前写工作流的)
 * Flowable Data Extension Configuration.
 * @author: Lu Yang
 * @data: 2019-05-21 15:04
 */
@Configuration
public class DatabaseConfiguration {

    // 表的开头命名
    private static final String LIQUIBASE_CHANGELOG_PREFIX = "T_";

    @Bean
    public Liquibase liquibase(DataSource dataSource) {
        Liquibase liquibase = null;
        try {
            DatabaseConnection connection = new JdbcConnection(dataSource.getConnection());
            Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
            database.setDatabaseChangeLogTableName(LIQUIBASE_CHANGELOG_PREFIX + database.getDatabaseChangeLogTableName());
            database.setDatabaseChangeLogLockTableName(LIQUIBASE_CHANGELOG_PREFIX + database.getDatabaseChangeLogLockTableName());
            //  自己sql表脚本路径
            liquibase = new Liquibase("custom/flowable-extend.sql", new ClassLoaderResourceAccessor(), database);
            liquibase.update("luyang");
            return liquibase;
        } catch (Exception e) {
            throw new RuntimeException("Error creating liquibase database", e);

        } finally {
            closeDatabase(liquibase);
        }
    }

    private void closeDatabase(Liquibase liquibase) {
        if (liquibase != null) {
            Database database = liquibase.getDatabase();
            if (database != null) {
                try {
                    database.close();
                } catch (DatabaseException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

springboot 2.0以后在application.yml文件加上
spring:
    liquibase:
        enabled: false
--liquibase formatted sql
--changeset luyang:1

-- ----------------------------------------------------------
-- 用户操作类型表 所用sql格式看着比xml舒服 给出格式自己去修改 一定要加上文件头两个申明注意不要有空格
-- ----------------------------------------------------------
drop table if exists `t_operation_type`;
create table `t_operation_type`
(
  `id_`          int(11) not null auto_increment comment '主键ID',
  `type_`      varchar(32) default null comment '动作',
  `valid_`       bit(1)      default b'0' comment '是否有效 0:是  1:否',
  `create_time_` datetime    default current_timestamp comment '创建时间',
  primary key (`id_`)
) engine = innodb
  auto_increment = 1
  default charset = utf8mb4
  collate utf8mb4_unicode_ci comment = '用户操作类型表';

insert into t_operation_type (type_) values ('启动流程');
insert into t_operation_type (type_) values ('通过');
insert into t_operation_type (type_) values ('驳回');
insert into t_operation_type (type_) values ('终止');


-- 不允许用户id_NAME映射表 用户ID重复
alter table flowable.t_user_id_name add unique (user_id_);

相关文章

网友评论

      本文标题:springboot 集成 liquibase 管理数据库版本

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