SpringBoot使用Flyway進行數(shù)據(jù)庫管理的操作方法
一、Flyway簡介
Flyway是一款數(shù)據(jù)庫遷移(migration)工具。簡單點說,就是在你部署應用的時候,幫你執(zhí)行數(shù)據(jù)庫腳本的工具。Flyway支持SQL和Java兩種類型的腳本,你可以將腳本打包到應用程序中,在應用程序啟動時,由Flyway來管理這些腳本的執(zhí)行,這些腳本被Flyway稱之為migration。
就目前而言,我們部署應用的流程大概是這樣的:
開發(fā)人員將應用程序打包、按順序匯總并整理數(shù)據(jù)庫升級腳本
DBA拿到數(shù)據(jù)庫升級腳本檢查、備份、執(zhí)行,以完成數(shù)據(jù)庫升級
應部署人員拿到應用部署包,備份、替換,以完成應用程序升級
引入Flyway之后的應用部署流程大概是這樣的:
開發(fā)人員將應用程序打包
應部署人員拿到應用部署包,備份、替換,以完成應用程序升(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 腳本的位置,默認為 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ū)分大小寫。默認連接對應的默認 Schema ## 如果這里明確指定了庫名,那么在 spring.flyway.url 連接中指定的庫名將無效 spring.flyway.schemas=test1 ## 用戶名 spring.flyway.user=root ## 密碼 spring.flyway.password=root ## 開啟,默認開啟 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)知道這個腳本已經執(zhí)行過了,如果你還想讓 V0.1.1__init_table.sql 腳本再執(zhí)行一遍,需要手動刪除 flyway_schema_history 表中的對應記錄,那么項目啟動時,這個腳本就會被執(zhí)行了。
注意:
我們在定義腳本的時候,除了 V 字開頭的腳本之外,還有一種 R 字開頭的腳本,V 字開頭的腳本只會執(zhí)行一次,而 R字開頭的腳本,只要腳本內容發(fā)生了變化,啟動時候就會執(zhí)行。
使用了 Flyway之后,如果再想進行數(shù)據(jù)庫版本升級,就不用該以前的數(shù)據(jù)庫腳本了,直接創(chuàng)建新的數(shù)據(jù)庫腳本,項目在啟動時檢測了有新的更高版本的腳本,就會自動執(zhí)行,這樣,在和其他同事配合工作時,也會方便很多。因為正常我們都是從Git 上拉代碼下來,不拉數(shù)據(jù)庫腳本,這樣要是有人更新了數(shù)據(jù)庫,其他同事不一定能夠收到最新的通知,使用了 Flyway就可以有效避免這個問題了。
所有的腳本,一旦執(zhí)行了,就會在 flyway_schema_history表中有記錄,如果你不小心搞錯了,可以手動從 flyway_schema_history 表中刪除記錄,然后修改 SQL腳本后再重新啟動(生產環(huán)境不建議)。
測試數(shù)據(jù)庫中結果:
添加V0.1.2__init_table.sql,測試數(shù)據(jù)庫中結果
三、項目整體結構
代碼地址鏈接: SpringbootFlyway
參考文章
https://www.cnblogs.com/wangzh1guo/articles/13834706.html
到此這篇關于SpringBoot使用Flyway進行數(shù)據(jù)庫管理的文章就介紹到這了,更多相關SpringBoot Flyway數(shù)據(jù)庫管理內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- SpringBoot整合Flyway的方法(數(shù)據(jù)庫版本遷移工具)
- Flyway詳解及Springboot集成Flyway的詳細教程
- SpringBoot整合flyway實現(xiàn)步驟解析
- SpringBoot使用flyway初始化數(shù)據(jù)庫
- SpringBoot整合flyway實現(xiàn)自動創(chuàng)建表的方法
- SpringBoot項目集成Flyway詳細過程
- SpringBoot使用Flyway進行數(shù)據(jù)庫遷移的實現(xiàn)示例
- springboot配置flyway(入門級別教程)
- spring boot整合flyway實現(xiàn)數(shù)據(jù)的動態(tài)維護的示例代碼
相關文章
Java并發(fā)編程之詳解ConcurrentHashMap類
在之前的文章中已經為大家介紹了java并發(fā)編程的工具:BlockingQueue接口、ArrayBlockingQueue、DelayQueue、LinkedBlockingQueue、PriorityBlockingQueue、SynchronousQueue、BlockingDeque接口,本文為系列文章第八篇.需要的朋友可以參考下2021-06-06Java多線程高并發(fā)中解決ArrayList與HashSet和HashMap不安全的方案
ArrayList實現(xiàn)了可變大小的數(shù)組。它允許所有元素,包括null。ArrayList沒有同步,HashMap和Hashtable類似,不同之處在于HashMap是非同步的,并且允許null,關于HashSet有一件事應該牢記,即就條目數(shù)和容量之和來講,迭代是線性的,接下來讓我們詳細來了解吧2021-11-11Spring MVC全局異常處理和單元測試_動力節(jié)點Java學院整理
本篇文章主要介紹了Spring MVC全局異常處理和單元測試,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08- 下面小編就為大家?guī)硪黄狫ava創(chuàng)建數(shù)組的幾種方式總結。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望能給大家?guī)韼椭?/div> 2021-06-06
最新評論