解决无法引入 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>
相关推荐
用户8307196840822 天前
spring ai alibaba + nacos +mcp 实现mcp服务负载均衡调用实战
spring boot·spring·mcp
NE_STOP5 天前
springMVC-HTTP消息转换器与文件上传、下载、异常处理
spring
JavaGuide6 天前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code
玹外之音6 天前
Spring AI MCP 实战:将你的服务升级为 AI 可调用的智能工具
spring·ai编程
来一斤小鲜肉6 天前
Spring AI入门:第一个AI应用跑起来
spring·ai编程
NE_STOP6 天前
springMVC-常见视图组件与RESTFul编程风格
spring
what丶k7 天前
Spring AI 多模态开发全解析:从入门到企业级落地
后端·spring·ai编程
NE_STOP7 天前
springMVC-获取前端请求的数据与三个作用域
spring
莫寒清7 天前
Spring MVC:@PathVariable 注解详解
java·spring·mvc