【杂类】理解 @Repository 和 Mapper 的关系

目录

[​1. @Mapper(MyBatis 注解)​​](#1. @Mapper(MyBatis 注解))

[​2. @Repository(Spring 注解)​​](#2. @Repository(Spring 注解))

[​3. 二者在 MyBatis 项目中的协作关系​](#3. 二者在 MyBatis 项目中的协作关系)

[​场景 1:使用 @MapperScan(推荐)​​](#场景 1:使用 @MapperScan(推荐))

[场景 2:单独使用 @Mapper(不推荐)​​](#场景 2:单独使用 @Mapper(不推荐))

[​场景 3:同时使用 @Mapper+ @Repository(冗余)​​](#场景 3:同时使用 @Mapper+ @Repository(冗余))

[​4. 关系总结​](#4. 关系总结)

[​实践(Spring Boot + MyBatis)​​](#实践(Spring Boot + MyBatis))


1. @Mapper(MyBatis 注解)​

  • 作用:​

    标记在 ​Mapper 接口 上,告诉 ​MyBatis​:"这个接口需要被扫描并生成动态代理实现类"。

  • 责任方:​

    MyBatis 框架(通过 mybatis-spring整合包)。

  • 关键点:​

    • 必须配合 ​**@MapperScan** ​ 或 ​XML 配置使用,否则 MyBatis 不会扫描该接口。

    • 不涉及 Spring 容器管理​(单纯标记接口,不注册 Bean)。

    • 主要解决 ​​"MyBatis 如何识别哪些接口需要生成代理类"​​ 的问题。


2. @Repository(Spring 注解)​

  • 作用:​

    标记在 ​ 上(通常是 DAO 实现类),告诉 ​Spring​:"这是一个数据访问层组件,请注册为 Bean 并启用异常转换"。

  • 责任方:​

    Spring Framework。

  • 关键点:​

    • 属于 Spring 组件扫描的一部分(如 @ComponentScan)。

    • 自动启用 ​异常转换 ​(将 MyBatis/JDBC 异常转为 Spring DataAccessException)。

    • 主要解决 ​​"Spring 如何管理数据访问层 Bean"​​ 的问题。


3. 二者在 MyBatis 项目中的协作关系

场景 1:使用 @MapperScan(推荐)​
复制代码
@SpringBootApplication
@MapperScan("com.example.mapper") // MyBatis 扫描包
public class App { ... }
  • Mapper 接口:​

    复制代码
    // 无需加 @Repository!MyBatis 代理类已由 @MapperScan 注册为 Spring Bean
    public interface UserMapper {
        User selectById(Long id);
    }
  • 为什么不需要 @Repository?​

    @MapperScan会:

    1. 扫描指定包下的接口;

    2. 让 MyBatis 为这些接口生成动态代理类

    3. 自动将这些代理类注册为 Spring Bean ​(相当于完成了 @Repository的注册功能)。

    4. 自动启用异常转换 ​(整合包已处理,无需 @Repository)。

场景 2:单独使用 @Mapper(不推荐)​
复制代码
@Mapper // 仅标记接口,但未配置扫描
public interface UserMapper { ... }
  • 问题:​

    若未配置 @MapperScan,MyBatis 不会生成代理类,Spring 也无法注册 Bean → ​运行时报 NoSuchBeanDefinitionException

  • 解决方案:​

    必须添加 @MapperScan或在配置文件中指定 Mapper 包路径。

场景 3:同时使用 @Mapper+ @Repository(冗余)​
复制代码
@Mapper
@Repository // 多余的!
public interface UserMapper { ... }
  • 效果:​

    • @Mapper让 MyBatis 识别接口;

    • @Repository会被 Spring 扫描到,但注册的是接口本身​(不是代理类),导致注入失败。

  • 结论:​

    不要同时在 Mapper 接口上加 @Repository!​

    它既无法替代 @Mapper的代理生成功能,又会干扰 Spring 对 MyBatis 代理 Bean 的识别。


4. 关系总结

注解 框架 作用对象 主要功能 是否必需
​**@Mapper**​ MyBatis 接口 标记需生成代理的 Mapper 接口 可选(可用 @MapperScan替代)
​**@Repository**​ Spring 注册 DAO Bean + 异常转换 在 MyBatis 中不需要
​**@MapperScan**​ MyBatis-Spring 扫描接口 → 生成代理 → 注册 Bean 必需 ​(推荐替代 @Mapper

实践(Spring Boot + MyBatis)​

  1. 配置类添加 @MapperScan:​

    复制代码
    @SpringBootApplication
    @MapperScan("com.example.mapper") // 指定 Mapper 接口包
    public class App { ... }
  2. Mapper 接口无需任何注解:​

    复制代码
    // 不加 @Mapper 或 @Repository!
    public interface UserMapper {
        @Select("SELECT * FROM user WHERE id = #{id}")
        User selectById(Long id);
    }
  3. Service 层直接注入:​

    复制代码
    @Service
    public class UserService {
        @Autowired
        private UserMapper userMapper; // 注入的是 MyBatis 代理对象
    }

关键结论​:

  • ​**@Mapper是 MyBatis 的"接口标识"​** ​(可被 @MapperScan替代);

  • ​**@Repository在 MyBatis 场景下完全不需要** ​(由 @MapperScan代理注册 + 异常转换);

  • 二者不要混用,避免冲突。

相关推荐
学长毕业设计21 分钟前
基于springboot的云南中草药知识普及管理网站的设计与开发(源码+文档+讲解视频)
java·spring boot·后端
大模型码小白22 分钟前
AI 提示词专栏:使用系统指令(System Prompt)实现全局约束
java·大数据·运维·开发语言·人工智能·python·prompt
cxoptics28 分钟前
蓝宝石的轴向在窗片和偏振片中起的作用
java
wuminyu40 分钟前
Java FFM处理网络读写事件源码剖析
java·linux·c语言·jvm·c++
小江的记录本1 小时前
【AI Agent】《2026年9月 AI Agent全栈开发技术选型专项面试宝典》
java·spring boot·python·spring·ai·面试·ai编程
梦梦代码精1 小时前
《LikeShop全产品技术硬核拆解:ThinkPHP8+Vue3+UniApp架构,私有化部署与二次开发实战指南》
java·低代码·系统架构·php·开源软件
一木 之林1 小时前
C/C++ 面向对象编程(OOP)(进阶)
java·c语言·c++
摇滚侠2 小时前
《SpringBoot 3:入门与应用实战》第 9 章 使用 WebMvc 开发应用 阅读笔记 2
java·spring boot·笔记
technology_x2 小时前
液冷超充站设备厂家哪家好?2026年功率与散热对比
java·开发语言
LayZhangStrive2 小时前
Agent开发 - MCP Client使用MCP Server的几种方式(Spring AI)
java·spring ai·mcp