添加maven 依赖

<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
    <version>5.0.3</version>
</dependency>
<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>5.0.3</version>
</plugin>

在项目路径下创建 db/migration

/db/migration

sprint boot ,maven 项目 flyway 使用-LMLPHP

sql 里面是创建数据库建表,加入数据 等

但是提示报错

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'flyway' threw exception; nested exception is java.lang.IllegalStateException: Cannot find migrations location in: [classpath:db/migration] (please add migrations or check your Flyway configuration)
 

sprint boot ,maven 项目 flyway 使用-LMLPHP

/db/migration 的位置问题

改到项目下就可以了

sprint boot ,maven 项目 flyway 使用-LMLPHP

但仍然报错

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Wrong migration name format: V1_initial.sql(It should look like this: V1.2__Description.sql)

注意格式 :V1.2__Description.sql 

修改后又有新的报错

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Found non-empty schema(s) `test` without schema history table! Use baseline() or set baselineOnMigrate to true to initialize the schema history table.

非空的数据库是不会执行,提示没有flyway 版本的表,初始化需要空的数据库
 

继续报错

===2019-09-19 16:08:34.078 INFO  org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory Line:44  - Creating Schema History table: `test`.`flyway_schema_history`
===2019-09-19 16:08:43.241 WARN  org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext Line:558 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.sqlscript.FlywaySqlScriptException: 
Script failed

-------------
SQL State  : null
Error Code : 0
Message    : sql injection violation, comment not allow : CREATE TABLE `test`.`flyway_schema_history` (
    `installed_rank` INT NOT NULL,
    `version` VARCHAR(50),
    `description` VARCHAR(200) NOT NULL,
    `type` VARCHAR(20) NOT NULL,
    `script` VARCHAR(1000) NOT NULL,
    `checksum` INT,
    `installed_by` VARCHAR(100) NOT NULL,
    `installed_on` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `execution_time` INT NOT NULL,
    `success` BOOL NOT NULL,
    -- Add the primary key as part of the CREATE TABLE statement in case `innodb_force_primary_key` is enabled
    CONSTRAINT `flyway_schema_history_pk`PRIMARY KEY (`installed_rank`)
) ENGINE=InnoDB
Line       : 17

.

.

.

===2019-09-19 16:08:43.241 INFO  org.springframework.jmx.export.annotation.AnnotationMBeanExporter Line:451 - Unregistering JMX-exposed beans on shutdown
===2019-09-19 16:08:43.257 INFO  com.alibaba.druid.pool.DruidDataSource Line:1825 - {dataSource-1} closed
===2019-09-19 16:08:43.257 INFO  org.springframework.cache.ehcache.EhCacheManagerFactoryBean Line:192 - Shutting down EhCache CacheManager
===2019-09-19 16:08:43.288 INFO  org.apache.catalina.core.StandardService Line:180 - Stopping service [Tomcat]
===2019-09-19 16:08:43.304 INFO  org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener Line:101 - 

sql 是可以执行创建表的

因为在数据源配置时,加上了 Druid 的 wall 过滤器。而它默认的拦截策略是,不允许 SQL 中带有备注。

手工优先创建表再启动项目

sprint boot ,maven 项目 flyway 使用-LMLPHP

没毛病启动成功

09-19 21:36