错误:java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver

1.创建springboot项目遇到的错误:java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver

错误截图:

错误:java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver

此时配置文件中mysql相关依赖为:

错误:java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver

上网查了资料发现是MySQL版本和配置的区别问题:

JDBC连接Mysql5 com.mysql.jdbc.Driver:

spring:
  datasource:
     url: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8

JDBC连接Mysql6 com.mysql.cj.jdbc.Driver, 需要指定时区serverTimezone:

spring:
  datasource:
     url: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false

2.修改后代码如下

pom文件依赖如下:

                <!-- mybatis依赖 -->
        <dependency>
           <groupId>org.mybatis.spring.boot</groupId>
           <artifactId>mybatis-spring-boot-starter</artifactId>
           <version>1.2.0</version>
        </dependency>
        <dependency>
           <groupId>mysql</groupId>
	   <artifactId>mysql-connector-java</artifactId>
	   <version>6.0.6</version>
        </dependency>
		<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.29</version>
        </dependency>

配置文件如下(application.yml):

spring:
  datasource:
    #url: jdbc:mysql://dev1.ichangyun.cn:13306/zjl?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
    url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: admin
    # 使用druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    # 下面为连接池的补充设置,应用到上面所有数据源中
    # 初始化大小,最小,最大
    initialSize: 5
    minIdle: 5
    maxActive: 20
    # 配置获取连接等待超时的时间
    maxWait: 60000
    # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    timeBetweenEvictionRunsMillis: 60000
    # 配置一个连接在池中最小生存的时间,单位是毫秒
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM user
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    # 打开PSCache,并且指定每个连接上PSCache的大小
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,‘wall‘用于防火墙
    spring.datasource.filters: stat,wall
    # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    # 合并多个DruidDataSource的监控数据
    #useGlobalDataSourceStat: true

错误:java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver

上一篇:关于springboot中数据库连接池报错:testWhileIdle is true, validationQuery not set


下一篇:MySQL和MyCat replace