简体   繁体   English

未找到Springboot + MySQL +驱动程序类

[英]Springboot + MySQL + Driver Class Not Found

I'm doing a simple spring boot project with a thread pool and MySQL in order to connect to MySQL whenever I'm adding spring-boot-starter-jdbc I'm getting below error. 我正在做一个带有线程池和MySQL的简单弹簧启动项目,以便在我添加spring-boot-starter-jdbc时连接到MySQL我遇到了以下错误。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.s

Update 1: 更新1:

    <dependencies>
            <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

<dependency>
    <groupId>com.opencsv</groupId>
    <artifactId>opencsv</artifactId>
    <version>3.3</version>
</dependency>

    </dependencies>

It seems like you are missing mysql-connector dependency, Add these to your pom. 您似乎缺少mysql-connector依赖项,将这些添加到您的pom中。

Maven: Maven的:

   <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

or gradle: 或者是gradle:

compile "mysql:mysql-connector-java:*"

Looks like you forget to add dependency to MySQL (Spring Boot is using H2 database by default), you should add next lines to your pom.xml: 看起来您忘记向MySQL添加依赖项(Spring Boot默认使用H2数据库),您应该在pom.xml中添加下一行:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

First include this in your pom file. 首先在你的pom文件中包含它。

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

Second, clean install the maven project. 其次,清理安装maven项目。 If you are using eclipse check your JDK version and maven (Sometime jre is used instead of jdk, so, though in console everything looks fine but acutally the jar is not downloaded) in short .m2 repo should have this jar file when maven build is done. 如果你正在使用eclipse检查你的JDK版本和maven(使用sometime jre而不是jdk,所以,虽然在控制台中一切看起来都很好,但实际上没有下载jar)。简而言之.m2 repo在maven build是的时候应该有这个jar文件完成。

Scope "runtime" is good for unit test and container as tomcat etc. when container provide jdbc driver. 当容器提供jdbc驱动程序时,范围“运行时”适用于单元测试和容器作为tomcat等。 When run standalone apps (spring-boot) you should remove it or set to "compile". 运行独立应用程序(spring-boot)时,应将其删除或设置为“compile”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM