spring boot 项目增加flyway的使用遇到问题解决
2017-05-25 17:13
906 查看
测试项目基础来源于官网例子:https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-flyway
官网例子修改后目录结构如图:
成功完成官方例子的运行和测试后,迫不及待的想把flyway集成到自己搭建项目中使用,但是每次都是在还未加载flyway就开始进行model的扫描。
遇到问题:
在遇到需要先加载数据方面的操作,都需要在flyway执行完毕后才能加载不然会出现找不到新增的表或者修改的表结构、新增表数据。
解决办法:
在对应的bean上加上@DependsOn注解
实际项目中开启hibernate事务,代码如下:
pom.xml配置清单:
application.properties配置清单:
V1_1__create.sql清单:
V1_1__create.sql清单:
delete from `PERSON`;
insert into `PERSON` (first_name, last_name) values ('Dave', 'Syer');
官网例子修改后目录结构如图:
成功完成官方例子的运行和测试后,迫不及待的想把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;
V1_1__create.sql清单:
delete from `PERSON`;
insert into `PERSON` (first_name, last_name) values ('Dave', 'Syer');
相关文章推荐
- SpringBoot项目使用视图解析器解决Circular view path 问题
- 使用eclipse构建springboot项目的解决的一些问题
- IntelliJ IDEA中Spring Boot项目使用spring-boot-devtools无法实现热部署/热更新的问题解决
- 用Spring Boot 构建项目中遇到问题及解决方法
- SpringBoot使用可能遇到的问题及解决方法
- 集成jcaptcha到spring-boot+maven项目中时出现java.lang.NoSuchMethodError: com.jhlabs.image.WaterFilter的问题的解决方法
- 使用myeclipse将Java项目打成jar包遇到classnotfound的问题解决办法
- 使用spring boot 改造老项目,多个数据源问题
- springboot项目打包后运行出现问题解决办法
- 严重: Error listenerStart的解决办法---使用Spring时遇到的问题
- 使用IntelliJ IDEA 创建Spring Boot项目时 显示 connect timed out 解决方法
- Idea使用Maven创建多模块SpringBoot项目问题记录
- springboot的restController使用swagger遇到的问题。
- visual studio设置为使用IIS运行网站时加载项目遇到权限问题的解决方法
- Swagger使用及Springfox+SpringBoot解决404问题
- 使用mssql2008新特性(存储过程参数类型使用"用户自定义表"来实现批量DML更新多表)解决项目里遇到的性能问题
- visual studio设置为使用IIS运行网站时加载项目遇到权限问题的解决方法
- 使用VS2005做VB项目时遇到的问题,现已经解决
- 使用 eclipse 导入android项目 遇到问题解决
- 使用eclipse+git插件开发android项目时遇到的问题及解决