测试项目基础来源于官网例子:https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-flyway
官网例子修改后目录结构如图:
成功完成官方例子的运行和测试后,迫不及待的想把flyway集成到自己搭建项目中使用,但是每次都是在还未加载flyway就开始进行model的扫描。
遇到问题:
在遇到需要先加载数据方面的操作,都需要在flyway执行完毕后才能加载不然会出现找不到新增的表或者修改的表结构、新增表数据。
解决办法:
在对应的bean上加上@DependsOn注解
实际项目中开启hibernate事务,代码如下:
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
public class HibernateConfig {
@Value("${hibernate.hbm2ddl.auto}")
private String auto;
@Value("${hibernate.dialect}")
private String dialect;
@Value("${hibernate.show_sql}")
private String show_sql;
@Value("${hibernate.packagesToScan}")
private String packagesToScan;
@Bean(name = "sessionFactory")
@DependsOn("flywayInitializer")
public SessionFactory sessionFactory(DataSource dataSource) {
LocalSessionFactoryBean bean = new LocalSessionFactoryBean();
bean.setDataSource(dataSource);
Properties props = new Properties();
props.put("hibernate.hbm2ddl.auto", auto);
props.put("hibernate.dialect", dialect);
props.put("hibernate.show_sql", show_sql);
bean.setHibernateProperties(props);
bean.setPhysicalNamingStrategy(PhysicalNamingStrategyStandardImpl.INSTANCE);
bean.setPackagesToScan(StringUtils.split(packagesToScan, ","));
try {
bean.afterPropertiesSet();
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Bean
@DependsOn("flywayInitializer")
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory)
throws IOException {
if (sessionFactory == null) {
return null;
}
return new HibernateTransactionManager(sessionFactory);
}
}
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 http://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>1.5.3.RELEASE</version>
</parent>
<artifactId>spring-boot-flyway</artifactId>
<name>Flyway Sample</name>
<description>Flyway Sample</description>
<dependencies>
<!-- Compile -->
<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.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties配置清单:
#datasource
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/twd?useUnicode=true&characterEncoding=UTF-8
spring.datasource.jdbcInterceptors=org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer
#spring jpa
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.show-sql=true
# FLYWAY (FlywayProperties)
flyway.baseline-version=1
flyway.enabled=true
flyway.baseline-on-migrate=true
flyway.locations=classpath:db/migration/{vendor}
flyway.sql-migration-prefix=V
flyway.sql-migration-suffix=.sql
注意:fly.locations中最好加上/{vendor},这样在连接不同数据库源时,会执行不同目录下的sql
V1_1__create.sql清单:
DROP TABLE IF EXISTS `PERSON`;
CREATE TABLE `PERSON` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) not null,
`last_name` varchar(255) not null,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
delete from `PERSON`;
insert into `PERSON` (first_name, last_name) values ('Dave', 'Syer');
insert into `PERSON` (first_name, last_name) values ('Dave', 'Syer');