Flyway在springboot中的应用

5 篇文章 0 订阅
3 篇文章 0 订阅

flyway
参考:https://blog.waterstrong.me/flyway-in-practice/
参考:https://blog.csdn.net/Cug_wangww/article/details/81150417?utm_source=blogkpcl0
参考:https://www.cnblogs.com/dreamingodd/p/6125435.html

Flyway
定义:Flyway是一款开源的数据库版本管理工具,Flyway可以独立于应用实现管理并跟踪数据库的变更,Flyway根据自己的约定,不需要复杂的配置就可以实现数据的Migrate。Migrations可以写成SQL脚本,也可以写在Java代码中,Flyway还支持Spring Boot。

1、在pom.xml文件中导入依赖,如下:

<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
    <version>5.0.7</version>
</dependency>

<build>
	<plugins>
	    <plugin>
	        <groupId>org.flywaydb</groupId>
	        <artifactId>flyway-maven-plugin</artifactId>
	        <version>5.0.7</version>
	    </plugin>
	</plugins>
 </build>

2、在src/resources/application.yml文件中添加flyway配置信息

flyway:
  baselineOnMigrate: true
  enabled: true
  baseline-version: 2018.10.25
  sql-migration-prefix: V
  sql-migration-suffix: .sql
  url: jdbc:mysql://127.0.0.1:3386/ship_test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
  user: root
  password: 970131
  validate-on-migrate: true
  locations: classpath:migrate
  check-location: false

jpa:
  hibernate:
    ddl-auto: none

注:

第一列第二列
baselineOnMigrate: true# 是否执行不含元数据表的非空模式迁移
enabled: true# 启用flyway
baseline-version: 2018.10.25# 版本号
sql-migration-prefix: V# SQL迁移的文件名前缀
sql-migration-suffix: .sql# SQL迁移的文件名后缀
user: root# 数据库用户名
password: 970131# 数据库密码
validate-on-migrate: true# 校验
locations: classpath:migrate# SQL迁移的文件所在位置
check-location: false# 检查迁移脚本位置是否存在。
url: value# 要迁移的数据库的J.BDBURL。如果未设置,则使用主配置数据源。

vlaue=
jdbc:mysql://127.0.0.1:3386/
ship_test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull )

3、 在src/resources/migrate目录下放置数据库脚本文件:V2018.10.25__ship_dev.sql文件;
内容为:创建数据库的语句;

在这里插入图片描述

注:
sql脚本命名规范:
prefix version,数字间用点【.】隔开 双下划线 文件名,字符间用下划线连接 suffix
V 2018.10.25 __ ship_dev .sql

4、在本地创建新的数据库ship_test:
create database ship_test;

5、重新编译程序、启动程序;
spring会自动创建flyway的bean用来处理脚本;

在这里插入图片描述
注:flyway_shema_history : 用于版本控制

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值