1.简介
Flyway提供了集成SpringBoot项目的方式,将flyway集成进SpringBoot后,flyway使用boot项目中定义的数据源,并在每次项目启动时,都会去执行(flyway migrate)。flyway中一些配置也可以通过boot项目的application.yml文件进行配置。
2.案例演示
2.1项目搭建
①pom文件
<dependency><groupId>org.flywaydb</groupId><artifactId>flyway-core</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>
②在resource目录下创建db.migrate目录
①②两步可以在创建工程时选择Flyway Migrate来替代,选择后项目会自动生成。
③配置application.yml配置文件
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/flyway?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8username: rootpassword: 888888
④在resource/db/migrate中编写一个sql脚本**V1__init.sql**
create table person(id varchar(10) not null,name varchar(100));

⑤运行程序,观察数据库和日志
flyway migrate会被默认执行
数据库被创建 person 表
2.2IDEA中配置Flyway 脚本创建插件
在编写Flyway脚本时,偶尔会忘记具体命名的格式,IDEA中的Flyway插件的作用就是为你规范脚本命名格式的
①搜索插件Flyway并安装
②在/resource/db/migration目录下创建一个Flyway文件
③点击后输入脚本名称
NewFlywayMigration
Entermigrationdescription
hello
Cancel
OK
 <br />**④查看创建出来的脚本**<br />
默认的版本号为时间戳,可以进行自行的修改。具体修改方式自行百度即可
2.3Flyway配置项
flyway一些配置项可以在application.yml配置,下面挑几个来说一下配置
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/flyway?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8username: rootpassword: 888888flyway:#若原数据库非空库,需要将此项设置为true 默认为 falsebaseline-on-migrate: true#指定脚本文件的位置 默认 db/migration 可以指定多个locations: classpath:db/migration,classpath:mysql/migration#数据库,若不指定,使用spring.datasource指定的数据库url: jdbc:mysql://127.0.0.1:3306/flyway2?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8#数据库用户,不指定使用 spring.datasource下的user: root#数据库密码 不指定使用 spring.datasource下的password: 888888

