java 无法加载驱动程序类:org.h2.Driver with springboot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43616563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:35:58  来源:igfitidea点击:

Cannot load driver class: org.h2.Driver with springboot

javaspringgradlespring-boot

提问by rayman

@SpringBootApplication
@Slf4j
public class Starter {

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

Applicaiton.yml

应用程序.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:

构建.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")
}

error:

错误:

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

Any idea what am I missing?

知道我错过了什么吗?

I tried to remove: driverClassName: org.h2.Driver from yml file. didnt work.

我试图从 yml 文件中删除:driverClassName: org.h2.Driver。没用。

tried to modify build.gradle and version to H2.. also didnt work.

试图将 build.gradle 和 version 修改为 H2 .. 也没有奏效。

回答by thejames42

try removing the driver name from your application.yml. hibernate should pick be able to pick it up from your url

尝试从 application.yml 中删除驱动程序名称。hibernate 应该选择能够从你的 url 中选择它