当前位置:网站首页>@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
边栏推荐
- @Analysis of conditional on Web Application
- Thoughts on the optimization of examination papers in the examination system
- static类变量快速入门
- The platinum library cannot search the debug process records of some projection devices
- A brief explanation of golang's keyword "competence"
- MySQL syntax collation (4)
- js获取本机ip地址
- Codeforces Round #784 (Div. 4)
- Kubernetes入门到精通-KtConnect(全称Kubernetes Toolkit Connect)是一款基于Kubernetes环境用于提高本地测试联调效率的小工具。
- C1000k TCP connection upper limit test 1
猜你喜欢
An algorithm problem was encountered during the interview_ Find the mirrored word pairs in the dictionary
arcMap 发布切片服务
FTP, SSH Remote Access and control
Kubernetes入门到精通-裸机LoadBalence 80 443 端口暴露注意事项
Intuitive understanding of the essence of two-dimensional rotation
SQL常用的命令
Matlab 2019 installation of deep learning toolbox model for googlenet network
RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out`
Zero base to build profit taking away CPS platform official account
Installation, use and problem summary of binlog2sql tool
随机推荐
坐标转换WGS-84 转 GCJ-02 和 GCJ-02转WGS-84
MySQL syntax collation (4)
Class loading mechanism
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
Common processing of point cloud dataset
ArcMap connecting ArcGIS Server
All table queries and comment description queries of SQL Server
Build intelligent garbage classification applet based on Zero
TI DSP的 FFT与IFFT库函数的使用测试
Thoughts on the optimization of examination papers in the examination system
Gossip: on greed
JS controls the file type and size when uploading files
MySQL syntax collation (2)
SQL server requires to query the information of all employees with surname 'Wang'
Why is PostgreSQL about to surpass SQL Server?
RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out`
Strange problems in FrameLayout view hierarchy
What is a message queue
Go modules daily use
Keysight has chosen what equipment to buy for you