最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot使用Flyway進行數(shù)據(jù)庫管理的操作方法

 更新時間:2021年09月17日 12:01:20   作者:懶蟲蟲~  
Flyway是一個開源的數(shù)據(jù)庫版本管理工具,并且極力主張“約定大于配置”,簡單、專注、強大。接下來通過本文給大家介紹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)知道這個腳本已經(jīng)執(zhí)行過了,如果你還想讓 V0.1.1__init_table.sql 腳本再執(zhí)行一遍,需要手動刪除 flyway_schema_history 表中的對應記錄,那么項目啟動時,這個腳本就會被執(zhí)行了。

注意:

我們在定義腳本的時候,除了 V 字開頭的腳本之外,還有一種 R 字開頭的腳本,V 字開頭的腳本只會執(zhí)行一次,而 R字開頭的腳本,只要腳本內(nèi)容發(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腳本后再重新啟動(生產(chǎn)環(huán)境不建議)。

測試數(shù)據(jù)庫中結(jié)果:

在這里插入圖片描述
請?zhí)砑訄D片描述

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

請?zhí)砑訄D片描述

請?zhí)砑訄D片描述

三、項目整體結(jié)構(gòu)

代碼地址鏈接: SpringbootFlyway

在這里插入圖片描述

參考文章

https://www.cnblogs.com/wangzh1guo/articles/13834706.html

到此這篇關(guān)于SpringBoot使用Flyway進行數(shù)據(jù)庫管理的文章就介紹到這了,更多相關(guān)SpringBoot Flyway數(shù)據(jù)庫管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

望奎县| 赤水市| 盐池县| 黄大仙区| 胶南市| 涡阳县| 五莲县| 凉城县| 浪卡子县| 新昌县| 北碚区| 安图县| 永新县| 綦江县| 同心县| 尼勒克县| 克什克腾旗| 根河市| 东至县| 宁国市| 双江| 曲阳县| 垣曲县| 祁连县| 泸水县| 昌邑市| 山西省| 芦山县| 江安县| 郸城县| 贵溪市| 北票市| 吉隆县| 武平县| 浑源县| 从江县| 讷河市| 麟游县| 南漳县| 朔州市| 乾安县|