Flyway自学之路-04(springboot结合Flyway)

本文介绍如何在SpringBoot项目中使用Flyway进行数据库迁移,包括添加依赖、配置属性、添加SQL脚本及启动项目的过程。通过Flyway,可以自动化地管理数据库版本,确保数据库结构与应用代码保持同步。

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

1.新建项目

2.添加mysql数据库依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

pom.xml如下所示

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springboot</groupId>
    <artifactId>my_flyway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>my_flyway</name>
    <description>Demo project for my_flyway</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

3.配置application.properties

## 设定 db source 属性
spring.datasource.url=jdbc:mysql://localhost:3306/flyway?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456

## 设定 flyway 属性
#spring.flyway.cleanDisabled = false
# flyway 的 clean 命令会删除指定 schema 下的所有 table, 杀伤力太大了, 应该禁掉.
#spring.flyway.enabled = true
# 启用或禁用 flyway
spring.flyway.locations =classpath:db/migration
# 设定 SQL 脚本的目录,多个路径使用逗号分隔, 比如取值为 classpath:db/migration,filesystem:/sql-migrations
#spring.flyway.baselineOnMigrate=true
# 如果指定 schema 包含了其他表,但没有 flyway schema history 表的话, 在执行 flyway migrate 命令之前, 必须先执行 flyway baseline 命令.
# 设置 spring.flyway.baseline-on-migrate 为 true 后, flyway 将在需要 baseline 的时候, 自动执行一次 baseline.
#spring.flyway.baselineVersion=1
# 指定 baseline 的版本号,缺省值为 1, 低于该版本号的 SQL 文件, migrate 的时候被忽略.
#spring.flyway.encoding=
# Encoding of SQL migrations (default: UTF-8)
#spring.flyway.table=flyway_schema_history_myapp
# 设定 flyway 的 metadata 表名, 缺省为 flyway_schema_history
spring.flyway.outOfOrder=true
# 开发环境最好开启 outOfOrder, 生产环境关闭 outOfOrder .
#spring.flyway.schemas=
# 需要 flyway 管控的 schema list, 缺省的话, 使用的时 dbsource.connection直连上的那个 schema, 可以指定多个schema, 但仅会在第一个schema下建立 metadata 表, 也仅在第一个schema应用migration sql 脚本. 但flyway Clean 命令会依次在这些schema下都执行一遍.

4.添加sql文件

