項目結構java
就是從io.spring.start下的乾淨的Springboot項目。mysql
(一)建立表git
CREATE TABLE `school` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8_bin DEFAULT NULL, `age` int(11) DEFAULT NULL, `title` varchar(255) COLLATE utf8_bin DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
(二)配置pom.xmlgithub
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.vincent</groupId> <artifactId>liquibase</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>liquibase</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.15.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <db.driver>com.mysql.jdbc.Driver</db.driver> <db.dropFirst>false</db.dropFirst> <db.url>jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false</db.url> <db.username>root</db.username> <db.password>451179</db.password> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.liquibase</groupId> <artifactId>liquibase-maven-plugin</artifactId> <version>3.5.3</version> <dependencies> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.18.2-GA</version> </dependency> </dependencies> <configuration> <changeLogFile>src/main/resources/config/liquibase/master_changelog.xml</changeLogFile> <diffChangeLogFile> src/main/resources/config/liquibase/changelog/${maven.build.timestamp}_changelog.xml </diffChangeLogFile> <outputChangeLogFile>src/main/resources/config/liquibase/changelog/changelog_original.xml </outputChangeLogFile> <driver>${db.driver}</driver> <url>${db.url}</url> <dropFirst>false</dropFirst> <defaultSchemaName/> <username>${db.username}</username> <password>${db.password}</password> <referenceUrl>hibernate:spring:com.jaguar.myapp.domain?dialect=&hibernate.ejb.naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy</referenceUrl> <verbose>true</verbose> <logging>debug</logging> <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase> </configuration> </plugin> </plugins> </build> </project>
(三)application.yml文件web
spring: #數據庫相關的配置 datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false username: root password: root liquibase: enabled: true change-log: classpath:/config/liquibase/master_changelog.xml
(四)mvn clean install spring
此刻會發現mvn報錯了,主要錯誤信息以下sql
(五)建立這個缺乏的文件master_changelog.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.5.xsd"> <!-- mvn liquibase:update --> </databaseChangeLog>
(六)繼續mvn clean installapache
此刻發現,終於能夠不報錯了。app
(七)根據數據庫反向生成changeLog文件
執行命令:mvn liquibase:generateChangeLog
此時會報錯
按照錯誤的提示創建路徑,這邊只用創建不須要把文件建好,由於會自動生成。只須要文件夾便可
再次執行命令
能夠看到文件已經自動生成,自動生成的文件changelog_original.xml是這樣子的的
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<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-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<changeSet author="vincent (generated)" id="1535544566470-1">
<createTable tableName="school">
<column autoIncrement="true" name="id" type="BIGINT">
<constraints primaryKey="true"/>
</column>
<column name="name" type="VARCHAR(255)"/>
<column name="age" type="INT"/>
<column name="title" type="VARCHAR(255)"/>
</createTable>
</changeSet>
</databaseChangeLog>
(四) 清空當前數據庫,包括liquibase的版本信息
執行命令:mvn liquibase:dropAll
(五)將xml的改變動新到數據庫
將剛剛的變動放到master_changelog.xml的目標執行中去
執行命令:mvn liquibase:update
這時候一樣會報錯
錯誤信息很明顯,缺乏changelog_original.xml
可是,我們項目中不是已經存在changelog_original.xml了嗎?爲什麼還報他不存在呢?
緣由很簡單,由於沒有編譯
此刻執行mvn clean install
執行完能夠發現文件編譯進去了,再次執行mvn liquibase:update
此刻能夠success了
咱們看下數據庫發送了什麼變化,多了2個表DATABASECHANGELOG,DATABASECHANGELOGLOCK
至此,集成工做已經完成。
如今咱們在數據庫中新增一個student表
CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, `school_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=11123 DEFAULT CHARSET=utf8
將信息更新到changelog文件中去
執行命令:mvn liquibase:liquibase:dbDoc
打開changelog_original.xml文件
最經常使用的命令說明:
update(將xml的改變動新到數據庫)
rollback(回滾到某一版本或者某一時刻,必需要帶上rollbackTag參數)
dbDoc (生成數據庫文檔)
dropAll(慎用,清空當前數據庫,包括liquibase的版本信息)
generateChangeLog(根據數據庫反向生成changeLog文件)
tag(爲當前數據庫打上標籤)
項目github地址:https://github.com/MonthsEmpty/liquibase.git
參考文章:https://blog.csdn.net/xiang__liu/article/details/80800205