无法实例化H2

时间:2019-06-24 08:54:47

标签: spring-boot h2

我正在尝试将H2设置为内存数据库。我将其配置为:

spring:
  profiles: qa
  datasource:
    #url: jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=CREATE SCHEMA IF NOT EXISTS "public"
    jdbc-url: jdbc:mysql://localhost:3306/lisdashboard
    username: '@lisdashboard.db.username@'
    password: '@lisdashboard.db.password@'
  h2:
    console:
      path: /admin/h2-console

但是当我尝试运行该错误时

- 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 java.lang.RuntimeException: Driver org.h2.Driver claims to not accept jdbcUrl, jdbc:mysql://localhost:3306/lisdashboard
-

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
- Application run failed
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 java.lang.RuntimeException: Driver org.h2.Driver claims to not accept jdbcUrl, jdbc:mysql://localhost:3306/lisdashboard

我不确定为什么它不接受jdbc url,我的confg格式是否有问题?

2 个答案:

答案 0 :(得分:0)

您正在使用MySQL jdbc url尝试连接到H2实例。

您需要替换配置以使用网址jdbc:h2:mem:,而不是现在使用的网址jdbc:mysql://localhost:3306/lisdashboard

如果要使用其他类型的H2实例,请检查documentation以查看替代方案(例如文件实例)。

答案 1 :(得分:0)

如果要使用H2,您的配置应如下所示:

spring:
  profiles: qa
  datasource:
    url: jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=CREATE SCHEMA IF NOT EXISTS "public"
    username: '@lisdashboard.db.username@'
    password: '@lisdashboard.db.password@'
  h2:
    console:
      path: /admin/h2-console

您的网址前面有#个,而#是YAML中的注释