SpringBoot结合Flyway实现数据库版本管理及配置文件说明

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/achenyuan/article/details/83417432

SpringBoot结合Flyway实现数据库版本管理及配置文件说明

前言

Flyway是个很好的数据库版本管理工具,根据版本号顺序执行sql文件,维护一个统一的数据库,适用于多人协作开发。
可以参考下面文章,说明很详细
快速掌握和使用Flyway
Flyway官网
我关注的是实战,以及Springboot如何配置Flyway,同时Springboot都支持Flyway哪些默认配置。

实例

maven引入Flyway-core.jar

<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
    <version>5.2.1</version>
</dependency>

Gradle引入Flyway-core.jar

compile "org.flywaydb:flyway-core:5.2.1"

上面代码可以从官网API(Java/Android)页签上找到

flyway迁移默认路径是resources/db/migration
默认记录更新版本的表名是flyway_schema_history

springboot的配置文件里配置application.yml即可:

#数据库版本管理和迁移
  flyway:
    baseline-on-migrate: true
    table: schame_version

如果我们没有配置url/user/password那么springboot会使用主要的datasource配置,换句话说,如果我们配置了mysql数据库,而flyway也是管理这个数据库,那么url等参数可以不配置。

至此Flyway配置完成,剩余就是写sql语句,我相信不用我再继续说了。

其它配置

从官网上我们可以得到Flyway的配置文件Config Files
同时Flyway是支持Springboot的,所以SpringBoot同样对Flyway有默认配置,可以看SpringBoot对Flyway的自动化配置,默认配置如下:

@ConfigurationProperties(prefix = "flyway", ignoreUnknownFields = true)
public class FlywayProperties {

	/**
	 * Locations of migrations scripts. Can contain the special "{vendor}" placeholder to
	 * use vendor-specific locations.
	 */
	private List<String> locations = new ArrayList<String>(
			Collections.singletonList("db/migration"));

	/**
	 * Check that migration scripts location exists.
	 */
	private boolean checkLocation = false;

	/**
	 * Enable flyway.
	 */
	private boolean enabled = true;

	/**
	 * Login user of the database to migrate.
	 */
	private String user;

	/**
	 * Login password of the database to migrate.
	 */
	private String password;

	/**
	 * JDBC url of the database to migrate. If not set, the primary configured data source
	 * is used.
	 */
	private String url;

	/**
	 * SQL statements to execute to initialize a connection immediately after obtaining
	 * it.
	 */
	private List<String> initSqls = new ArrayList<String>();

	...getter/setter
}

上面是springboot自动化配置Flyway的属性。我想说明3点

  1. springboot支持flyway的自动配置,我们可以从上面网址上找到flyway配置。这样不至于我们不知道怎么配置flyway
  2. 上面没出现的flyway属性我们一样可以写。FlywayProperties.java文件的意思是springboot提供的配置优先于flyway本身的配置,当然,我们可以埴写flyway其它的配置文件,比如FlywayProperties.java里没有sqlMigrationPrefix属性,但它在Flyway配置里有,那我们依然可以spring.flyway.sqlMigrationPrefix: "V"这样配置。
  3. springboot配置名怎么写?spring.flyway.sqlMigrationPrefix: "V"spring.flyway.sql-migration-prefix: "V"是等价的。

授人以鱼不如授人以渔,我写的是从哪里可以找到配置,而不是配置本身。
官方Config Files
SpringBoot对Flyway的自动化配置

拓展

访问springboot其它项目的配置,看到很多项目的配置,当然它不是很全,比较redis就没在里面。访问web/ServerProperties.java

@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties
		implements EmbeddedServletContainerCustomizer, EnvironmentAware, Ordered {

	/**
	 * Server HTTP port.
	 */
	private Integer port;

	/**
	 * Network address to which the server should bind to.
	 */
	private InetAddress address;

	....


}

是不是很熟悉,它就是我们常要使用的application.yml如下配置

server:
  port:
  address: localhost

如果找不到springboot如何配置基本个项目,2种解决办法

  • 试着从这里找试试,springboot其它项目的配置
  • 从官方的配置文件上下手,找到配置文件,在application.yml配置属性,按住ctrl,鼠标移到配置上,看是不是出现手型。

猜你喜欢

转载自blog.csdn.net/achenyuan/article/details/83417432