SpringBoot + Liquibase 简单学习过程

今天项目中需要用到数据库版本控制工具,因为之前项目用过一段时间,使用感觉不错,所以这次还是选择用 liquibase 。好了,接下来进入正题。

1. 创建 SpringBoot 项目

SpringBoot + Liquibase 简单学习过程_第1张图片image.gif

SpringBoot + Liquibase 简单学习过程_第2张图片image.gif

SpringBoot + Liquibase 简单学习过程_第3张图片image.gif

SpringBoot + Liquibase 简单学习过程_第4张图片image.gif

2. 添加 Maven 依赖


    org.springframework.boot
    spring-boot-starter-web


    org.liquibase
    liquibase-core


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.1.3


    mysql
    mysql-connector-java

3. application.properties 配置

spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
spring.datasource.driver=com.mysql.cj.jdbc.Driver
spring.liquibase.change-log=classpath:/db/changelog/master.xml
spring.liquibase.enabled=true
spring.liquibase.contexts=dev, faker

4. 创建 changelog 文件夹

根据 application.properties 中的 spring.liquibase.change-log=classpath:/db/changelog/master.xml 创建 liquibase 的 master.xml 文件。



    

创建子文件 changelog-1.0.xml


当前项目目录如下:

SpringBoot + Liquibase 简单学习过程_第5张图片image.gif

5. 启动项目,查看数据库

 liquibase 生成了两张表

image.gif

databasechangelog :用于 Liquibase 操作记录,以行的形式跟踪每个变更集,并由“ id”,“ author”和“ filename”列的组合标识。

databasechangeloglock :来确保一次仅运行一个 Liquibase 实例。

6. 编写 changelog-1.0.xml 文件




    
        
            
                
            
            
            
            
            
            
        
    



7. 执行查看表有没有建好

image.gif

order 表已经创建好了。

8. liquibase 相关使用



    
        
            
                
            
            
            
            
            
            
        
    


    
    
        
            
        
    

    
    
        
    

    
    
        
    

    
    
        
    

      
    
        
    


到此结束,欢迎大家下方留言互相学习~

你可能感兴趣的:(Java基础,java,liquibase,spring)