解决无法引入 mysql-connector-j 的问题

开发环境

  • Windows 10
  • Oracle JDK 1.8
  • Maven 3.8.8
  • IntelliJ IDEA 2022.2.2

问题

在使用 Spring initializr 创建 Spring Boot 项目时,无法引入 mysql-connector-j 这个依赖,报错信息:

复制代码
com.mysql:mysql-connector-j:jar:unknown was not found in https://maven.aliyun.com/repository/public during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of aliyunmaven has elapsed or updates are forced

pom.xml 文件

复制代码
// 父依赖
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.14</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <version>8.0.32</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.3.1</version>
    </dependency>
</dependencies>

原因

我使用的 Spring Boot 版本为 2.5.14,该版本使用的 mysql-connector-j 版本为 8.0.29,同时我的依赖是从阿里云的镜像库而不是 Maven 的中央仓库上下载的。我尝试手动引入这个版本。

复制代码
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.0.29</version>
    <scope>runtime</scope>
</dependency>

结果报了同样的错误。也就是说,阿里云镜像仓库可能没有这个版本的 mysql-connector-j

解决方法

手动引入高于 8.0.29 版本的 mysql-connector-j 即可解决。

复制代码
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.0.32</version>
    <scope>runtime</scope>
</dependency>
相关推荐
计算机学姐17 小时前
基于SpringBoot的电影点评交流平台【协同过滤推荐算法+数据可视化统计】
java·vue.js·spring boot·spring·信息可视化·echarts·推荐算法
dear_bi_MyOnly19 小时前
【多线程——线程状态与安全】
java·开发语言·数据结构·后端·中间件·java-ee·intellij-idea
tb_first19 小时前
万字超详细苍穹外卖学习笔记4
java·spring boot·笔记·学习·spring·mybatis
To Be Clean Coder21 小时前
【Spring源码】createBean如何寻找构造器(四)——类型转换与匹配权重
java·后端·spring
何中应1 天前
IDEA 中让 Git 忽略 .idea 目录
java·git·intellij-idea
键盘帽子1 天前
多线程情况下长连接中的session并发问题
java·开发语言·spring boot·spring·spring cloud
无名-CODING1 天前
Spring事务管理完全指南:从零到精通(上)
java·数据库·spring
多多*1 天前
2026年最新 测试开发工程师相关 Linux相关知识点
java·开发语言·javascript·算法·spring·java-ee·maven
树码小子1 天前
SpringIoC & DI (1):IOC介绍 & Spring IoC使用 & DI
java·后端·spring
tb_first1 天前
万字超详细苍穹外卖学习笔记5
java·数据库·spring boot·笔记·学习·spring