springboot集成liquibase

417 阅读1分钟

pom

<dependency>    
    <groupId>org.liquibase</groupId>    
    <artifactId>liquibase-core</artifactId>    
    <version>3.5.3</version>
</dependency>

application.yml

配置changelog的配置文件,数据库用户名密码

liquibase.drop-first=false 默认为false,如果设置为true,liquibase将首先删除所有数据库对象的所有连接的用户。

配置类

contexts配置启用环境(spring.profiles.active),如果有多个环境,逗号隔开

脚本存放路径

master.xml

<?xml version="1.0" encoding="utf-8"?><databaseChangeLog       
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"        
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog     
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">   
 <!-- include新增的脚本-->    
<include file="classpath:config/liquibase/changelog/o2pcm_category_tl.sql" 
    relativeToChangelogFile="false"/>
</databaseChangeLog>

.sql changelog

--liquibase formatted sql

--changeset c-crystal.li202011170101:1
CREATE TABLE `o2pcm_category_tl_ly` (  `category_id` bigint(20) NOT NULL COMMENT '类别ID 标记为主键',  `lang` varchar(30) NOT NULL COMMENT '多语言 标记为主键',  `category_name` varchar(255) DEFAULT NULL COMMENT '类别名称',  UNIQUE KEY `o2pcm_category_tl_u1` (`category_id`,`lang`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='类别多语言';

--changeset zhouyi:2INSERT INTO `o2_product`.`o2pcm_category_tl_ly` (`category_id`, `lang`, `category_name`) VALUES ('1', 'en_US', '名称');

--changeset zhouyi:3
INSERT INTO `o2_product`.`o2pcm_category_tl_ly` (`category_id`, `lang`, `category_name`) VALUES ('2', 'en_US', '名称');

changelog支持多种格式:xml、yaml、json、sql。

这里使用sql文件,必须添加--liquibase formatted sql,才能被识别为changelog文件

changeset使用--注释,格式是--changeset 作者:changeid,作者+changeid唯一

参考资料:

zhuanlan.zhihu.com/p/66187627

juejin.cn/post/684490…