当前位置:网站首页>@MapperScan与@Mapper
@MapperScan与@Mapper
2022-04-23 19:24:00 【justry_deng】
@MapperScan与@Mapper
背景说明
我们在编写mapper时,只需要编写接口而不需要对其实现,由Mybatis框架对接口提供对应的代理实现类(,并将代理实现类注册进容器中)。但是Mybatis是怎么知道需要对哪些接口进行代理实现呢,就是通过@MapperScan与@Mapper注解来知道的。
@MapperScan与@Mapper的作用
使Mybatis知道需要对哪些接口提供代理实现。
通过@Mapper让Mybatis对接口提供代理实现
声明:不同版本mybatis代理类生成机制可能存在差异,以下结论
@MapperScan基于org.mybatis:mybatis-spring:2.0.6,@Mapper基于org.mybatis:mybatis:3.5.9
-
在目标接口上使用@Mapper注解进行标识
-
需保证该接口源码必须在当前项目下
即:该接口必须是当前项目的源码接口,不能是其它依赖类库中的,否者@Mapper不会生效。
注:有人可能会以为使用@ComponentScan将其它依赖类库中的mapper导入就可以了。其实是不可以的,@ComponentScan扫描的是被@Component等注解标注了的类并注册进Spring容器,与Mybatis扫描生成代理类的识别机制是两个概念。
注:如果需要让Mybatis对其它依赖类库中的mapper接口也生成代理类,那么请使用@MapperScan
通过@MapperScan让Mybatis对接口提供代理实现
声明:不同版本mybatis代理类生成机制可能存在差异,以下结论
@MapperScan基于org.mybatis:mybatis-spring:2.0.6,@Mapper基于org.mybatis:mybatis:3.5.9
-
只需要指定要范围即可。Mybatis会对该范围下的所有接口进行对应的代理实现(,并将代理实现类注册进容器中)。
注:范围既可以包含当前项目内的范围,也可以包含当前项目所依赖的类库的范围。
注:@MapperScan指定范围下的所有接口,是所有接口,不论是mapper接口、还是service接口、或者是其它什么接口,只要接口是在@MapperScan指定的范围内,Mybatis都会对该接口进行对应的代理实现(,并将代理实现类注册进容器中)。所以在使用@MapperScan时,一定要注意指定的范围不能过大。避免以下情况的发生:

相关资料
- 本文已被收录进《程序员成长笔记》 ,笔者JustryDeng
版权声明
本文为[justry_deng]所创,转载请带上原文链接,感谢
https://blog.csdn.net/justry_deng/article/details/124227444
边栏推荐
- C6748 软件仿真和硬件测试 ---附详细FFT硬件测量时间
- Strange problems in FrameLayout view hierarchy
- Codeforces Round #784 (Div. 4)
- SSDB foundation 1
- MySQL syntax collation (2)
- Problems caused by flutter initialroute and home
- @Analysis of conditional on Web Application
- binlog2sql 工具安装使用及问题汇总
- Web Security
- Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)
猜你喜欢

Pdf reference learning notes

An algorithm problem was encountered during the interview_ Find the mirrored word pairs in the dictionary

Using oes texture + glsurfaceview + JNI to realize player picture processing based on OpenGL es

Oracle配置st_geometry

MySQL syntax collation (5) -- functions, stored procedures and triggers

An idea of rendering pipeline based on FBO

MySQL syntax collation (2)

MySQL lock

The platinum library cannot search the debug process records of some projection devices

Garbage collector and memory allocation strategy
随机推荐
浅谈c语言指针的强制转换
MFC获取本机IP(网络通讯时用得多)
Class loading process of JVM
JVM的类加载过程
Zero base to build profit taking away CPS platform official account
Decompile and get the source code of any wechat applet - just read this (latest)
White screen processing method of fulter startup page
Openharmony open source developer growth plan, looking for new open source forces that change the world!
Codeforces Round #784 (Div. 4)
Web Security
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
[record] typeerror: this getOptions is not a function
Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)
Hot reload debugging
Problems caused by flutter initialroute and home
深度学习环境搭建步骤—gpu
LPC1768 关于延时Delay时间与不同等级的优化对比
Switching power supply design sharing and power supply design skills diagram
Grafana 分享带可变参数的链接
HTTP cache - HTTP authoritative guide Chapter VII