/*
 Navicat Premium Data Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 80019
 Source Host           : localhost:3306
 Source Schema         : test

 Target Server Type    : MySQL
 Target Server Version : 80019
 File Encoding         : 65001

 Date: 24/05/2020 09:35:26
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for person
-- ----------------------------
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person`  (
  `id` int(0) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
  `age` int(0) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin COMMENT = '人员信息表' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of person
-- ----------------------------
INSERT INTO `person` VALUES (1, 'zhangjie', 32);
INSERT INTO `person` VALUES (2, 'zhangsan', 27);

SET FOREIGN_KEY_CHECKS = 1;

5.启动项目

打印日志如下所示:

D:\software\java\jdk_1.8\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=63821 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:D:\software\idea\IntelliJ IDEA 2018.3.1\lib\idea_rt.jar=63822:D:\software\idea\IntelliJ IDEA 2018.3.1\bin" -Dfile.encoding=UTF-8 -classpath D:\software\java\jdk_1.8\jre\lib\charsets.jar;D:\software\java\jdk_1.8\jre\lib\deploy.jar;D:\software\java\jdk_1.8\jre\lib\ext\access-bridge-64.jar;D:\software\java\jdk_1.8\jre\lib\ext\cldrdata.jar;D:\software\java\jdk_1.8\jre\lib\ext\dnsns.jar;D:\software\java\jdk_1.8\jre\lib\ext\jaccess.jar;D:\software\java\jdk_1.8\jre\lib\ext\jfxrt.jar;D:\software\java\jdk_1.8\jre\lib\ext\localedata.jar;D:\software\java\jdk_1.8\jre\lib\ext\nashorn.jar;D:\software\java\jdk_1.8\jre\lib\ext\sunec.jar;D:\software\java\jdk_1.8\jre\lib\ext\sunjce_provider.jar;D:\software\java\jdk_1.8\jre\lib\ext\sunmscapi.jar;D:\software\java\jdk_1.8\jre\lib\ext\sunpkcs11.jar;D:\software\java\jdk_1.8\jre\lib\ext\zipfs.jar;D:\software\java\jdk_1.8\jre\lib\javaws.jar;D:\software\java\jdk_1.8\jre\lib\jce.jar;D:\software\java\jdk_1.8\jre\lib\jfr.jar;D:\software\java\jdk_1.8\jre\lib\jfxswt.jar;D:\software\java\jdk_1.8\jre\lib\jsse.jar;D:\software\java\jdk_1.8\jre\lib\management-agent.jar;D:\software\java\jdk_1.8\jre\lib\plugin.jar;D:\software\java\jdk_1.8\jre\lib\resources.jar;D:\software\java\jdk_1.8\jre\lib\rt.jar;D:\搜狗高速下载\flyway-springboot-master\flyway-springboot-master\my_flyway\target\classes;D:\document\eclipse_workspace\local_repository\local_repository\org\springframework\boot\spring-boot-starter\2.3.0.RELEASE\spring-boot-starter-2.3.0.RELEASE.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\springframework\boot\spring-boot\2.3.0.RELEASE\spring-boot-2.3.0.RELEASE.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\springframework\spring-context\5.2.6.RELEASE\spring-context-5.2.6.RELEASE.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\springframework\spring-aop\5.2.6.RELEASE\spring-aop-5.2.6.RELEASE.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\springframework\spring-expression\5.2.6.RELEASE\spring-expression-5.2.6.RELEASE.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\springframework\boot\spring-boot-autoconfigure\2.3.0.RELEASE\spring-boot-autoconfigure-2.3.0.RELEASE.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\springframework\boot\spring-boot-starter-logging\2.3.0.RELEASE\spring-boot-starter-logging-2.3.0.RELEASE.jar;D:\document\eclipse_workspace\local_repository\local_repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\document\eclipse_workspace\local_repository\local_repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.2\log4j-to-slf4j-2.13.2.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\apache\logging\log4j\log4j-api\2.13.2\log4j-api-2.13.2.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\document\eclipse_workspace\local_repository\local_repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\springframework\spring-core\5.2.6.RELEASE\spring-core-5.2.6.RELEASE.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\springframework\spring-jcl\5.2.6.RELEASE\spring-jcl-5.2.6.RELEASE.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\flywaydb\flyway-core\6.4.1\flyway-core-6.4.1.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\springframework\boot\spring-boot-starter-jdbc\2.3.0.RELEASE\spring-boot-starter-jdbc-2.3.0.RELEASE.jar;D:\document\eclipse_workspace\local_repository\local_repository\com\zaxxer\HikariCP\3.4.5\HikariCP-3.4.5.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\springframework\spring-jdbc\5.2.6.RELEASE\spring-jdbc-5.2.6.RELEASE.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\springframework\spring-beans\5.2.6.RELEASE\spring-beans-5.2.6.RELEASE.jar;D:\document\eclipse_workspace\local_repository\local_repository\org\springframework\spring-tx\5.2.6.RELEASE\spring-tx-5.2.6.RELEASE.jar;D:\document\eclipse_workspace\local_repository\local_repository\mysql\mysql-connector-java\8.0.20\mysql-connector-java-8.0.20.jar com.springboot.my_flyway.MyFlywayApplication

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.0.RELEASE)

2020-05-24 12:15:55.447  INFO 19064 --- [           main] c.s.my_flyway.MyFlywayApplication        : Starting MyFlywayApplication on LAPTOP-SRJM9GEI with PID 19064 (D:\搜狗高速下载\flyway-springboot-master\flyway-springboot-master\my_flyway\target\classes started by 张琴 in D:\搜狗高速下载\flyway-springboot-master\flyway-springboot-master\my_flyway)
2020-05-24 12:15:55.449  INFO 19064 --- [           main] c.s.my_flyway.MyFlywayApplication        : No active profile set, falling back to default profiles: default
2020-05-24 12:15:55.968  INFO 19064 --- [           main] o.f.c.internal.license.VersionPrinter    : Flyway Community Edition 6.4.1 by Redgate
2020-05-24 12:15:55.975  INFO 19064 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-05-24 12:15:56.712  INFO 19064 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-05-24 12:15:56.736  INFO 19064 --- [           main] o.f.c.internal.database.DatabaseFactory  : Database: jdbc:mysql://localhost:3306/flyway (MySQL 8.0)
2020-05-24 12:15:56.768  INFO 19064 --- [           main] o.f.core.internal.command.DbValidate     : Successfully validated 1 migration (execution time 00:00.012s)
2020-05-24 12:15:56.783  INFO 19064 --- [           main] o.f.c.i.s.JdbcTableSchemaHistory         : Creating Schema History table `flyway`.`flyway_schema_history` ...
2020-05-24 12:15:56.863  INFO 19064 --- [           main] o.f.core.internal.command.DbMigrate      : Current version of schema `flyway`: << Empty Schema >>
2020-05-24 12:15:56.863  WARN 19064 --- [           main] o.f.core.internal.command.DbMigrate      : outOfOrder mode is active. Migration of schema `flyway` may not be reproducible.
2020-05-24 12:15:56.874  INFO 19064 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema `flyway` to version 20200524.09.35 - V1.0 person
2020-05-24 12:15:56.890  WARN 19064 --- [           main] o.f.c.i.s.DefaultSqlScriptExecutor       : DB: Unknown table 'flyway.person' (SQL State: 42S02 - Error Code: 1051)
2020-05-24 12:15:56.925  WARN 19064 --- [           main] o.f.c.i.s.DefaultSqlScriptExecutor       : DB: Integer display width is deprecated and will be removed in a future release. (SQL State: HY000 - Error Code: 1681)
2020-05-24 12:15:56.925  WARN 19064 --- [           main] o.f.c.i.s.DefaultSqlScriptExecutor       : DB: Integer display width is deprecated and will be removed in a future release. (SQL State: HY000 - Error Code: 1681)
2020-05-24 12:15:56.954  INFO 19064 --- [           main] o.f.core.internal.command.DbMigrate      : Successfully applied 1 migration to schema `flyway` (execution time 00:00.097s)
2020-05-24 12:15:57.018  INFO 19064 --- [           main] c.s.my_flyway.MyFlywayApplication        : Started MyFlywayApplication in 1.862 seconds (JVM running for 3.103)
2020-05-24 12:15:57.023  INFO 19064 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-05-24 12:15:57.027  INFO 19064 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Process finished with exit code 0

查看数据多了2张表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值