引入flyway_core jar包java
java 代碼實現spring
public class FlywayMigration {sql
@Resource
private DataSource dataSource;this
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}編碼
public void migration() {
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource);code
flyway.setInitOnMigrate(true);xml
flyway.setTable("schemas_version");// 設置存放flyway metadata數據的表名it
// 設置flyway掃描sql升級腳本,java升級腳本的目錄路徑或包路徑
flyway.setLocations("database", "com.navinsight.holoviewer.back.transverse.util");io
flyway.setEncoding("UTF-8");// 設置sql腳本文件的編碼class
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>