天天看點

liquibase的使用說明筆記

liquibase的使用說明筆記

使用說明:

我想用xml檔案的話就要修改配置(也可以在pom檔案中配置)

liquibase: 

enabled: true 

change-log: classpath:/db/changelog/db.changelog-master.xml 

 application-dao.yml  的配置說明:

spring:
  datasource:
    dynamic:
      primary: master
      datasource:
        master:
          username:  問問
          password:  面膜
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://資料庫IP:3306/執行個體名稱?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
  liquibase:
    change-log: classpath:liquibase/master.xml
mybatis-plus:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath*:mapper/*.xml      

POM的配置檔案詳情:

<dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>${liquibase.version}</version>
        </dependency>      

mybatis-config.xml

配置的詳情:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!-- 全局參數 -->
    <settings>
        <!-- 使全局的映射器啟用或禁用緩存。 -->
        <setting name="cacheEnabled" value="true"/>

        <!-- 全局啟用或禁用延遲加載。當禁用時,所有關聯對象都會即時加載。 -->
        <setting name="lazyLoadingEnabled" value="true"/>

        <!-- 當啟用時,有延遲加載屬性的對象在被調用時将會完全加載任意屬性。否則,每種屬性将會按需要加載。 -->
        <setting name="aggressiveLazyLoading" value="true"/>

        <!-- 是否允許單條sql 傳回多個資料集 (取決于驅動的相容性) default:true -->
        <setting name="multipleResultSetsEnabled" value="true"/>

        <!-- 是否可以使用列的别名 (取決于驅動的相容性) default:true -->
        <setting name="useColumnLabel" value="true"/>

        <!-- 允許JDBC 生成主鍵。需要驅動器支援。如果設為了true,這個設定将強制使用被生成的主鍵,有一些驅動器不相容不過仍然可以執行。 default:false -->
        <setting name="useGeneratedKeys" value="false"/>

        <!-- 指定 MyBatis 如何自動映射 資料基表的列 NONE:不隐射 PARTIAL:部分 FULL:全部 -->
        <setting name="autoMappingBehavior" value="PARTIAL"/>

        <!-- 這是預設的執行類型 (SIMPLE: 簡單; REUSE: 執行器可能重複使用prepared statements語句;BATCH:
            執行器可以重複執行語句和批量更新) -->
        <setting name="defaultExecutorType" value="SIMPLE"/>

        <!-- 使用駝峰命名法轉換字段。 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>

        <!-- 設定本地緩存範圍 session:就會有資料的共享 statement:語句範圍 (這樣就不會有資料的共享 ) defalut:session -->
        <setting name="localCacheScope" value="SESSION"/>

        <!-- 設定但JDBC類型為空時,某些驅動程式 要指定值,default:OTHER,插入空值時不需要指定類型 -->
        <setting name="jdbcTypeForNull" value="NULL"/>
        <setting name="callSettersOnNulls" value="true"/>
        <!-- 顯示sql日志 -->
        <setting name="logImpl" value="STDOUT_LOGGING"/>

    </settings>

</configuration>      

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="liquibase/change_log/init-table.xml" relativeToChangelogFile="false"/>
    <include file="liquibase/change_log/init-data.xml" relativeToChangelogFile="false"/>

</databaseChangeLog>      

init-table.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">

</databaseChangeLog>      

init-data.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">

</databaseChangeLog>      

最重要的倆個檔案就是需要建立表結構

@Slf4j
@EnableTransactionManagement
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class CorpusProviderApplication {

    public static void main(String[] args) throws IOException {
        SpringApplication.run(CorpusProviderApplication.class, args);
        log.info(" Application啟動成功!");
        System.in.read();
    }

}