当前位置: 首页 > 知识库问答 >
问题:

如何使用HikariDataSource解决“无法加载驱动程序类com.mysql.cj.jdbc.Driver”?

宗涵蓄
2023-03-14

我在Eclipse中运行Spring Boot应用程序时遇到以下异常。

创建在类路径资源中定义的名为“dataSourceScriptDatabaseInitializer”的bean时出错 [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]:通过方法'dataSourceScriptDatabaseInitializer'参数0表示不满意的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCreationException: 在类路径资源 [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] 中创建名称“dataSource”的 Bean 时出错: 通过工厂方法实例化失败;nested exception is org.springframework.beans.BeanInstantiationException: Failed to instanceiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception;嵌套异常是 java.lang.RuntimeException:无法在 HikariConfig 类加载器或线程上下文类加载器中加载驱动程序类 com.mysql.cj.jdbc.驱动程序

Spring Boot:2.5.3 Java:1.8

pom.xml:

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.3</version>
    <relativePath/> 
</parent>
<groupId>spring-examples</groupId>
<artifactId>hplusapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>hplusapp</name>
<description>Demo project for Spring Boot</description>
<properties>
    <java.version>1.8</java.version>
</properties>
<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mshtml" target="_blank">sql-jdbc</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

spring.jpa.hibernate.ddl auto=update

spring.datasource.url=jdbc:mysql://localhost:3306/hplus

spring.datasource .用户名=root

spring.datasource.password=root

我注意到如果我删除JPA依赖项,我没有得到任何错误。但是,我需要JPA用于进一步的用例。任何人都可以指导我吗?

共有1个答案

公孙宏远
2023-03-14

您使用MySQL,但使用MSSQL (Microsoft)的驱动程序。

删除

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <scope>runtime</scope>
</dependency>

并添加

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <scope>runtime</scope>
</dependency>
 类似资料: