SpringBoot整合Mybatis扫描不到Mapper的问题

本文详细阐述了如何在Spring Boot应用中使用MyBatis的UserDao接口,并介绍了在没有@MapperScan的情况下遇到的问题,以及通过添加mybatis-spring-boot-starter包和配置MapperScan注解来解决注入问题的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

package com.example.dao;

import com.example.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface UserDao {
    @Select("select * from user where id = #{id}")
    public User getById(@Param("id") int id);
    
}

通过mapper注解,无法将userdao注入到service中

在网上找了很多材料都是通过在主方法启动类上添加@MapperScan注解来解决问题的,代码如下,但是我一直没有@MapperScan注解

package com.example;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.dao")
public class SpringbootredisApplication {

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

}

找资料得知他在mybatis-spring-boot-starter这个jar包下,所以我添加了这个jar包,重启idea问题就解决了。maven依赖如下

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

另外我的程序打爆上传百度云,连接如下

链接:https://pan.baidu.com/s/1wBj8yyGd9s-TYEhMeof34A
提取码:1111

### SpringBoot 整合 MyBatisMapper 配置找不到的解决方案 在 Spring Boot整合 MyBatis 时,`Invalid bound statement (not found)` 错误通常是因为框架无法找到对应的 Mapper 接口或者其关联的 XML 文件。以下是可能的原因及其解决办法: #### 1. **确认 `mybatis.mapperLocations` 属性配置** 确保在 `application.properties` 或 `application.yml` 文件中正确指定了 Mapper XML 文件的位置。例如: ```properties mybatis.mapperLocations=classpath:mapper/*.xml ``` 上述配置表示所有的 Mapper XML 文件位于类路径下的 `mapper/` 目录下[^1]。 如果使用的是 YAML 格式的配置文件,则应这样书写: ```yaml mybatis: mapper-locations: classpath:mapper/*.xml ``` #### 2. **检查包扫描范围** Spring Boot 默认会自动扫描启动类所在包及其子包内的组件。因此,需确保 Mapper 接口所在的包被正确扫描到。可以在 `@MapperScan` 注解中显式声明 Mapper 所属的包路径。例如: ```java @SpringBootApplication @MapperScan("com.example.mapper") // 替换为实际的 Mapper 包路径 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 此操作可有效避免因未扫描Mapper 导致的问题[^4]。 #### 3. **验证接口与 XML 文件的一致性** 确保 Mapper 接口的方法签名与其对应的 XML 文件中的 SQL 定义完全一致。任何不匹配都会引发绑定失败错误。XML 文件应当存放在指定目录(如 `src/main/resources/mapper/`),并以 `.xml` 结尾。例如: ```xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.UserMapper"> <select id="getUserById" resultType="User"> SELECT * FROM users WHERE id = #{id}; </select> </mapper> ``` 此处的 `namespace` 值必须与对应 Mapper 接口全限定名相同[^3]。 #### 4. **调整注解方式** 当遇到 `could not autowire` 报错时,尝试更换注解类型。虽然 `@Autowired` 和 `@Resource` 功能相似,但它们的行为略有不同。推荐优先使用 `@Resource` 并通过 `name` 明确指定目标 Bean 名称。例如: ```java @Resource(name = "userMapper") private UserMapper userMapper; ``` 这种方式能够减少由于命名冲突引起的注入问题[^5]。 #### 5. **清理缓存与重新构建项目** 有时 IDE 缓存可能导致资源加载异常。建议执行以下步骤来排除干扰因素: - 清理 Maven 工程:运行命令 `mvn clean install`; - 刷新 IntelliJ IDEA 的索引:点击菜单栏选项 `File -> Invalidate Caches / Restart...` 后重启环境。 --- ### 总结 综合以上分析可知,解决 Spring Boot 整合 MyBatis 出现的 `mapper not found` 问题的关键在于合理配置映射器位置、扩展包扫描区域以及保持接口定义同 XML 实现同步等方面的工作。遵循这些指导原则即可显著降低此类故障发生的概率。 --- 相关问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值