无法使用SpringBoot加载驱动类:org.h2.Driver

9
@SpringBootApplication
@Slf4j
public class Starter {

    public static void main(String[] args) {
        SpringApplication.run(Starter.class, args);
    }
}

Applicaiton.yml

spring:
  profiles: default
  allowedIPPattern: 127.0.0.1|0:0:0:0:0:0:0:1|::1
  jpa.hibernate.ddl-auto: validate
  datasource:
    driverClassName: org.h2.Driver
    url: jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE;INIT=create schema if not exists \"public\"\; SET SCHEMA public;

build.gradle:

buildscript {

    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }

    ext.ver = [
            'springboot' : '1.5.3.RELEASE',
            'slf4j': '1.7.12'
    ]

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${ver.springboot}")
        classpath "se.transmode.gradle:gradle-docker:1.2"
    }
}

group 'com.mycompany'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'org.springframework.boot'



repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile("org.springframework.boot:spring-boot-starter-web:${ver.springboot}")
    compile ('org.projectlombok:lombok:1.16.6')
    compile("org.springframework.boot:spring-boot-starter-actuator")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    runtime("mysql:mysql-connector-java:5.1.38")
    runtime("org.flywaydb:flyway-core")
    compile("com.h2database:h2")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
}

错误:

Caused by: java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver
    at org.springframework.util.Assert.state(Assert.java:70) ~[spring-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:231) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:183) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:42) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Tomcat.dataSource(DataSourceConfiguration.java:56) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_73]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_73]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_73]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_73]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    ... 55 common frames omitted

您有什么想法我可能遗漏了什么?

我尝试从yml文件中删除:driverClassName:org.h2.Driver,但没有起作用。

尝试修改build.gradle和版本到H2..同样也没有起作用。


1
也许是 build.gradle 文件中的 H2 版本号? - Mario
你建议将其更改为哪个版本? - rayman
我目前正在使用“com.h2database:h2:1.4.194”,并没有任何问题。 - Mario
用户user17780没有仔细阅读问题。这是关于H2数据库,而不是MySql数据库。请恢复问题。谢谢。 - CCC
1个回答

6

尝试从您的application.yml文件中删除驱动程序名称。Hibernate应该能够从您的URL中选择它。


由于java.lang.ClassNotFoundException: org.h2.Driver,出现了“removed. got”错误。 - rayman
1
看DataSourceProperties方法,它似乎包装了一个classNotFoundError,这表明驱动程序没有从依赖项中正确导入。检查您的库以查看是否可以在那里找到它。您也可以尝试将其设置为testCompile。 - thejames42
什么库?我已经向你展示了通过我的build.grade文件导入的所有库。 - rayman
也尝试将其设置为testCompile。同样的东西。 - rayman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接