当前位置:网站首页>@Mapper和@Repository的使用和区别
@Mapper和@Repository的使用和区别
2022-04-22 07:45:00 【机械提桶跑路ing】
两个注解的意思,它们之间的区别和联系。
① @Mapper是MyBatis的注解,@Repository是Spring中的注解,这些注解就是声明一个Bean。
② @Mapper注解不需要在SpringBoot启动类上配置扫描类;通过xml里面的namespace里面的接口地址,生成bean对象后注入到Service里面。
在连接数据库的接口上添加@Mapper,然后在xml中的namespace指向mapper,会生成一个动态bean,然后在servcieImpl进行动态注入。
如果不在dao的接口上添加@Mapper,可以在启动类上添加@MapperScan(""),并且指明扫描的位置
@Autowired
private Mapper mapper;
没有在Mapper类上添加注解@Mapper 名字mapper会出现波浪线
// 指定启动类
@SpringBootApplication
// Mapper映射的扫描注释,指定扫描位置
@MapperScan("com.lv.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
③ @Repository需要配置扫描地址,然后dao层生成的bean才能被注入到Service层进行使用.
如果在dao层中加了@Repository,在spring的管理下,生成bean,在serviceImpl添加
@Autowired
private Mapper mapper;
版权声明
本文为[机械提桶跑路ing]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_46703961/article/details/122016539
边栏推荐
- Spark SQL gets the element at an index of the array
- 第1关:节点监听机制
- Lambda 表达式
- Machine learning notes - Mathematics in principal component analysis
- sql查询去除空数据和null 字段
- Installation de MySQL par CentOS
- leaflet、cesium加载百度地图,加载自定义样式百度地图
- Single page application
- Monkey eating peach problem (loop, recursion)
- 构造函数与toString
猜你喜欢

el-input输入框输入无效,且Error in data(): “ReferenceError: el is not defined“

PCIe学习-PCIe总线怎样做到在软件上兼容PCI总线(七)

redis 简单使用

OLED显示驱动

94. Middle order traversal of binary tree (easy)
![[Dahua cloud native] micro service chapter - service mode of five-star hotels](/img/fc/be18f39e4aa653efa8603ab63ff3f1.png)
[Dahua cloud native] micro service chapter - service mode of five-star hotels

CentOS 安装 MySQL

Cesium realizes the monomer of buildings (divided into buildings and layers)

微信小程序:TypeError: Cannot read property ‘mark’ of undefined

leaflet、cesium加载百度地图,加载自定义样式百度地图
随机推荐
C的动态内存管理
第2关:多态
PCIe学习-PCIe总线体系结构入门:事务层-数据链路层-物理层(八)
Pointer and string
oracle 数据库常识以及使用
Client server communication project 2
字符串的替换
猴子吃桃子问题(循环、递归)
Level 2: create, list and delete child nodes
Cesium realizes the monomer of buildings (divided into buildings and layers)
指针和字符串
94. Middle order traversal of binary tree (easy)
函数指针和指针函数
Machine learning notes - Mathematics in principal component analysis
使用art-template继承时CompileError: Invalid or unexpected token generated
Level 1: Inheritance
Linked list printing (linked list reverse output)
1315. 祖父节点值为偶数的节点和(Medium)
Redis is easy to use
Seven crimes of hackers in social engineering -- greed (Ping of death)