SpringBoot使用Flyway進(jìn)行數(shù)據(jù)庫管理的操作方法
一、Flyway簡介
Flyway是一款數(shù)據(jù)庫遷移(migration)工具。簡單點說,就是在你部署應(yīng)用的時候,幫你執(zhí)行數(shù)據(jù)庫腳本的工具。Flyway支持SQL和Java兩種類型的腳本,你可以將腳本打包到應(yīng)用程序中,在應(yīng)用程序啟動時,由Flyway來管理這些腳本的執(zhí)行,這些腳本被Flyway稱之為migration。
就目前而言,我們部署應(yīng)用的流程大概是這樣的:
開發(fā)人員將應(yīng)用程序打包、按順序匯總并整理數(shù)據(jù)庫升級腳本
DBA拿到數(shù)據(jù)庫升級腳本檢查、備份、執(zhí)行,以完成數(shù)據(jù)庫升級
應(yīng)部署人員拿到應(yīng)用部署包,備份、替換,以完成應(yīng)用程序升級
引入Flyway之后的應(yīng)用部署流程大概是這樣的:
開發(fā)人員將應(yīng)用程序打包
應(yīng)部署人員拿到應(yīng)用部署包,備份、替換,以完成應(yīng)用程序升(Flyway將自動執(zhí)行升級/備份腳本)。
二、SpringBoot集成使用
1.pom.xml引入依賴
<?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.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <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> <!-- mysql 依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 數(shù)據(jù)庫版本管理 依賴 --> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> </dependency> <!-- 數(shù)據(jù)庫訪問依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- 自動生成get,set方法 依賴 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.application.properties
# 端口 server.port=8082 # 數(shù)據(jù)源 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=PRC&useSSL=false spring.datasource.username=root spring.datasource.password=root # flyway 注:可以完全不用配置 ## sql 腳本的位置,默認(rèn)為 classpath:db/migration??墒謩又付? spring.flyway.locations=classpath:db ## 指定數(shù)據(jù)源,如果沒有指定的話,將使用配置的主數(shù)據(jù)源 spring.flyway.url=jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=PRC&useSSL=false ## Flyway 管理的 Schema 列表,區(qū)分大小寫。默認(rèn)連接對應(yīng)的默認(rèn) Schema ## 如果這里明確指定了庫名,那么在 spring.flyway.url 連接中指定的庫名將無效 spring.flyway.schemas=test1 ## 用戶名 spring.flyway.user=root ## 密碼 spring.flyway.password=root ## 開啟,默認(rèn)開啟 spring.flyway.enabled=true
3.resources創(chuàng)建db數(shù)據(jù)庫腳本文件夾
V0.1.0__init_table.sql
-- 創(chuàng)建表
create table t_startAlarm
(
id int(100) primary key not null auto_increment,
name varchar(100),
type varchar(100)
)
V0.1.1__init_table.sql
-- 初始化數(shù)據(jù)
INSERT INTO t_startAlarm ( id , name , type ) VALUES
('1','電飯煲','用來蒸飯')
V0.1.2__init_table.sql
-- 初始化數(shù)據(jù)
INSERT INTO t_startAlarm ( id , name , type ) VALUES
('2','電飯煲2','用來蒸飯2')
4.啟動DemoApplication主啟動類
4.1只有V0.1.0__init_table.sql和V0.1.1__init_table.sql。
項目啟動,F(xiàn)lyway 會自動創(chuàng)建一個 flyway_schema_history 表,這個表用來記錄數(shù)據(jù)庫的更新歷史。
有了這條記錄,下次再啟動項目,V0.1.1__init_table.sql 這個腳本文件就不會執(zhí)行了,因為系統(tǒng)知道這個腳本已經(jīng)執(zhí)行過了,如果你還想讓 V0.1.1__init_table.sql 腳本再執(zhí)行一遍,需要手動刪除 flyway_schema_history 表中的對應(yīng)記錄,那么項目啟動時,這個腳本就會被執(zhí)行了。
注意:
我們在定義腳本的時候,除了 V 字開頭的腳本之外,還有一種 R 字開頭的腳本,V 字開頭的腳本只會執(zhí)行一次,而 R字開頭的腳本,只要腳本內(nèi)容發(fā)生了變化,啟動時候就會執(zhí)行。
使用了 Flyway之后,如果再想進(jìn)行數(shù)據(jù)庫版本升級,就不用該以前的數(shù)據(jù)庫腳本了,直接創(chuàng)建新的數(shù)據(jù)庫腳本,項目在啟動時檢測了有新的更高版本的腳本,就會自動執(zhí)行,這樣,在和其他同事配合工作時,也會方便很多。因為正常我們都是從Git 上拉代碼下來,不拉數(shù)據(jù)庫腳本,這樣要是有人更新了數(shù)據(jù)庫,其他同事不一定能夠收到最新的通知,使用了 Flyway就可以有效避免這個問題了。
所有的腳本,一旦執(zhí)行了,就會在 flyway_schema_history表中有記錄,如果你不小心搞錯了,可以手動從 flyway_schema_history 表中刪除記錄,然后修改 SQL腳本后再重新啟動(生產(chǎn)環(huán)境不建議)。
測試數(shù)據(jù)庫中結(jié)果:


