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

Spring Boot整合MyBatis出现Mapper找不到、实体非托管报错如何解决

问题1:找不到CustomerMapper Bean的修复方案

报错原因是MyBatis的Mapper接口默认不会被Spring自动识别为Bean,可选两种修复方式:

  • 方案一:在启动类添加@MapperScan("com.crudapp.mapper")注解,指定Mapper接口的扫描路径,你已经完成这步,解决了第一个报错
  • 方案二:每个Mapper接口除了加@Mapper注解外,额外添加@Repository注解,项目Mapper数量多的话更推荐用第一种方案
问题2:Not a managed type: class com.crudapp.entity.Customer报错修复方案

报错原因是你最初用Spring Data JPA做CRUD时残留了JPA相关代码,Spring Boot默认会自动开启JPA仓库扫描,现在你切换到MyBatis架构,二选一解决即可:

方案A:完全移除JPA相关内容(推荐)

如果你确定后续只用MyBatis做持久层,直接清理JPA残留:

  1. 删除com.crudapp.repository包下的所有JPA Repository类(即报错提到的CustomerRepo)
  2. 移除项目依赖里的Spring Data JPA包,以Maven为例,删除pom.xml中如下配置:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
  1. 清理完成后重启项目即可。

方案B:保留JPA的修复方式

如果你暂时需要保留JPA相关代码,补充JPA的实体类配置即可:

  1. 给com.crudapp.entity.Customer类添加JPA注解:
@Entity
@Table(name = "customers")
public class Customer {
    @Id
    private Long id; // 对应你的主键字段
    // 其余字段无需改动
}
  1. 如果还是报错,在启动类上添加@EntityScan("com.crudapp.entity")注解,手动指定JPA的实体类扫描路径。

其他潜在问题排查

你贴出的CustomerServiceImpl实现类代码中没有加@Service注解,记得补上,否则后续会报找不到CustomerService Bean的错误。

内容的提问来源于stack exchange,提问作者Chaudhry Talha

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 04:36:00