Flyway+Springboot整合使用

本文详细介绍了在非SpringBoot环境下如何配置Flyway进行数据库迁移,包括项目依赖设置、YAML配置、脚本编写及项目启动流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

flyway在非springboot中的配置方法可参考flyway入门使用教程

1.项目依赖

项目中需要的依赖

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.lx</groupId>
  <artifactId>flyway</artifactId>
  <version>1.0-SNAPSHOT</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath/>
  </parent>

  <dependencies>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--与springboot整合时需要此包,如果是通过插件的方式则不需要-->
    <dependency>
      <groupId>org.flywaydb</groupId>
      <artifactId>flyway-core</artifactId>
      <version>6.2.4</version>
    </dependency>
    <!--配置flywaydb时,需要引入org.springframework:spring-jdbc包,否则会报找不到驱动bug-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

2.yml配置

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
    username: root
    password: root

  flyway:
    enabled: true
    encoding: UTF-8
    locations: ['classpath:db/migration']
    url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
    user: root
    password: root

更多配置信息可以参考org.springframework.boot.autoconfigure.flyway.FlywayProperties基本和flyway入门使用教程中的配置参数一致,只是不需要在写driver驱动,而是根据链接自动识别,通过springboot引入的spring-jdbc引用数据源

3.编写脚本

参照flyway入门使用教程编写一个数据迁移脚本V1.0__Create_user_table.sql

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `create_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP,
 `update_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP,
 `valid` tinyint(2) NOT NULL DEFAULT 0 COMMENT '是否有效 0.有效 1.无效',
 `delete_time` datetime(0) NULL DEFAULT NULL,
 `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '账号名称',
 `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '加密后密码',
 `salt` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '密码盐值',
 `admin_role_ids` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '角色id(多个使用,号隔开)',
 PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '用户表' ROW_FORMAT = Compact;

4. 启动项目

到此处为止有关springboot和flyway的配置基本已经配置完毕,直接启动项目,查看控制台,可以看到flyway配置成功,可以正常执行脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值