@MapperScan和@ComponentScan

这两天新开一个springboot + mybatis项目,启动时报错提示


***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.xxx.xxx.wechat.service.impl.WeChatServiceImpl required a bean of type 'com.xxx.xxx.wechat.dao.WeChatDao' that could not be found.


Action:

Consider defining a bean of type 'com.xxx.xxx.wechat.dao.WeChatDao' in your configuration.


Process finished with exit code 1

dao代码如下


WeChatDao

启动类代码如下


启动类

这妥妥的不科学啊,在同一个路径包下的其它service和controller都扫描注册成功了,独独@Repository失败。后经研究发现,使用@MapperScan代替@ComponentScan就能解决这个问题。


解决后

他们的区别如下:
@ComponentScan是组件扫描注解,用来扫描@Controller @Service @Repository @Component这类,主要就是从扫描路径中找出标志了需要装配的类到Spring容器中

@MapperScan 是扫描mapper类的注解

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容