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

搭建Spring Boot+MyBatis+MySQL项目启动报错:无法加载MybatisAutoConfiguration

Spring Boot + MyBatis启动异常排查与解决

问题背景

我正在搭建Spring Boot + MyBatis + MySQL项目,已经在pom.xml中添加了以下依赖:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>
<!-- 添加 MySQL -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.41</version>
</dependency>

主启动类代码如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

但运行时抛出了这个异常:

java.lang.IllegalStateException: Cannot load configuration class: org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:410) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:263) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.context.support....


问题分析与解决方案

这个异常的核心原因是Spring Boot版本和MyBatis Spring Boot Starter版本不兼容。从异常栈里能看到你的Spring Context版本是4.2.3.RELEASE,对应的是Spring Boot 1.3.x系列,但你用的MyBatis Starter是1.3.2,这个版本需要搭配Spring Boot 1.5.x才能正常工作,版本不匹配就会导致自动配置类加载失败。

给你几个具体的解决步骤:

  1. 用Spring Boot Parent统一版本管理(推荐)
    最省心的方式是在pom.xml里引入Spring Boot的parent依赖,它会自动帮你管理所有starter的兼容版本,避免手动指定版本踩坑:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.22.RELEASE</version> <!-- 和MyBatis 1.3.2完美兼容的稳定版本 -->
        <relativePath/>
    </parent>
    

    之后MyBatis Starter就可以不用写version了,parent会自动匹配:

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <!-- 移除手动指定的version -->
    </dependency>
    
  2. 手动匹配版本(如果不想用parent)
    如果你不想引入parent,那就手动确保版本对应:

    • MyBatis Spring Boot Starter 1.3.x ↔ Spring Boot 1.5.x
    • MyBatis Spring Boot Starter 2.x ↔ Spring Boot 2.x
      你可以根据需求升级Spring Boot到1.5.x,或者降级MyBatis Starter到适配1.3.x的版本。
  3. 排查依赖冲突
    有时候项目里可能引入了其他依赖,间接带来了旧版本的Spring组件,导致冲突。可以用Maven命令查看依赖树:

    mvn dependency:tree
    

    找到冲突的Spring相关jar包,在对应的依赖里排除掉旧版本。

  4. 补充数据库配置(别忘了这一步)
    解决版本问题后,还需要在application.properties里配置MySQL连接信息,不然启动后还是会报错:

    # 数据库连接配置
    spring.datasource.url=jdbc:mysql://localhost:3306/你的数据库名?useUnicode=true&characterEncoding=utf8&useSSL=false
    spring.datasource.username=root
    spring.datasource.password=你的数据库密码
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    # MyBatis配置
    mybatis.mapper-locations=classpath:mapper/*.xml  # 指定Mapper文件位置
    mybatis.type-aliases-package=com.你的包名.entity  # 实体类别名包路径
    

按照这些步骤调整后,应该就能顺利启动项目了。

内容的提问来源于stack exchange,提问作者ZhiGuang Tang

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:55:33