Field baseMapper in xxxxx required a bean of type 'xxxxxx' that could not be found 问题

在Spring Boot中整合通用Mapper时遇到bean找不到的问题,本文详细介绍了问题的解决思路,包括检查pom.xml中相关依赖,代码目录结构,确保BaseMapper与业务类Mapper不在同一目录,Service实现类为抽象类,业务类Mapper需添加@Mapper注解或使用@MapperScan,以及检查命名空间和接口名的对应关系,避免低级错误。希望对遇到同样问题的人有所帮助。

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

最近在整合spring boot 和通用mapper时遇到错误,纠结了好长时间,先看错误

Description:Field baseMapper in com.sinosoft.common.mybatis.service.impl.BaseServiceImpl required a bean of type 'com.sinosoft.common.mybatis.mapper.MyBaseMapper' that could not be found.Action:Consider defining a bean of type 'com.sinosoft.common.mybatis.mapper.MyBaseMapper' in your configuration.Process finished with exit code 1

问题解决思路
1.先看下pom,下边是整合通用mapper所须的依赖

 <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

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

        <!-- pagehelper分页插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!-- 通用mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>


        <!-- 阿里巴巴druid数据库连接池 非必须 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.18</version>
        </dependency>

2.代码目录结构

这里写图片描述
3.需要检查的点
3.1basemapper 不能和业务类的mapper放在同一目录下
这里写图片描述
3.2service实现类应该抽象的避免被通用mapper扫描

public abstract class BaseServiceImpl<T,ID extends Serializable> implements BaseService<T,ID> {
    @Autowired
    public MyBaseMapper<T> baseMapper;

3.3业务类mapper应该加上@Mapper注解,或者在spring boot启动类上添加@MapperScan注解

@Mapper
public interface MusicMapper extends MyBaseMapper<MusicModel> {
}
@SpringBootApplication
@MapperScan(basePackages ={"com.sinosoft.musicproxy.mapper"})
public class MusicProxyApplication  {
    public static void main(String[] args) {
        SpringApplication.run(MusicProxyApplication.class, args);
    }
}

3.4业务类应该继承你自己的通用mapper接口。我自己的错误就是吧MyBaseMapper写成了BaseMapper(这个错误是真的低级)

//错误
@Mapper
public interface MusicMapper extends BaseMapper<MusicModel> {
}

//正确
@Mapper
public interface MusicMapper extends MyBaseMapper<MusicModel> {
}

3.5namespace对应接口名

<mapper namespace="com.sinosoft.musicproxy.mapper.MusicMapper" >
package com.sinosoft.musicproxy.mapper;

/**
 * @author fxl
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2018-06-149:55
 */
@Mapper
public interface MusicMapper extends BaseMapper<MusicModel> {
}

3.6.还有可能是你的某些mapper没有加@mapper注解,也会出现这种情况
这算我在遇到这个问题在网上找到解决方法吧,希望别有人跟我一样犯同样的错误,特别是低级错误。写的不好,见谅。

### 关于 `articleMapper` Bean 未找到的问题分析 在 Spring Boot 中,当遇到 `articleMapper` Bean 未被正确加载的情况时,通常是因为以下几个原因: #### 1. **Mapper 接口未被扫描** 如果 Mapper 接口所在的包路径不在 Spring 的组件扫描范围内,则不会自动注册为 Bean。可以通过指定 `@MapperScan` 注解来显式声明需要扫描的包路径[^3]。 ```java @SpringBootApplication @MapperScan("com.example.mapper") // 替换为实际的 mapper 包路径 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` #### 2. **缺少 MyBatis-Plus 或 MyBatis 相关依赖** 如果项目中未引入 MyBatis-Plus 或 MyBatis 的相关依赖,可能导致 Mapper 接口无法正常工作。以下是常见的 Maven 依赖配置: ```xml <!-- MyBatis-Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.3</version> </dependency> <!-- MySQL 驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> ``` #### 3. **Mapper 文件未正确映射到接口** 当使用 XML 映射文件时,需确保其位置与命名符合约定。默认情况下,MyBatis 会在资源目录下的 `mapper` 文件夹中查找对应的 XML 文件。如果路径不匹配,可能会导致 Bean 加载失败[^4]。 #### 解决方案: - 将 XML 文件放置在 `resources/mapper/` 下。 - 确保 XML 文件名与 Mapper 接口名称一致(例如:`ArticleMapper.java` 对应 `ArticleMapper.xml`)。 #### 4. **Bean 名称冲突或重复定义** 若存在多个同名的 Bean 定义,Spring 可能会选择错误的一个,从而引发问题。建议检查是否存在其他地方重新定义了相同的 Bean[^2]。 #### 5. **反射调用 Service 失败** 使用反射动态获取并调用 Service 方法时,可能因类型转换或其他异常而导致 Bean 找不到。可通过以下方式验证 Bean 是否已成功加载: ```java ApplicationContext context = SpringApplication.run(Application.class, args); ArticleMapper articleMapper = (ArticleMapper) context.getBean("articleMapper"); System.out.println(articleMapper != null ? "Bean 存在" : "Bean 不存在"); ``` --- ### 示例代码调整 假设当前项目的结构如下: - 数据库表名为 `articles` - Mapper 接口为 `ArticleMapper` - Service 层实现类为 `ArticleServiceImpl` #### 修改后的代码示例 ##### Mapper 接口 ```java public interface ArticleMapper extends BaseMapper<Article> {} ``` ##### Service 实现类 ```java @Service public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService { } ``` ##### Controller 类 ```java @RestController @RequestMapping("/api/articles") @RequiredArgsConstructor public class ArticleController { private final IArticleService articleService; @GetMapping("/{id}") public ResponseEntity<Article> getArticleById(@PathVariable Long id) { Optional<Article> articleOptional = Optional.ofNullable(articleService.getById(id)); return articleOptional.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } } ``` --- ###
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值