[Liquibase]集成liquibase的兩種形式

除去單獨部署外,liquibase有兩種使用方式: springboot集成以及以插件形式運行。這篇先將集成的形式,下一篇開始着重講其中的語法。

一、springboot方式集成

程序架構組成:
在這裏插入圖片描述

1.引入依賴 pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.4.RELEASE</version>
  </parent>

  <dependencies>
    <!-- liquibase 依賴-->
    <dependency>
      <groupId>org.liquibase</groupId>
      <artifactId>liquibase-core</artifactId>
      <version>3.8.5</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
      <groupId> org.springframework.boot </groupId >
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>6.0.6</version>
    </dependency>
  </dependencies>

2.application.yml配置

spring:
  liquibase:
    change-log: classpath:liquibase/master.xml
    user: pcloud
    password: pcloud
    enabled: true
    drop-first: false
    url: jdbc:mysql://192.168.133.15:3306/webchat
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.133.15:3306/webchat
    username: pcloud
    password: pcloud

3.master.xml主文件

<?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.4.xsd">

    <include file="classpath:liquibase/change_log/2020-02-06-init-schema.xml" relativeToChangelogFile="false"/>
    <include file="classpath:liquibase/change_log/2020-02-06-add-user.sql"/>
    <include file="classpath:liquibase/change_log/2020-02-06-update-user.sql"/>
    <include file="classpath:liquibase/change_log/2020-02-06-delete-user.sql"/>
    <include file="classpath:liquibase/change_log/2020-02-06-add-user2.sql"/>
</databaseChangeLog>

4.數據修改文件

4.1xml形式

2020-02-06-init-schema.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.4.xsd">
    <property name="autoIncrement" value="true" dbms="mysql"/>
    <changeSet id="init-schema" author="Young" >
        <comment>init schema</comment>
        <createTable tableName="user">
            <column name="id" type="bigint" autoIncrement="${autoIncrement}">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="nick_name" type="varchar(255)">
                <constraints  nullable="false"/>
            </column>
            <column name="email" type="varchar(255)">
                <constraints  nullable="false"/>
            </column>
            <column name="register_time" type="timestamp"  defaultValueComputed="CURRENT_TIMESTAMP">
                <constraints nullable="false"/>
            </column>
        </createTable>

        <modifySql dbms="mysql">
            <append value="ENGINE=INNODB DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci"/>
        </modifySql>
    </changeSet>
</databaseChangeLog>

4.2 sql形式

--liquibase formatted sql
--changeset luyang:1
insert into user(nick_name,email,register_time) values('young','[email protected]',now());

二、插件形式集成

這種形式用於大部分我們的程序(Spring + SpringMvc集成)
項目組成架構:
在這裏插入圖片描述

1.引入依賴 pom.xml

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <spring.version>5.1.8.RELEASE</spring.version>
    <liquibase.version>3.8.5</liquibase.version>
    <mysql.version>6.0.6</mysql.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.liquibase</groupId>
      <artifactId>liquibase-core</artifactId>
      <version>${liquibase.version}</version>
    </dependency>
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.4</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>
    <dependency>
      <groupId>org.yaml</groupId>
      <artifactId>snakeyaml</artifactId>
      <version>1.25</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>spring-liquibase-webapps</finalName>
    <pluginManagement>
        <plugin>
          <groupId>org.kuali.maven.plugins</groupId>
          <artifactId>liquibase-maven-plugin</artifactId>
          <version>2.0.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

2.web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:spring/applicationcontext-*.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:spring/springmvc-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

3.application-liquibase.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                                           http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                                                   http://www.springframework.org/schema/context
                                                   http://www.springframework.org/schema/context/spring-context-4.1.xsd
                                                   http://www.springframework.org/schema/jee
                                                        http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
                                                        http://www.springframework.org/schema/mvc
                                                   http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
                                                   http://www.springframework.org/schema/util
                                                   http://www.springframework.org/schema/util/spring-util-4.1.xsd
                                                   http://www.springframework.org/schema/task
                                                   http://www.springframework.org/schema/task/spring-task-3.1.xsd">
    <!--讀取配置文件-->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="10" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <!-- 默認的在上,項目得在下,下面得最終生效 -->
                <value>classpath*:conf/*.properties</value>
            </list>
        </property>
    </bean>

    <!-- 數據源 -->
    <bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driverClass}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}"/>
        <property name="minPoolSize" value="${jdbc.minPoolSize}" />
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
        <property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
        <property name="autoCommitOnClose" value="${jdbc.autoCommit}" />
        <property name="checkoutTimeout" value="${jdbc.checkoutTimeout}"/>
        <property name="idleConnectionTestPeriod" value="3600"/>
    </bean>

    <!-- 初始化數據庫 -->
    <bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
        <property name="dataSource" ref="ds" />
        <property name="changeLog" value="classpath:liquibase/db-changeLog-master.xml" />
    </bean>
</beans>

4.db.properties

jdbc.driverClass=com.mysql.cj.jdbc.Driver

jdbc.url=jdbc:mysql://192.168.133.15:3306/webchat?useUnicode=true&characterEncoding=utf8
jdbc.username=pcloud
jdbc.password=pcloud

jdbc.checkoutTimeout=10000

jdbc.initialPoolSize=4
jdbc.minPoolSize=4
jdbc.maxPoolSize=100
jdbc.autoCommit=false

5.db-changeLog-master.xml

主數據文件和被引入數據文件寫法與springboot一模一樣

<?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.4.xsd">

    <include file="classpath:liquibase/changelog/2020-02-11-init-schema.xml" relativeToChangelogFile="false"/>
    <include file="classpath:liquibase/changelog/2020-02-11-first-log-add.sql"/>
    <include file="classpath:liquibase/changelog/2020-02-11-second-log-add.sql"/>
</databaseChangeLog>

6.2020-02-11-init-schema.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.4.xsd">
    <property name="autoIncrement" value="true" dbms="mysql"/>
    <changeSet id="init-schema" author="Young" >
        <comment>init schema</comment>
        <createTable tableName="log">
            <column name="id" type="bigint" autoIncrement="${autoIncrement}">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="log_detail" type="varchar(255)">
                <constraints  nullable="false"/>
            </column>
            <column name="log_time" type="timestamp"  defaultValueComputed="CURRENT_TIMESTAMP">
                <constraints nullable="false"/>
            </column>
        </createTable>

        <modifySql dbms="mysql">
            <append value="ENGINE=INNODB DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci"/>
        </modifySql>
    </changeSet>
</databaseChangeLog>

7.2020-02-11-first-log-add.sql

--liquibase formatted sql
--changeset luyang:1
insert into log(log_detail,log_time) values ('日誌詳情信息1','2020-02-11');
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章