- Spring Boot 教程
- Spring Boot - 首页
- Spring Boot - 简介
- Spring Boot - 快速入门
- Spring Boot - 引导
- Spring Tool Suite
- Spring Boot - Tomcat 部署
- Spring Boot - 构建系统
- Spring Boot - 代码结构
- Spring Bean & 依赖注入
- Spring Boot - 运行器
- Spring Boot - 启动器
- Spring Boot - 应用程序属性
- Spring Boot - 配置
- Spring Boot - 注解
- Spring Boot - 日志
- 构建 RESTful Web 服务
- Spring Boot - 异常处理
- Spring Boot - 拦截器
- Spring Boot - Servlet 过滤器
- Spring Boot - Tomcat 端口号
- Spring Boot - Rest 模板
- Spring Boot - 文件处理
- Spring Boot - 服务组件
- Spring Boot - Thymeleaf
- 使用 RESTful Web 服务
- Spring Boot - CORS 支持
- Spring Boot - 国际化
- Spring Boot - 调度
- Spring Boot - 启用 HTTPS
- Spring Boot - Eureka 服务器
- 使用 Eureka 进行服务注册
- 网关代理服务器和路由
- Spring Cloud 配置服务器
- Spring Cloud 配置客户端
- Spring Boot - 执行器
- Spring Boot - Admin 服务器
- Spring Boot - Admin 客户端
- Spring Boot - 启用 Swagger2
- Spring Boot - 使用 SpringDoc OpenAPI
- Spring Boot - 创建 Docker 镜像
- 跟踪微服务日志
- Spring Boot - Flyway 数据库
- Spring Boot - 发送电子邮件
- Spring Boot - Hystrix
- Spring Boot - Web Socket
- Spring Boot - 批处理服务
- Spring Boot - Apache Kafka
- Spring Boot - Twilio
- Spring Boot - 单元测试用例
- Rest Controller 单元测试
- Spring Boot - 数据库处理
- 保护 Web 应用程序
- Spring Boot - 带 JWT 的 OAuth2
- Spring Boot - Google Cloud Platform
- Spring Boot - Google OAuth2 登录
- Spring Boot 资源
- Spring Boot - 快速指南
- Spring Boot - 有用资源
- Spring Boot - 讨论
Spring Boot - Flyway 数据库
Flyway 是一款版本控制应用程序,用于轻松可靠地在所有实例中发展您的数据库模式。要了解有关 Flyway 的更多信息,您可以使用以下链接:www.flywaydb.org
许多软件项目使用关系数据库。这需要处理数据库迁移,也常称为模式迁移。
在本章中,您将详细了解如何在 Spring Boot 应用程序中配置 Flyway 数据库。
配置 Flyway 数据库
首先,从 Spring Initializer 页面www.start.spring.io下载 Spring Boot 项目,并选择以下依赖项:
- Spring Web
- Flyway 迁移
- MySQL 驱动程序
- Spring Data JPA
Maven 用户可以在 pom.xml 文件中添加以下依赖项。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-mysql</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
Gradle 用户可以在 build.gradle 文件中添加以下依赖项。
compile('org.flywaydb:flyway-core') compile('org.flywaydb:flyway-mysql') compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.boot:spring-boot-starter-test') compile('mysql:mysql-connector-j')
在应用程序属性中,我们需要配置数据库属性以创建 DataSource,并且还需要在应用程序属性中配置 flyway 属性。
对于属性文件用户,请在 application.properties 文件中添加以下属性。
spring.application.name = flywayapp spring.datasource.url = jdbc:mysql://127.0.0.1:3306/details spring.datasource.username = root spring.datasource.password = Admin@123 spring.flyway.enabled=true spring.flyway.validate-on-migrate=true spring.flyway.baseline-on-migrate = true spring.flyway.baseline-version = 0
YAML 用户可以在 application.yml 文件中添加以下属性。
spring: application: name: flywayapp datasource: url: jdbc:mysql://127.0.0.1:3306/details password: Admin@123 username: root flyway: enabled: true validate-on-migrate: true baseline-on-migrate: true baseline-version: 0
现在,在src/main/resources/db/migration目录下创建一个 SQL 文件。将 SQL 文件命名为“V1__Initial.sql”,遵循命名约定 V<版本号>__<名称>.sql
CREATE TABLE USERS (ID INT AUTO_INCREMENT PRIMARY KEY, USERID VARCHAR(45)); INSERT INTO USERS (ID, USERID) VALUES (1, 'tutorialspoint.com');
主要的 Spring Boot 应用程序类文件代码如下所示:
FlywayappApplication.java
package com.tutorialspoint.flywayapp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class FlywayappApplication { public static void main(String[] args) { SpringApplication.run(FlywayappApplication.class, args); } }
完整的构建配置文件如下所示。
Maven – 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>3.3.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.tutorialspoint</groupId> <artifactId>flywayapp</artifactId> <version>0.0.1-SNAPSHOT</version> <name>flywayapp</name> <description>Demo project for Spring Boot</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>21</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-mysql</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Gradle – build.gradle
buildscript { ext { springBootVersion = '3.3.4' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' group = 'com.tutorialspoint' version = '0.0.1-SNAPSHOT' sourceCompatibility = 21 repositories { mavenCentral() } dependencies { compile('org.flywaydb:flyway-core') compile('org.flywaydb:flyway-mysql') compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-web') compile('mysql:mysql-connector-j') testCompile('org.springframework.boot:spring-boot-starter-test') }
编译和执行
您可以创建一个可执行的 JAR 文件,并使用以下 Maven 或 Gradle 命令运行 Spring Boot 应用程序:
对于 Maven,您可以使用此处显示的命令:
mvn clean install
“BUILD SUCCESS”后,您可以在 target 目录下找到 JAR 文件。
对于 Gradle,您可以使用此处显示的命令:
gradle clean build
“BUILD SUCCESSFUL”后,您可以在 build/libs 目录下找到 JAR 文件。
现在,使用以下命令运行 JAR 文件:
java –jar <JARFILE>
现在,Tomcat 在端口 8080 上启动,并且您可以在控制台窗口中看到 flyway 数据库日志,如下所示。
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ [32m :: Spring Boot :: [39m [2m (v3.3.4)[0;39m [2m2024-09-20T13:17:03.838+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mc.t.flywayapp.FlywayappApplication [0;39m [2m:[0;39m Starting FlywayappApplication using Java 21.0.3 with PID 9760 (E:\Dev\flywayapp\target\classes started by Tutorialspoint in E:\Dev\flywayapp) [2m2024-09-20T13:17:03.841+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mc.t.flywayapp.FlywayappApplication [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default" [2m2024-09-20T13:17:04.545+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Bootstrapping Spring Data JPA repositories in DEFAULT mode. [2m2024-09-20T13:17:04.568+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Finished Spring Data repository scanning in 14 ms. Found 0 JPA repository interfaces. [2m2024-09-20T13:17:05.144+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port 8080 (http) [2m2024-09-20T13:17:05.161+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mo.apache.catalina.core.StandardService [0;39m [2m:[0;39m Starting service [Tomcat] [2m2024-09-20T13:17:05.161+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mo.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/10.1.30] [2m2024-09-20T13:17:05.235+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext [2m2024-09-20T13:17:05.235+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 1339 ms [2m2024-09-20T13:17:05.481+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mcom.zaxxer.hikari.HikariDataSource [0;39m [2m:[0;39m HikariPool-1 - Starting... [2m2024-09-20T13:17:05.892+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mcom.zaxxer.hikari.pool.HikariPool [0;39m [2m:[0;39m HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@6c5ca0b6 [2m2024-09-20T13:17:05.894+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mcom.zaxxer.hikari.HikariDataSource [0;39m [2m:[0;39m HikariPool-1 - Start completed. [2m2024-09-20T13:17:05.952+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36morg.flywaydb.core.FlywayExecutor [0;39m [2m:[0;39m Database: jdbc:mysql://127.0.0.1:3306/details (MySQL 8.0) [2m2024-09-20T13:17:05.993+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mo.f.c.i.s.JdbcTableSchemaHistory [0;39m [2m:[0;39m Schema history table `details`.`flyway_schema_history` does not exist yet [2m2024-09-20T13:17:05.996+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mo.f.core.internal.command.DbValidate [0;39m [2m:[0;39m Successfully validated 1 migration (execution time 00:00.018s) [2m2024-09-20T13:17:06.026+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mo.f.c.i.s.JdbcTableSchemaHistory [0;39m [2m:[0;39m Creating Schema History table `details`.`flyway_schema_history` with baseline ... [2m2024-09-20T13:17:06.282+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mo.f.core.internal.command.DbBaseline [0;39m [2m:[0;39m Successfully baselined schema with version: 0 [2m2024-09-20T13:17:06.321+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mo.f.core.internal.command.DbMigrate [0;39m [2m:[0;39m Current version of schema `details`: 0 [2m2024-09-20T13:17:06.329+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mo.f.core.internal.command.DbMigrate [0;39m [2m:[0;39m Migrating schema `details` to version "1 - Initial" [2m2024-09-20T13:17:06.455+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mo.f.core.internal.command.DbMigrate [0;39m [2m:[0;39m Successfully applied 1 migration to schema `details`, now at version v1 (execution time 00:00.073s) [2m2024-09-20T13:17:06.586+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mo.hibernate.jpa.internal.util.LogHelper [0;39m [2m:[0;39m HHH000204: Processing PersistenceUnitInfo [name: default] [2m2024-09-20T13:17:06.655+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36morg.hibernate.Version [0;39m [2m:[0;39m HHH000412: Hibernate ORM core version 6.5.3.Final [2m2024-09-20T13:17:06.690+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mo.h.c.internal.RegionFactoryInitiator [0;39m [2m:[0;39m HHH000026: Second-level cache disabled [2m2024-09-20T13:17:07.025+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mo.s.o.j.p.SpringPersistenceUnitInfo [0;39m [2m:[0;39m No LoadTimeWeaver setup: ignoring JPA class transformer [2m2024-09-20T13:17:07.446+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mo.h.e.t.j.p.i.JtaPlatformInitiator [0;39m [2m:[0;39m HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration) [2m2024-09-20T13:17:07.452+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Initialized JPA EntityManagerFactory for persistence unit 'default' [2m2024-09-20T13:17:07.514+05:30[0;39m [33m WARN[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mJpaBaseConfiguration$JpaWebConfiguration[0;39m [2m:[0;39m spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning [2m2024-09-20T13:17:07.978+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port 8080 (http) with context path '/' [2m2024-09-20T13:17:07.988+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp ] [ main][0;39m [2m[0;39m[36mc.t.flywayapp.FlywayappApplication [0;39m [2m:[0;39m Started FlywayappApplication in 4.595 seconds (process running for 5.424)
您现在可以转到数据库并检查在details模式中创建的Users表,其详细信息与 V1__Initial.sql 中提供的脚本一致。
您还可以跟踪 flyway 元数据表flyway_schema_history中的迁移,如下所示