栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot整合liquibase

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot整合liquibase

1、依赖
这里数据源使用的是postgresql

		
			org.liquibase
			liquibase-core
		
		
		
			org.postgresql
			postgresql
			42.2.14
		
		
		
			com.baomidou
			mybatis-plus-boot-starter
			3.4.1
		

2、配置

spring:
  liquibase:
    # 开启 liquibase
    enabled: true
    # 配置 changlog 文件路径
    change-log: classpath:liquibase/master.xml
    url: jdbc:postgresql://${POSTGRES_HOST:39.98.xxx.xxx}:${POSTGRES_PORT:xxx}/${POSTGRES_DATAbase:xxx}?sslmode=disable¤tSchema=${POSTGRES_CURRENTSCHEMA:test}
    user: ${POSTGRES_USERNAME:xxx}
    password: ${POSTGRES_PASSWORD:123456}
  datasource:
    username: ${POSTGRES_USERNAME:xxx}
    password: ${POSTGRES_PASSWORD:123456}
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://${POSTGRES_HOST:39.98.xxx.xxx}:${POSTGRES_PORT:xxxx}/${POSTGRES_DATAbase:xxx}?sslmode=disable¤tSchema=${POSTGRES_CURRENTSCHEMA:test}
mybatis-plus:
  mapper-locations=classpath: mapper/*xml

3、配置类

@Configuration
public class LiquibaseConfig {

    @Qualifier("dataSource")
    private DataSource dataSource;


//    更多配置:
//
//    spring.liquibase.change-log 配置文件的路径,默认值为 classpath:/db/changelog/db.changelog-master.yaml
//    spring.liquibase.check-change-log-location 检查 change log的位置是否存在,默认为true.
//    spring.liquibase.contexts 用逗号分隔的运行环境列表。
//    spring.liquibase.default-schema 默认数据库 schema
//    spring.liquibase.drop-first 是否先 drop schema(默认 false)
//    spring.liquibase.enabled 是否开启 liquibase(默认为 true)
//    spring.liquibase.password 数据库密码
//    spring.liquibase.url 要迁移的JDBC URL,如果没有指定的话,将使用配置的主数据源.
//    spring.liquibase.user 数据用户名
//    spring.liquibase.rollback-file 执行更新时写入回滚的 SQL文件

    @Bean
    public SpringLiquibase liquibase() {
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setDataSource(dataSource);
        liquibase.setChangeLog("classpath:liquibase/master.xml");
//        liquibase.setContexts("development,test,preproduction,production");
//        liquibase.setShouldRun(true);
        return liquibase;
    }

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
}

4、创建master.xml



5、启动项目

自动生成两张表

6、简单使用
创建表


         
    
        
            
                
            
            
            
            
            
            
            
            
        
    
    

启动项目

其他

// 新增删除字段
    
        
        
            
        
    
// 修改字段的类型
    
        
    
// 如果master.xml 写的太多了,可以在另一个文件中继续写
 

以后各个环境的表结构,都能自动同步啦

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/684465.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号