Springboot集成Liquibase笔记整理

  1. 添加依赖
    <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-core</artifactId>
    </dependency>
    
  2. 添加配置
    spring:
      liquibase:
        contexts: dev,test
        enabled: true
    
  3. 编写liquibase配置类
    @Configuration
    @EnableConfigurationProperties(LiquibaseProperties.class)
    public class LiquibaseConfig {
        @Bean
        public SpringLiquibase liquibase(DataSource dataSource, LiquibaseProperties properties) {
            SpringLiquibase liquibase = new SpringLiquibase();
            liquibase.setDataSource(dataSource);
            //指定changelog的位置,这里使用的一个master文件引用其他文件的方式
            liquibase.setChangeLog("classpath:liquibase/master.xml");
            liquibase.setContexts(properties.getContexts());
            liquibase.setDefaultSchema(properties.getDefaultSchema());
            liquibase.setDropFirst(properties.isDropFirst());
            liquibase.setShouldRun(properties.isEnabled());
            liquibase.setChangeLogParameters(properties.getParameters());
            liquibase.setRollbackFile(properties.getRollbackFile());
            liquibase.setShouldRun(true);
            return liquibase;
        }
    }
    
  4. 编写master.xml文件(注意includeAll配置的路径与changelog文件的路径)
    <?xml version="1.0" encoding="utf-8"?>
    <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.6.xsd">
        <property name="now" value="now()" dbms="h2"/>
        <property name="now" value="now()" dbms="mysql"/>
        <property name="floatType" value="float4" dbms="postgresql, h2"/>
        <property name="floatType" value="float" dbms="mysql, oracle, mssql, mariadb"/>
        <property name="clobType" value="clob" dbms="h2"/>
        <property name="clobType" value="clob" dbms="mysql, oracle, mssql, mariadb, postgresql"/>
        <property name="uuidType" value="varchar(36)" dbms="h2, mysql, mariadb"/>
        <includeAll path="liquibase/changelog/"/>
    </databaseChangeLog>
    
  5. 编写changelog文件 00000000000000_init_schema.xml
    <?xml version="1.0" encoding="utf-8"?>
    <databaseChangeLog
            xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
            xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
            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.6.xsd
                            http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
        <property name="autoIncrement" value="true"/>
        <changeSet author="system" id="00000000000001" context="dev">
            <createTable tableName="hello_date_time_wrapper">
                <column  name="id" type="BIGINT">
                    <constraints primaryKey="true" primaryKeyName="hello_date_time_wrapperPK"/>
                </column>
                <column name="instant" type="timestamp"/>
                <column name="local_date_time" type="timestamp"/>
                <column name="offset_date_time" type="timestamp"/>
                <column name="zoned_date_time" type="timestamp"/>
                <column name="local_time" type="time"/>
                <column name="offset_time" type="time"/>
                <column name="local_date" type="date"/>
            </createTable>
        </changeSet>
    </databaseChangeLog>
    
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值