springboot + liquibase数据库版本控制

创建项目

idea创建springboot项目时已集成了liquibase可以直接勾选,其中flyway是另一种数据库版本控制工具,有兴趣的可以去了解下
在这里插入图片描述

liquibase做了什么

项目启动时,liquibase会根据yml中配置的changelog-master.xml文件中配置的路径找到path下的所有未执行过的脚本执行一遍并在数据库中做记录,下次启动就不会执行了

配置 application.yml

server:
  port: 8080
  servlet:
    context-path: /liquibase

spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/liquibase-demo?useSSL=false&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8&allowMultiQueries=true&useAffectedRows=true
    username: root
    password: root
    autoCommit: false
    minIdle: 5
    maximumPoolSize: 20
    connectionInitSql: select 1
    hikari:
      idle-timeout: 10000
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
  liquibase:
    # 是否开启 liquibase(默认为 true)
    enabled: true
    # 配置文件的路径,默认值为 classpath:/db/changelog/db.changelog-master.yaml
    change-log: classpath:/db/changelog/changelog-master.xml

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>liquibase-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>liquibase-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

changelog-master.xml

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
    <!-- 扫面 所有 Sql文件路径 ,相对路径-->
    <includeAll path="../sql/" relativeToChangelogFile="true"/>
</databaseChangeLog>

测试脚本

-- 创建user表
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

效果图

在这里插入图片描述

demo源码地址

在Spring Boot中集成Liquibase可以帮助你管理数据库版本控制和迁移。Liquibase是一个开源的数据库变更管理工具,它使用XML或YAML文件来描述数据库的变更,并提供了一些命令行工具和Java API来执行这些变更。 下面是将Liquibase集成到Spring Boot项目的步骤: 1. 添加依赖:在你的项目的`pom.xml`文件中,添加Liquibase和相应的数据库驱动的依赖。例如,如果你使用MySQL数据库,可以添加以下依赖: ```xml <dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> <version>4.5.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> ``` 2. 创建Liquibase配置文件:在项目的资源目录下创建一个`liquibase.properties`或`liquibase.yml`文件,用于配置Liquibase的连接信息和变更文件的位置。例如,创建一个`liquibase.properties`文件,并添加以下内容: ```properties # 数据库连接信息 url=jdbc:mysql://localhost:3306/mydb username=root password=your_password # 变更文件位置 changeLogFile=classpath:db/changelog.xml ``` 3. 创建变更文件:在项目的资源目录下创建一个`db/changelog.xml`文件(根据你在配置文件中指定的位置),用于描述数据库的变更。你可以在该文件中定义创建表、添加列等操作。以下是一个简单的例子: ```xml <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.5.xsd"> <changeSet id="1" author="your_name"> <createTable tableName="example_table"> <column name="id" type="bigint" autoIncrement="true"> <constraints primaryKey="true" nullable="false"/> </column> <column name="name" type="varchar(255)"/> </createTable> </changeSet> </databaseChangeLog> ``` 4. 启用Liquibase:在你的Spring Boot应用程序的启动类上添加`@EnableLiquibase`注解,以启用Liquibase的自动配置。例如: ```java @SpringBootApplication @EnableLiquibase public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 现在,当你运行Spring Boot应用程序时,Liquibase会自动检查数据库的变更,并根据变更文件进行相应的更新操作。 希望对你有所帮助!如有任何疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值