MyBatis错误:Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Canno

MyBatis错误:Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Canno,第1张

MyBatis错误:Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: com.mysql.cj.jdbc.Driver

从报错来看,是因为找不到com.mysql.cj.jdbc.Driver类,那应该是MySQL驱动的问题,但是在maven项目构建中已经引入了MySQL驱动的jar包,为什么还报错?

仔细查看了以下pom.xml文件,因为逆向工程使用到了mybatis generator插件,该插件需要依赖MySQL驱动,我就将驱动放到了plugin标签下,但是项目运行也要使用数据库,因此也要将依赖添加到项目dependencies标签下。因为插件依赖和项目依赖不同级,项目运行时访问不到插件中的依赖,因此必须将项目依赖放到项目能够访问到的地方。

下面的pom.xml文件是报错的


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.atguigu.mybatisgroupId>
    <artifactId>MyBatis_MBGartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>jarpackaging>

    <dependencies>
        
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.9version>
        dependency>

    
    <build>

        
        <plugins>
            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.4.0version>
                <dependencies>
                    
                    
                    <dependency>
                        <groupId>org.mybatis.generatorgroupId>
                        <artifactId>mybatis-generator-coreartifactId>
                        <version>1.4.0version>
                    dependency>

                    
                    
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>8.0.27version>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>

project>

**解决上面问题的办法:**在pom.xml的project标签下的dependencies标签下添加SQL驱动就可以。

修改后的pom.xml文件如下:


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.atguigu.mybatisgroupId>
    <artifactId>MyBatis_MBGartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>jarpackaging>
    
    <dependencies>
        
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.9version>
        dependency>

        
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.27version>
        dependency>

    dependencies>

    
    <build>

        
        <plugins>
            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.4.0version>
                <dependencies>
                    
                    
                    <dependency>
                        <groupId>org.mybatis.generatorgroupId>
                        <artifactId>mybatis-generator-coreartifactId>
                        <version>1.4.0version>
                    dependency>
					
                    
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>8.0.27version>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>

project>

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/905212.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-15
下一篇 2022-05-15

发表评论

登录后才能评论

评论列表(0条)

保存