添加V0.1.2__init_table.sql,測試數(shù)據(jù)庫中結(jié)果


三、項目整體結(jié)構(gòu)
代碼地址鏈接: SpringbootFlyway

參考文章
https://www.cnblogs.com/wangzh1guo/articles/13834706.html
到此這篇關(guān)于SpringBoot使用Flyway進(jìn)行數(shù)據(jù)庫管理的文章就介紹到這了,更多相關(guān)SpringBoot Flyway數(shù)據(jù)庫管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合Flyway的方法(數(shù)據(jù)庫版本遷移工具)
- Flyway詳解及Springboot集成Flyway的詳細(xì)教程
- SpringBoot整合flyway實現(xiàn)步驟解析
- SpringBoot使用flyway初始化數(shù)據(jù)庫
- SpringBoot整合flyway實現(xiàn)自動創(chuàng)建表的方法
- SpringBoot項目集成Flyway詳細(xì)過程
- SpringBoot使用Flyway進(jìn)行數(shù)據(jù)庫遷移的實現(xiàn)示例
- springboot配置flyway(入門級別教程)
- spring boot整合flyway實現(xiàn)數(shù)據(jù)的動態(tài)維護(hù)的示例代碼
相關(guān)文章
Spring?JPA使用CriteriaBuilder動態(tài)構(gòu)造查詢方式
這篇文章主要介紹了Spring?JPA使用CriteriaBuilder動態(tài)構(gòu)造查詢方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot中配置Web靜態(tài)資源路徑的方法
這篇文章主要介紹了SpringBoot中配置Web靜態(tài)資源路徑的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
總結(jié)Junit4,Junit5,Jupiter之間的聯(lián)系
Jupiter和Junit5之間有什么聯(lián)系?Jupiter提供了哪些新的測試方法?如何用IDEA和Jupiter生成可讀性更好的測試報告?文中有非常詳細(xì)的說明,需要的朋友可以參考下2021-06-06
JAVA StringBuffer類與StringTokenizer類代碼解析
這篇文章主要介紹了JAVA StringBuffer類與StringTokenizer類代碼解析,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
Spring Cloud Stream如何實現(xiàn)服務(wù)之間的通訊
這篇文章主要介紹了Spring Cloud Stream如何實現(xiàn)服務(wù)之間的通訊,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10
SpringBoot使用Spring-Data-Jpa實現(xiàn)CRUD操作
這篇文章主要為大家詳細(xì)介紹了SpringBoot使用Spring-Data-Jpa實現(xiàn)CRUD操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
SpringBoot上傳文件到本服務(wù)器 目錄與jar包同級問題
這篇文章主要介紹了SpringBoot上傳文件到本服務(wù)器 目錄與jar包同級問題,需要的朋友可以參考下2018-11-11
Javaweb 500 服務(wù)器內(nèi)部錯誤的解決
這篇文章主要介紹了Javaweb 500 服務(wù)器內(nèi)部錯誤的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

