1. Flyway
可以将初始化sql在项目启动时候执行,取代单独的DBN更新包
2. 依赖 <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>5.2.1</version> </dependency> 3. 配置说明(springboot配置版本)springboot中默认使用flyway配置进行加载,使用api方式进行灵活配置时候需要将其(FlywayAutoConfiguration)进行屏蔽,不然会报错,部分报错信息及屏蔽方式如下所示
5.1 报错信息 Caused by: java.lang.reflect.InvocationTargetException: null at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:70) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ... 18 common frames omitted Caused by: org.flywaydb.core.api.FlywayException: Found non-empty schema(s) `xxxx` without schema history table! Use baseline() or set baselineOnMigrate to true to initialize the schema history table. at org.flywaydb.core.Flyway$1.execute(Flyway.java:1371) at org.flywaydb.core.Flyway$1.execute(Flyway.java:1341) at org.flywaydb.core.Flyway.execute(Flyway.java:1696) at org.flywaydb.core.Flyway.migrate(Flyway.java:1341) ... 25 common frames omitted 5.2 屏蔽方法 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; @SpringBootApplication(exclude = {FlywayAutoConfiguration.class}) public class HzBaseApplication { public static void main(String[] args) { SpringApplication.run(HzBaseApplication.class, args); } } 6. 执行结果 6.1 日志 2022-12-16 11:20:34,800 INFO (DruidDataSource.java:998)- {dataSource-1} inited 2022-12-16 11:20:35,116 INFO (Slf4jLog.java:49)- Database: jdbc:mysql://106.14.11.239:3306/hz_base (MySQL 5.7) 2022-12-16 11:20:35,343 INFO (Slf4jLog.java:49)- Successfully validated 1 migration (execution time 00:00.105s) 2022-12-16 11:20:35,492 INFO (Slf4jLog.java:49)- Current version of schema `hz_base`: 1.0.0 2022-12-16 11:20:35,493 WARN (Slf4jLog.java:53)- outOfOrder mode is active. Migration of schema `hz_base` may not be reproducible. 2022-12-16 11:20:35,509 INFO (Slf4jLog.java:49)- Schema `xxxx` is up to date. No migration necessary. 2022-12-16 11:20:35,565 INFO (MainDataSourceMigration.java:56)- 初始化Sql成功 6.2 数据库验证 ?7.?扩展多数据源多数据源通过buildConfiguration构建多个配置(数据源及script脚本),执行多次即可完成多数据源sql的初始化。
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。 |
标签: #flyway多数据源