文章目录
- 前言
- 一、liquibase是什么?
- 二、基本使用
-
- 1.项目结构
- 2.引入依赖
- 3.项目配置
- 4.运行结果
- 总结
前言
本文只记录liquibase快速使用,最佳实践参考:
Spring Boot使用Liquibase最佳实践
提示:以下是本篇文章正文内容,下面案例可供参考
一、liquibase是什么?
Liquibase是一个数据库表结构迭代演进变更的管理工具。开发人员可以不是直接针对某个特定的数据库编写SQL的创建、更新或删除数据库对象,而是在通过XML、YAML、JSON、SQL等文件中定义描述他们所需的数据库表结构的变更这些变更。更多请参考: [官方网址](https://www.liquibase.org/)二、基本使用
1.项目结构
2.引入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.liquibase</groupId><artifactId>liquibase-core</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency>
3.项目配置
1:yml配置
spring:datasource:username: rootpassword: 123456url: jdbc:mysql://localhost/liqiubase-demo?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=falseliquibase:url: jdbc:mysql://localhost/liqiubase-demo?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=falseuser: rootpassword: 123456change-log: classpath:/db/changelog/changelog-master.xml
2: changeLog-master.xml 配置
<?xml version="1.0" encoding="UTF-8"?><databaseChangeLogxmlns="http://www.liquibase.org/xml/ns/dbchangelog"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsdhttp://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd"><changeSet id="demo" author="demo.test"><sqlFile path="/sqlfiles/20201201.sql" /></changeSet></databaseChangeLog>
3: 20201201.xml
-- liquibase formatted sql-- ----------------------------
-- Table structure for sys_role_permission
-- ----------------------------
DROP TABLE IF EXISTS `sys_role_permission`;
CREATE TABLE `sys_role_permission` (`role_id` varchar(50) NOT NULL COMMENT '角色ID',`permission_code` varchar(100) NOT NULL COMMENT '权限code',PRIMARY KEY (`role_id`,`permission_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;-- ----------------------------
-- Records of sys_role_permission
-- ----------------------------INSERT INTO `sys_role_permission` VALUES ('1', 'qa:faq');
4.运行结果
总结
本文基于springboot2.4.0配置,不同版本或许有不同方式配置,仅供参考
源码地址: