前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot Flyway数据库

Spring Boot Flyway数据库

作者头像
黑洞代码
发布2021-10-14 11:11:03
1.3K0
发布2021-10-14 11:11:03
举报

Flyway是一个版本控制应用程序,可以在所有实例中轻松可靠地演变数据库模式。要了解有关Flyway的更多信息,可以使用链接 - www.flywaydb.org[1]

许多软件项目使用关系数据库。这需要处理数据库迁移,通常也称为模式迁移。

在本章中,将详细了解如何在Spring Boot应用程序中配置Flyway数据库。

配置Flyway数据库

首先,从Spring Initializer 页面 www.start.spring.io[2] 下载Spring Boot项目并选择以下依赖项 -

1.Spring Boot Starter Web2.Flyway3.MySQL4.JDBC

Maven用户可以在pom.xml 文件中添加以下依赖项。

代码语言:javascript
复制
<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>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</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>
</dependency>

在应用程序属性中,需要配置数据库属性以创建DataSource,还要在应用程序属性中配置的flyway属性。

对于属性文件用户,请在application.properties 文件中添加以下属性。

代码语言:javascript
复制
spring.application.name = flywayapp  

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/USERSERVICE?autoreconnect=true
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.testOnBorrow = true
spring.datasource.testWhileIdle = true
spring.datasource.timeBetweenEvictionRunsMillis = 60000
spring.datasource.minEvictableIdleTimeMillis = 30000
spring.datasource.validationQuery = SELECT 1
spring.datasource.max-active = 15
spring.datasource.max-idle = 10
spring.datasource.max-wait = 8000

flyway.url = jdbc:mysql://localhost:3306/mysql
flyway.schemas = USERSERVICE
flyway.user = root
flyway.password = root

现在,在src/main/resources/db/migration 目录下创建一个SQL文件。将SQL文件命名为V1__Initial.sql

代码语言:javascript
复制
CREATE TABLE USERS (ID INT AUTO_INCREMENT PRIMARY KEY, USERID VARCHAR(45));
INSERT INTO USERS (ID, USERID) VALUES (1, 'yiibai.com');

主 Spring Boot应用程序类文件代码如下 -

代码语言:javascript
复制
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);
   }
}

使用以下命令运行JAR文件 -

代码语言:javascript
复制
java –jar <JARFILE>

现在,Tomcat在端口8080上启动,在控制台窗口中,可以看到如此处所示的flyway数据库日志。

代码语言:javascript
复制
pringframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-08 21:07:58.525  INFO 16088 --- [           main] o.f.core.internal.util.VersionPrinter    : Flyway 3.2.1 by Boxfuse
Mon Oct 08 21:07:58 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Mon Oct 08 21:07:58 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
2018-10-08 21:07:58.825  INFO 16088 --- [           main] o.f.c.i.dbsupport.DbSupportFactory       : Database: jdbc:mysql://localhost:3306/mysql (MySQL 5.7)
2018-10-08 21:07:58.860  INFO 16088 --- [           main] o.f.core.internal.command.DbValidate     : Validated 1 migration (execution time 00:00.014s)
2018-10-08 21:07:59.150  INFO 16088 --- [           main] o.f.c.i.metadatatable.MetaDataTableImpl  : Creating Metadata table: `testdb`.`schema_version`
2018-10-08 21:07:59.987  INFO 16088 --- [           main] o.f.core.internal.command.DbMigrate      : Current version of schema `testdb`: << Empty Schema >>
2018-10-08 21:07:59.988  INFO 16088 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema `testdb` to version 1 - Initial
2018-10-08 21:08:00.092  INFO 16088 --- [           main] o.f.core.internal.command.DbMigrate      : Successfully applied 1 migration to schema `testdb` (execution time 00:00.947s).
2018-10-08 21:08:00.339  INFO 16088 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-10-08 21:08:00.515  INFO 16088 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-10-08 21:08:00.526  INFO 16088 --- [           main] c.yiibai.flywayapp.FlywayappApplication  : Started FlywayappApplication in 6.384 seconds (JVM running for 6.903)
2018-10-08 21:08:20.802  INFO 16088 --- [       Thread-3] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@14514713: startup date [Mon Oct 08 21:07:54 CST 2018]; root of context hierarchy
2018-10-08 21:08:20.810  INFO 16088 --- [       Thread-3] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

现在连接到数据库并执行选择查询。结果如下所示 -

References

[1] www.flywaydb.org: http://www.flywaydb.org [2] www.start.spring.io: http://www.start.spring.io

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-09-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 落叶飞翔的蜗牛 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 配置Flyway数据库
    • References
    相关产品与服务
    数据库
    云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档