前言
一般在我们开发项目的时候经常会更新数据库表的字段,如果同事 a 添加了表字段,没有及时给同事 b sql 脚本,
可能同事 b 的代码运行就会报错,而且随着时间的推移,sql 脚本越来越多,项目上线的时候整理起来就很费时间
所以就有大佬级别的人物创造了 Flyway 这个数据库版本管理工具。
源码
GitHub地址:https://github.com/intomylife/SpringBoot
环境
- JDK 1.8.0 +
- Maven 3.0 +
- MySQL 5.6.17
- SpringBoot 2.0.3
开发工具
- IntelliJ IDEA
正文
commons 工程 – POM 文件
4.0.0
com.zwc
springboot-flyway-commons
0.0.1-SNAPSHOT
springboot-flyway-commons
公用工程
jar
UTF-8
1.8
Cairo-SR3
mysql
mysql-connector-java
org.flywaydb
flyway-core
org.springframework.boot
spring-boot-starter-jdbc
io.spring.platform
platform-bom
${platform-bom.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
- 配置一些共用依赖,其中包括 flyway-core 来整合 Flyway
commons 工程 – 项目结构

service 工程
service 工程是一个父工程,里面可能会包含 基础模块,用户模块,订单模块等等… 每个模块中又会分为 core 和 api
service 工程 – base-service-core – application.properties
# 端口 server.port=8082 # 数据源 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/base_db_test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=PRC&useSSL=false spring.datasource.username=root spring.datasource.password= # 打印 sql 日志 logging.level.com.zwc.base.mapper=debug # flyway 注:可以完全不用配置 sql 脚本的位置,默认为 classpath:db/migration。可手动指定 spring.flyway.locations=classpath:db/zwc 指定数据源,如果没有指定的话,将使用配置的主数据源 spring.flyway.url=jdbc:mysql://127.0.0.1:3306/base_db_flyway?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=PRC&useSSL=false Flyway 管理的 Schema 列表,区分大小写。默认连接对应的默认 Schema 如果这里明确指定了库名,那么在 spring.flyway.url 连接中指定的库名将无效 spring.flyway.schemas=base_db_flyway 用户名 spring.flyway.user=root 密码 spring.flyway.password= 开启,默认开启 spring.flyway.enabled=true
- 此次整合的整个核心部分就在这个配置文件里
- 如果 Flyway 没有任何配置,默认配置为数据源的配置信息
- Flyway 默认信息:
a) sql 脚本的位置,默认为 classpath:db/migration。可手动指定
b) 数据源的连接地址,操作的库,用户名以及密码
- Flyway 配置信息:
a) 如果配置了 spring.flyway.schemas 明确指定了库名,那么在 spring.flyway.url 连接中指定的库名将无效
b) spring.flyway.schemas > spring.flyway.url > spring.datasource.url
service 工程 – base-service-core – resources -> db.zwc
注:此处为 sql 脚本位置( application.properties -> spring.flyway.locations)
V0.1.0__init_table.sql
-- 创建表 CREATE TABLE `springboot_flyway` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID', `type` varchar(2) DEFAULT NULL COMMENT '生活用品类别:1. 家电类 2. 厨具', `name` varchar(50) DEFAULT NULL COMMENT '生活用品名称', `description` varchar(200) DEFAULT NULL COMMENT '生活用品描述', PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='springboot整合flyway测试表';
V0.1.1__init_data.sql
-- 初始化数据 INSERT INTO springboot_flyway ( type , name , description ) VALUES ('1','电饭煲','用来蒸饭'),('1','电热壶','用来烧水'),('1','空调','用来制冷或制热'), ('2','菜刀','用来切菜'),('2','刨子','用来剥皮'),('2','打蛋器','用来搅拌鸡蛋');
注意命名规范,参考:https://flywaydb.org/documentation/migrations
如果没有翻译全文,搜索关键字 Naming;如果翻译了全文,搜索关键字 命名
启用项目,调用接口
注:根据你配置的数据源信息或 Flyway 信息创建好对应的数据库
1. 项目启动成功后,到对应的数据库中刷新,即可看到 Flyway 创建的表
2. 第一次启动的时候,除了主动写的 sql 脚本会被执行以及创建表以外, Flyway 还会自动创建一张历史版本记录的表
service 工程 – 项目结构
- 在 service 总工程中创建了一个 base-service 的基础模块
- 每一个模块中都包含 api 和 core

- api:主要包含接口,常量以及实体类的扩展类

- core:带有启动类的工程,此模块的核心代码都在里面
把多工程项目使用 IntelliJ IDEA 打开
- 把项目从 GitHub 中下载到你的本地
- 打开 IntelliJ IDEA
- 点击 File -> Open
- 打开你下载到本地的项目目录
- springboot-flyway -> springboot-flyway-service(选择打开此工程)
- 打开 service 工程后
- 再次点击 File -> Project Structrue
- 选择 Modules,点击 ‘+’ 符号
- 点击 Import Module
- 还是打开你下载到本地的项目目录
- springboot-flyway -> springboot-flyway-commons -> pom.xml
- 点击 OK
- 点击 Next,Finish
- 点击 Apply,OK
结语
到此 SpringBoot 整合 Flyway 就结束了,很简单,主要在配置,多多尝试,一定会成功的!
希望能够帮助到你
over
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/218658.html原文链接:https://javaforall.net
