FlyWay整合Springbooot 实战步骤


FlyWay整合Springbooot 实战教程

##政治老师:是什么?为什么?怎么做?

源码:
GitHub地址:https://github.com/intomylife/SpringBoot

环境
JDK 1.8.0 +
Maven 3.0 +
MySQL 5.6以上
SpringBoot 2.0以上(1.5可以和MySql5.5搭配)

是什么:

Flyway的简述:
在这里插入图片描述

Flyway的运作方式:
在这里插入图片描述

  1. 默认创建一个flyway_schema_history表 无则创建有则更新;

  2. 在项目启动的时候,会自动检索Version,根项目的sql文件(schema)进行对比,如果是最新版本则不执行迁移,不是最新版本则更新;ps:这bendi(文件夹)根db(文件夹)代表着2个数据库,小编自定义的路径取代了作者本身默认的路径
    在这里插入图片描述
    在这里插入图片描述

  3. 若为最新:日志描述当前版本为4.0,schema“mall”是最新的。无需迁移在这里插入图片描述

  4. 无更新:在这里插入图片描述

怎么做?

1.新建srpingboot项目 相信大伙都会了 只需勾选这个
在这里插入图片描述
2.maven依赖参考:

	 <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-core</artifactId>
        <version>5.2.1</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

3.FlyWay注册类:
@Configuration public class FlyWayConfigiton { @Bean(name = "Flyway") public Flyway Flyway(){ Flyway flyway = Flyway.configure().dataSource( "jdbc:mysql://localhost:3306/flyway?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC", "root", "123").locations("a").load();//locations就是你的sql文件目录作者这里自定义 flyway.migrate(); return flyway; } }
在这里插入图片描述
4.application.yml:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/bendi?createDatabaseIfNotExist=true&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: 123

5.sql文件(迁移文件):
V1.0_bendi.sql:
DROP TABLE IF EXISTS user;

CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT ‘主键ID’,
name VARCHAR(30) NULL DEFAULT NULL COMMENT ‘姓名’,
age INT(11) NULL DEFAULT NULL COMMENT ‘年龄’,
email VARCHAR(50) NULL DEFAULT NULL COMMENT ‘邮箱’,
PRIMARY KEY (id)
);
重点关注命名规则:V+版本号(默认1.0)+"_"+xxxxx.sql 如:V1.0_bendi.sql V2.0_bendi.sql FlyWay会根据flyway_schema_history的script&version进行校验 对比版本号 所以一定得按规则行事

6.启动项目就自动校验执行:
在这里插入图片描述
7.加入到已有的项目也是一样加FlyWay的dependency 加入配置类 如果多数据源则加多个配置类
在这里插入图片描述

可能遇到的问题:

1.Unable to resolve location classpath
在这里插入图片描述

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值