• SpringBoot整合liquibase


    源码分析:

    class LiquibaseChangelogMissingFailureAnalyzer extends AbstractFailureAnalyzer<ChangeLogParseException> {
    
    	private static final String MESSAGE_SUFFIX = " does not exist";
    
    	@Override
    	protected FailureAnalysis analyze(Throwable rootFailure, ChangeLogParseException cause) {
    		if (cause.getMessage().endsWith(MESSAGE_SUFFIX)) {
    			String changelogPath = extractChangelogPath(cause);
    			return new FailureAnalysis(getDescription(changelogPath),
    					"Make sure a Liquibase changelog is present at the configured path.", cause);
    		}
    		return null;
    	}
    
    	private String extractChangelogPath(ChangeLogParseException cause) {
    		return cause.getMessage().substring(0, cause.getMessage().length() - MESSAGE_SUFFIX.length());
    	}
    
    	private String getDescription(String changelogPath) {
    		return "Liquibase failed to start because no changelog could be found at '" + changelogPath + "'.";
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    LiqubaseDatabaseInitliazer:

    class LiquibaseDatabaseInitializerDetector extends AbstractBeansOfTypeDatabaseInitializerDetector {
    
    	@Override
    	protected Set<Class<?>> getDatabaseInitializerBeanTypes() {
    		return Collections.singleton(SpringLiquibase.class);
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    整理jar 包:
    package org.springframework.boot.liquibase;

    二。使用:
    LiquiBase 是一个数据库重构和迁移的开源框架,通过日志的方式来记录数据库的变更。通过执行日志文件中的修改,将数据库更新或回滚到达一致的状态。

    三。使用步骤:

    <!-- liquibase -->
    <dependency>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-core</artifactId>
    </dependency>
    
    <!--mysql jdbc驱动 -->
    <dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>8.0.27</version>
    </dependency>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.2、创建SpringBoot配置文件来指定一些配置文件地址
    LiquibaseConfig.java
    @Configuration
    public class LiquibaseConfig {

    @Bean
    public SpringLiquibase liquibase(DataSource dataSource) {
        SpringLiquibase liquibase = new SpringLiquibase();
        //设置数据源
        liquibase.setDataSource(dataSource);
        //sql文件位置
        liquibase.setChangeLog("classpath:liquibase/master.xml");
        return liquibase;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    }

    • application.properties
      spring.datasource.username=root
      spring.datasource.password=123456
      spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
      spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    在这里插入代码片
    
    • 1

    2.3、创建配置文件

    
    
    
    • 1
    • 2
  • 相关阅读:
    【计算机毕业设计】33.快递取件预约系统源码
    echarts 绘制仿立体 矩形 渐变柱状图图表
    车载通信与DDS标准解读系列(1):DDS-RPC
    JVM虚拟机:JVM中垃圾回收器的总结
    项目技术复盘
    Capture One Studio for Mac:打造完美影像的利器
    机器学习笔记1
    BS-GX-017基于SSM实现的在线考试管理系统
    每天一个数据分析题(一百八十三)
    贪心算法,删数问题
  • 原文地址:https://blog.csdn.net/xiamaocheng/article/details/126917898