数据库版本管理工具flyway

引入flyway_core  jar包

java 代码实现

 

public class FlywayMigration {

  @Resource
  private DataSource dataSource;

public void setDataSource(DataSource dataSource) {
  this.dataSource = dataSource;
}

public void migration() {
  Flyway flyway = new Flyway();
  flyway.setDataSource(dataSource);

  flyway.setInitOnMigrate(true);

  flyway.setTable("schemas_version");// 设置存放flyway metadata数据的表名

  // 设置flyway扫描sql升级脚本,java升级脚本的目录路径或包路径
  flyway.setLocations("database", "com.navinsight.holoviewer.back.transverse.util");

  flyway.setEncoding("UTF-8");// 设置sql脚本文件的编码

  flyway.setValidateOnMigrate(true); // 设置执行migrate操作之前的validation行为

  try {
  flyway.migrate();
  } catch (Exception e) {
  flyway.repair();
  e.printStackTrace();
  }

  }

}

 

sql文件命名规范:V+版本号+两个_+描述+.sql

 

在Spring 中根据上面实现的类来定义(实例化)一个bean

<bean id="flywayMigration" class="com.kedacom.flywaydemo.FlywayMigration" init-method="migrate">
	<property name="dataSource" ref="dataSource" />
</bean>

如果启动有问题,可以设置:

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" depends-on="flywayMigration">
	<property name="dataSource" ref="dataSource" />
</bean>

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" depends-on="flywayMigration">
	<property name="dataSource" ref="dataSource" />
</bean>
posted on 2016-12-30 13:39  斜月三星一太阳  阅读(297)  评论(0编辑  收藏  举报