当前位置:网站首页>什么时候用@ComponentScan?与@MapperScan有什么区别?
什么时候用@ComponentScan?与@MapperScan有什么区别?
2022-06-10 04:09:00 【面向鸿蒙编程】
@ComponentScan与@MapperScan有什么区别?
什么时候用@ComponentScan?
@ComponentScan是SpringBoot的注解,如其意“Sacn”是扫描的意思。
SpringBoot在没配置@ComponentScan的情况下,默认只扫描和主类处于同包下的Class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//@ComponentScan(basePackages = "com.自定义.mall")如果不加,只扫描和主类处于同包下的Class!!!
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}例如下述情况:
不加@ComponentScan可用的范围
必须要加的情况
SpringBoot的启动类GunRestApplication 在restfulapi包下,但是存在一个与restfulapi包同级的modular包也想被扫描到就必须要使用。
@ComponentScan与@MapperScan有什么区别?
首先要搞清楚它们两个分别是干什么的
(1)显然两者都有Scan——“扫描”,@MapperScan和@ComponentScan都是扫描包
(2)@ComponentScan是组件扫描注解,用来扫描@Controller @Service @Repository这类,主要就是定义扫描的路径从中找出标志了需要装配的类到Spring容器中
(3)@MapperScan 是扫描mapper类的注解,就不用在每个dao层的各个类上加@Mapper了
举一个例子:
如下是我的SpringBoot项目,里面使用了MyBatisPlus,用了@MapperScan

dao包下的BookDao
//@Mapper
@Repository
public interface BookDao extends BaseMapper<Book> {
}使用@MapperScan有两种方式
可以在启动类上标注
@SpringBootApplication
@MapperScan("com.harmony.dao")
public class SSMPApplication {
public static void main(String[] args) {
// 启动springboot时,断开读取外部临时配置对应的入口,也就是去掉读取外部参数的形参
// SpringApplication.run(SSMPApplication.class);
SpringApplication.run(SSMPApplication.class, args);
}
}或者在配置类MPConfig类使用
@Configuration
@MapperScan("com.harmony.dao") // 使用该注解,在dao层的各个类中就不用添加 @Mapper
public class MPConfig {
// 第三方bean的配置方式
@Bean
// 想使用MybatisPlus提供的分页,必须使用MP的拦截器
public MybatisPlusInterceptor mybatisPlusInterceptor() {
// 定义Mp拦截器的壳
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 添加具体的拦截器 PaginationInnerInterceptor -> 分页
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}上述两种效果是一样的!!!
边栏推荐
- Is the online account opening channel reliable? Is it safe?
- 5- common tool management
- Business card wechat applet error version 2
- [enlightenment -18]: Confucianism, Buddhism and Taoism, different life stages, different choices
- [understanding of opportunity -20]: Guiguzi - Manoeuvring chapter - one opening and one closing, one movement and one relaxation are called manoeuvring, which can be used in the workplace.
- [adaptive motion compensation] FPGA based adaptive motion compensation video image enhancement system
- MySQL - data type
- 【图像检测-边缘检测】基于PCNN实现图像边缘提取附matlab代码
- QT中的QProgressBar+QPushButton+QMainwindow+QTmer+布局管理器+QTextCodec+QTextToSpeech
- MySQL——数据类型
猜你喜欢
![[pytorch modifies the pre training model: there is little difference between the measured loading pre training model and the random initialization of the model]](/img/ad/b96e9319212cf2724e0a640109665d.png)
[pytorch modifies the pre training model: there is little difference between the measured loading pre training model and the random initialization of the model]

4-镜像地址
![[semi supervised classification] semi supervised web page classification based on K-means and label+propagation](/img/3c/d709544542d3db8b28263f87f42f7e.png)
[semi supervised classification] semi supervised web page classification based on K-means and label+propagation

Leetcode stretch Weekly 296

[singleshotmultiboxdetector (SSD)

Some vulnerabilities and testing methods of security testing

Rest-Assure框架最全使用教程

Open vs code from current folder
![[science and technology specialty-3]: how many students with science and technology specialty know about the senior high school entrance examination](/img/a6/f0a2f8dafffa28d2b2d2cd0431b739.jpg)
[science and technology specialty-3]: how many students with science and technology specialty know about the senior high school entrance examination

Assembly: assembly instruction classification
随机推荐
用80%的图表满足日常工作,用剩下20%的图表建立核心竞争力!
SSTI(模板注入) ——(7)
【分库分表三部曲精华】
Mengxiaofeng, a Chinese encryption artist, participated in the "liquid alloy" meta universe painting exhibition held in Italy
在Vim/Vi中查找和替换,从网上遇到的好文章
[机缘参悟-20]:鬼谷子-捭阖篇-一开一合,一动一静,一张一弛谓之捭阖,职场应用。
OpenAI新博客:训练大模型的技术
SSTI (template injection) - (7)
[机缘参悟-22]:鬼谷子-闭藏之术,适时收藏自己的锋芒,在职场、商务、商业活动中保护自己
[机缘参悟-19]:鬼谷子-捭阖篇-阴阳即矛盾,是万事万物的基本属性,也是鬼谷子哲学的根基之一
I wrote getter and setter methods every day. I was impatient, so I used the artifact mapperstruct, and the crud efficiency was suddenly improved! (hard coding is preferred)
Rest-Assure框架最全使用教程
[从零开始学习FPGA编程-13]:快速入门篇 - 操作步骤3(功能仿真)-2-Mentor HDL仿真工具modelsim工具的简介、功能仿真的基本原理
从当前文件夹打开 VS Code
MySQL learning
描述符就绪条件
Pytorch CPU/GPU 安装方法。
SSTI(模板注入) ——(7)
[从零开始学习FPGA编程-14]:快速入门篇 - 操作步骤3(功能仿真)-3-modelsim快速使用入门(8bits循环计数器)
使用luks2对ceph rbd进行加密