You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot无法加载MySQL驱动类致数据源初始化失败求助

解决SpringBoot无法加载com.mysql.cj.jdbc.Driver驱动的问题

1. 检查MySQL驱动依赖是否正确引入

  • 确认build.gradle里已经添加了适配你MySQL版本的驱动依赖,比如MySQL 8.x可使用:
implementation 'mysql:mysql-connector-java:8.0.33'
  • 执行gradle clean build --refresh-dependencies刷新依赖,清除缓存后重新拉取
  • 查看本地maven仓库是否存在该依赖包,若缺失则重新同步或手动下载

2. 核对application.properties的数据库配置

  • 驱动类必须填写正确,MySQL 8.x只能用com.mysql.cj.jdbc.Driver,不能使用旧版的com.mysql.jdbc.Driver:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/your_db_name?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.username=your_username
spring.datasource.password=your_password
  • URL中的serverTimezone是MySQL 8.x必填项,缺失会触发时区错误,间接导致驱动加载失败

3. 排查依赖冲突

  • 执行gradle dependencies命令查看项目依赖树,检查是否有其他数据库驱动(比如H2、Oracle)与MySQL驱动冲突
  • 若存在冲突,在对应依赖中排除冲突包,示例:
implementation('some-third-party-library') {
    exclude group: 'com.h2database', module: 'h2'
}

4. 确认JDK版本兼容性

  • MySQL 8.x驱动要求JDK版本至少为1.8,确保项目使用的JDK版本达标,可在build.gradle中明确指定:
sourceCompatibility = '1.8'
targetCompatibility = '1.8'

内容的提问来源于stack exchange,提问作者Ben Chu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 07:31:19