Mybatis报错Cannot find class: com.mysql.cj.jdbc.Driver排查踩坑记录

初次学习mybatis,写了一个最简单的demo,然而运行报错:
java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: com.mysql.cj.jdbc.Driver

判断问题出在driver驱动上,根据网上常见解决办法依次排查(我的MySQL版本是8):

step 1.检查maven依赖,好像没问题,很多人就是把mysql-connector-java版本从5升到8就好了:

<dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
        </dependency>
</dependencies>

step 2.检查mybatis配置文件:尝试在url "jdbc:mysql:///study?serverTimezone=UTC"中添加参数“&useSSL=false”,仍然报错。

<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql:///study?serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="123"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="UserMapper.xml"/>
    </mappers>
</configuration>

step 3.偶然在maven仓库发现mysql-connector-java有更新版本,决定一试,把version 8.0.17换为8.0.23
Mybatis报错Cannot find class: com.mysql.cj.jdbc.Driver排查踩坑记录

替换后果然运行成功,看来问题还是在step 1,如果你也试了各种办法无解,可以再关注下是不是mysql-connector-java版本的问题。

<dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version>
        </dependency>
</dependencies>
上一篇:数据库系统概论(中)


下一篇:Flink SQL自定义connector