宁波网站制作服务,做外贸推广自己网站,ps建模教程,wordpress目录册翻页1、是什么#xff1f;
Liquibase官网
Liquibase是一个开源的数据库管理工具#xff0c;可以帮助开发人员管理和跟踪数据库变更。它可以与各种关系型数据库和NoSQL数据库一起使用#xff0c;并提供多种数据库任务自动化功能#xff0c;例如数据库迁移、版本控制和监控。Li…1、是什么
Liquibase官网
Liquibase是一个开源的数据库管理工具可以帮助开发人员管理和跟踪数据库变更。它可以与各种关系型数据库和NoSQL数据库一起使用并提供多种数据库任务自动化功能例如数据库迁移、版本控制和监控。Liquibase还提供了一个Web界面可以方便地管理和跟踪数据库变更。它支持Java、Python、Ruby等多种语言可以轻松地集成到现有的开发环境中。
2、能干嘛
Liquibase主要功能包括
数据库迁移可以方便地将数据库从一个版本迁移到另一个版本。版本控制可以跟踪数据库变更的历史记录并可以根据需要回滚到以前的版本。监控可以监控数据库变更并在发生变更时收到通知。自动化可以自动化数据库任务例如在应用程序部署之前检查数据库完整性。
Liquibase可以帮助开发人员更加高效地管理数据库并减少由于数据库变更而导致的错误。
Liquibase的优点:
配置文件支持SQL、XML、JSON 或者 YAML版本控制按序执行可以用上下文控制sql在何时何地如何执行支持schmea的变更根据配置文件自动生成sql语句用于预览可重复执行迁移可插件拓展可回滚可兼容14中主流数据库如oraclemysqlpg等支持平滑迁移支持schema方式的多租户(multi-tenant)
3、怎么玩
这里主要使用SpringBoot整合Liquibase实现对数据库进行版本管理
(1) 引入依赖
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.ly/groupIdartifactIdspringboot-liquibase/artifactIdversion1.0-SNAPSHOT/versionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.1.0/version/parentpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.33/version/dependencydependencygroupIdorg.liquibase/groupIdartifactIdliquibase-core/artifactIdversion4.23.0/version/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.28/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-starter/artifactIdversion1.2.18/version/dependency/dependencies
/project(2) 配置数据源
package com.ly.config;import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;import javax.sql.DataSource;import static com.ly.config.DataSourcesConfig.SPRING_DATASOURCE;/*** author ly (个人博客:https://www.cnblogs.com/ybbit)* date 2023-07-22 16:28* tags 喜欢就去努力的争取*/
ConfigurationProperties(prefix SPRING_DATASOURCE)
SpringBootConfiguration
Data
public class DataSourcesConfig {public static final String SPRING_DATASOURCE spring.datasource;private String driverClassName;private String url;private String username;private String password;/*** 数据源配置** return*/Beanpublic DataSource dataSource() {DruidDataSource dataSource new DruidDataSource();dataSource.setDriverClassName(driverClassName);dataSource.setUrl(url);dataSource.setUsername(username);dataSource.setPassword(username);return dataSource;}
}
(3) 配置Liquibase
package com.ly.config;import liquibase.integration.spring.SpringLiquibase;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;import javax.sql.DataSource;/*** author ly (个人博客:https://www.cnblogs.com/ybbit)* date 2023-07-22 14:53* tags 喜欢就去努力的争取*/
ConditionalOnProperty(value spring.profiles.active, havingValue dev)
SpringBootConfiguration
public class LiquibaseConfig {public static final String CHANGE_LOG_PATH classpath:/liquibase/db.changelog-master.xml;Beanpublic SpringLiquibase liquibase(DataSource dataSource) {SpringLiquibase liquibase new SpringLiquibase();liquibase.setChangeLog(CHANGE_LOG_PATH);liquibase.setDataSource(dataSource);liquibase.setShouldRun(true);return liquibase;}}(4) 创建db.changelog-master.xml文件
?xml version1.0 encodingUTF-8?
databaseChangeLogxmlnshttp://www.liquibase.org/xml/ns/dbchangelogxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.liquibase.org/xml/ns/dbchangeloghttp://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd!--1includeAll 标签可以把一个文件夹下的所有 changelog 都加载进来。如果单个加载可以用 include。2includeAll 标签里有两个属性path 和 relativeToChangelogFile。2.1path 在 include 标签里是 file指定要加载的文件或文件夹位置2.2relativeToChangelogFile 文件位置的路径是否相对于 root changelog 是相对路径默认 false即相对于 classpath 是相对路径。--!-- includeAll pathchange/ relativeToChangelogFiletrue/--!--加入一张test_create_table表--include fileclasspath:liquibase/change/changelog_v1.0.xml/include!--给test_create_table表加一个email字段--include fileclasspath:liquibase/change/changelog_v2.0.xml/include!--修改test_create_table表加email字段--include fileclasspath:liquibase/change/changelog_v3.0.xml/include!--向test_create_table表加一条数据--include fileclasspath:liquibase/change/changelog_v4.0.xml/include/databaseChangeLog(5) 创建每一项的变更文件推荐把各个模块的变更都分门别类的整理好
changelog_v1.0.xml
?xml version1.1 encodingUTF-8 standaloneno?
databaseChangeLog xmlnshttp://www.liquibase.org/xml/ns/dbchangelogxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd!--changeSet每一个changeSet对应一个数据库相关的操作author修改人id唯一--!--加入一张表【test_create_table】--changeSet authorly id2023072201-1createTable remarks用户表 tableNametest_create_tablecolumn autoIncrementtrue nameid typeINT remarks主键constraints nullablefalse primaryKeytrue uniquetrue//columncolumn nameusername remarks用户名 typeVARCHAR(32)constraints uniquetrue nullablefalse//columncolumn namepassword remarks密码 typeVARCHAR(100)constraints uniquefalse nullablefalse//column/createTable/changeSet
/databaseChangeLog
changelog_v2.0.xml
?xml version1.1 encodingUTF-8 standaloneno?
databaseChangeLog xmlnshttp://www.liquibase.org/xml/ns/dbchangelogxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsdchangeSet authorly id2023072202-1!--加入一个email字段--addColumn tableNametest_create_tablecolumn nameemail typeVARCHAR(32) remarks邮箱constraints nullabletrue//column/addColumn/changeSet
/databaseChangeLog
changelog_v3.0.xml
?xml version1.1 encodingUTF-8 standaloneno?
databaseChangeLog xmlnshttp://www.liquibase.org/xml/ns/dbchangelogxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsdchangeSet authorly id2023072203-1!--重命名email列名称--renameColumn tableNametest_create_table oldColumnNameemail newColumnNamenewEmail columnDataTypeVARCHAR(50)//changeSet
/databaseChangeLogchangelog_v4.0.xml
?xml version1.1 encodingUTF-8 standaloneno?
databaseChangeLog xmlnshttp://www.liquibase.org/xml/ns/dbchangelogxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsdchangeSet authorly id2023072204-1!--插入一条数据--insert tableNametest_create_tablecolumn nameid value1/columncolumn nameusername valuezs/columncolumn namepassword value123/columncolumn namenewEmail valuezs163.com/column/insert/changeSet
/databaseChangeLog(6) SpringBoot配置文件
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/qbb01username: rootpassword: rootprofiles:active: dev(7) Main
package com.ly;import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;/*** author ly (个人博客:https://www.cnblogs.com/ybbit)* date 2023-07-22 14:52* tags 喜欢就去努力的争取*/
EnableConfigurationProperties
SpringBootApplication
public class LiquibaseApplication {public static void main(String[] args) {new SpringApplicationBuilder(LiquibaseApplication.class).run(args);}
}(7) 启动日志 (8) 数据库 4、ChangeSet标签集与作用
(1) add
标签说明addAutoIncrement将已存在的列改为自增列addColumn增加列addDefaultValue增加列的默认值addForeignKeyConstraint增加外键addLookupTable创建外键的关联表addNotNullConstraint增加非空值约束addPrimaryKey增加主键addUniqueConstraint增加唯一值约束
(2) create
标签说明createIndex创建索引createProcedure创建存储过程createSequence创建序列createTable创建表createView创建视图
(3) drop
标签说明dropAllForeignKeyConstraints删除全部外键约束dropColumn删除列dropDefaultValue删除默认值dropForeignKeyConstraint删除某一外键约束dropNotNullConstraint删除空值约束dropPrimaryKey删除主键dropProcedure删除存储过程dropSequence删除序列dropTable删除表dropUniqueConstraint删除唯一约束dropView删除视图
(4) rename
标签说明renameColumn重命名列renameSequence重命名序列renameTable重命名表renameView重命名视图
5、sql
标签说明sqlsql语句sqlFilesql文件
6、其他
标签说明insert插入数据update更新数据delete删除数empty空操作executeCommand执行命名alterSequence修改序列customChange自定义操作需自己实现loadData导入csv数据至已存在的表中loadUpdateData导入csv数据至表中表不存在则新建mergeColumns合并列modifyDataType修改数据类型output输出日志setColumnRemarks增加列说明setTableRemarks增加表说明stop停止liquibasetagDatabase打标签用于将来回滚
5、集成Maven插件
(1) 在pom.xml中加入下面的插件配置
buildpluginsplugingroupIdorg.liquibase/groupIdartifactIdliquibase-maven-plugin/artifactIdconfiguration!--properties文件路径该文件记录了数据库连接信息等--propertyFilesrc/main/resources/liquibase.properties/propertyFilepropertyFileWillOverridetrue/propertyFileWillOverride!--生成文件的路径--outputChangeLogFilesrc/main/resources/liquibase/change/changelog_base.xml/outputChangeLogFile/configuration/plugin/plugins/build(2) 在resources目录下加入liquibase.properties配置文件
#要连接库配置信息
drivercom.mysql.cj.jdbc.Driver
urljdbc:mysql://localhost:3306/qbb01
usernameroot
passwordroot
#liquibase
changeLogFilesrc/main/resources/liquibase/db.changelog-master.xml(3) 根据当前配置的数据源生成changelog (4) 结果 代码仓库springboot-liquibase