当前位置:网站首页>@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
边栏推荐
- Garbage collector and memory allocation strategy
- 浅谈c语言指针的强制转换
- openlayers draw矩形
- Why is PostgreSQL about to surpass SQL Server?
- Wechat applet part of the mobile phone Preview PDF did not respond
- Codeforces Round #784 (Div. 4)
- Application of DCT transform
- Zero base to build profit taking away CPS platform official account
- Kubernetes入门到精通-在 Kubernetes 上安装 OpenELB
- 命令-sudo
猜你喜欢
优先使用组合而不使用继承
Grafana 分享带可变参数的链接
RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out`
First experience of using fluent canvas
MySQL syntax collation (4)
Wechat applet part of the mobile phone Preview PDF did not respond
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
Audio signal processing and coding - 2.5.3 the discrete cosine transform
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
Garbage collector and memory allocation strategy
随机推荐
Main differences between go and PHP
Oracle configuration st_ geometry
Virtual machine performance monitoring and fault handling tools
Go three ways to copy files
Redis core technology and practice 1 - start with building a simple key value database simplekv
Some speculation about the decline of adults' language learning ability
Codeforces Round #784 (Div. 4)
Speculation on the way to realize the smooth drag preview of video editing software
机器学习目录
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
2021-2022-2 ACM training team weekly Programming Competition (8) problem solution
One stop service platform for high-level talents and development of comprehensive service platform system for talents
Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)
openlayers 5.0 当地图容器大小改变时,重新加载地图
SQL常用的命令
Kubernetes入门到精通-裸机LoadBalence 80 443 端口暴露注意事项
DevOps集成-Jenkins 服务的环境变量和构建工具 Tools
Gossip: on greed
ArcMap连接 arcgis server
SQL of contention for system time plus time in ocrale database