The new driver class is `com.mysql.cj.jdbc.Driver'

本文详细解析了在SpringBoot2.1及以上版本中集成Mybatis时出现的关于mysql驱动类的报错问题。指出该问题是由于springboot版本升级引起的,并提供了两种解决方案:一是将springboot版本回退到2.0.4.RELEASE;二是更新mysql驱动类为com.mysql.cj.jdbc.Driver,并在JDBC连接中指定时区。这为遇到类似问题的开发者提供了明确的指导。

新建springboot项目集成mybatis 启动报错:

Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

如图:
在这里插入图片描述

网上大多原因都是说 mysql驱动包依赖的版本6以上的原因,但是我的依赖5.1.38 照样报错

        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>5.1.38</scope>
        </dependency>

反复和之前没问题项目进行比较发现,是springboot版本问题,版本2.1.0.RELEASE及以上版本会报上面错误,将版本改2.0不再报错:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/>
    </parent>

修改springboot版本2.0.4.RELEASE后,重新导入pom.xml , 右键pom.xml -> Maven ->Reimport
再次启动不再报错 The new driver class is `com.mysql.cj.jdbc.Driver’ 问题解决!

应该是springboot2.1版本改动导致,有时间去看下2.1的变化.
如果还是希望使用springboot2.1以后的版本的话,那就将mysql驱动类配置由com.mysql.jdbc.Driver 改为 com.mysql.cj.jdbc.Driver ,同时注意 JDBC连接Mysql6 com.mysql.cj.jdbc.Driver 需要指定时区serverTimezone ,如下:

jdbc:mysql://127.0.0.1:3306/robot_information?useUnicode=true&characterEncoding=UTF-8&usessl=false&servertimezone=GMT%2B8

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值