当前位置:网站首页>@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
边栏推荐
猜你喜欢
Installation, use and problem summary of binlog2sql tool
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
Reflection on the performance of some OpenGL operations in the past
5 minutes to achieve wechat cloud applet payment function (including source code)
[transfer] summary of new features of js-es6 (one picture)
Wechat applet part of the mobile phone Preview PDF did not respond
Virtual machine performance monitoring and fault handling tools
MySQL syntax collation (3)
优先使用组合而不使用继承
First experience of using fluent canvas
随机推荐
Data analysis learning directory
RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out`
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
openlayers 5.0 热力图
SQL of contention for system time plus time in ocrale database
Installation, use and problem summary of binlog2sql tool
Transaction processing of SQL Server database
Thoughts on the optimization of examination papers in the examination system
SQL server requires to query the information of all employees with surname 'Wang'
Class loading process of JVM
Web Security
深度学习——特征工程小总结
Openharmony open source developer growth plan, looking for new open source forces that change the world!
Modify the font size of hint in editext
LPC1768 关于延时Delay时间与不同等级的优化对比
arcMap 发布切片服务
JS to get the local IP address
What is a message queue
A brief explanation of golang's keyword "competence"
openlayers 5.0 加载arcgis server 切片服务