MapperScan和ComponentScan同时使用问题
@MapperScan:
1.首先了解@Mapper
在接口上添加了@Mapper,在编译之后就会生成相应的接口实现类。
不过需要在每个接口上面进行配置,为了简化开发,就有了 @MapperScan。
@MapperScan:
指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类。
@ComponentScan:
会自动扫描包路径下面的所有@Controller、@Service、@Repository、@Component 的类,并把符合扫描规则的类装配到spring容器中。
@MapperScan和@ComponentScan可以同时使用。
如果都是扫描的相同路径时,对于同一个接口,可能就会出现识别错误。比如
在springBoot项目的Application上面定义了
@MapperScan(basePackages = { "com" })
@SpringBootApplication
@SpringBootApplication包含了@ComponentScan。
在此项目下面有一个接口是com.xxInf,实现是com.xxImpl在实现类上面通过@Service加入spring容器中
我们在注入的时候接口时
@Autowired
private com.xxInf xx;
可能识别的不是xxImpl,而去mybatis里面通过反射找绑定,这样就会出现BindingException错误
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
所以在设计项目结构的时候要把mapper放到一个合适的位置,通过设置MapperScan的路径basePackages 好避免这种冲突
MapperScan和ComponentScan同时使用问题的更多相关文章
- @MapperScan 和 @ComponentScan 区别
1.首先@MapperScan和@ComponentScan都是扫描包 2.@ComponentScan是组件扫描注解,用来扫描@Controller @Service @Repository这类 ...
- @MapperScan和@ComponentScan的区别
区别 今天在撸SpringBoot的时候,突然对注解产生了混淆,@MapperScan和@ComponentScan都是扫描包,二者之间有什么区别呢? 首先,@ComponentScan是组件扫描注解 ...
- Spring Boot 报错记录
Spring Boot 报错记录 由于新建的项目没有配置数据库连接启动报错,可以通过取消自动数据源自动配置来解决 解决方案1: @SpringBootApplication(exclude = Dat ...
- springboot 注册dao层 service 层
可以使用三种注解来引入DAO层的接口到spring容器中.1.@Mapper,写在每一个DAO层接口上,如下: 2.@MapperScan和@ComponentScan两者之一.前者的意义是将指定包中 ...
- springboot 工程启动报错之Consider defining a bean of type ‘XXX’ in your configuration.
一.前言: 使用springboot自动注入的方式搭建好了工程,结果启动的时候报错了!!!,错误如下图: Description: Field userEntityMapper in com.xxx. ...
- 「小程序JAVA实战」springboot的后台搭建(31)
转自:https://idig8.com/2018/08/29/xiaochengxujavashizhanspringbootdehoutaidajian31/ 根据下面的图,我们来建立下对应的sp ...
- spring mvc改造成spring boot
一.新增依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- 聊聊、Spring ServletContainerInitializer
我们平时用 Java 注解很多,例如 @Configuration.@Component.@Service,我们习惯于通过 XML 方式来实现 Web,而用 Java 注解方式来实现 Web 却很少. ...
- springboot 启动报错Consider defining a bean of type 'com.example.springbootdruid.mapper.UserMapper' in your configurati
一.问题 springboot项目启动时报错: Field userMapper in com.example.springbootdruid.service.impl.UserServiceImpl ...
随机推荐
- ubuntu 18.04启动samba图形管理界面
启动samba图形界面管理器出现错误: Failed to load module "canberra-gtk-module" 或 SystemError: could not o ...
- git和github的第一次接触
我的github的helloworld链接: https://github.com/xuziqian111/hello-world/blob/master/helloworld.java 我的gith ...
- 类 __getitem__ __getattr__ __call__
__getitem__ 实例虽然能作用于for循环,看起来和list有点像,但是,把它当成list来使用还是不行,要表现得像list那样按照下标取出元素,需要实现__getitem__()方法 __g ...
- 转载 * jQuery实现动态分割div—通过拖动分隔栏实现上下、左右动态改变左右、上下两个相邻div的大小
由jQuery实现上下.左右动态改变左右.上下两个div的大小,需要自己引入jquery1.8.0.min.js包 可用于页面布局. //============================ind ...
- Cookie Manager
https://github.com/Rob--W/cookie-manager 修改饼干获取VIP标识
- 页面添加锚点后如何点击不改变URL?
直接奔主题,前端简单地锚点实现方法大家都会,无非就是在把 a 标签的 href 写成想要跳到的元素的id ,比如点击 <a href="#box"></a> ...
- [LeetCode&Python] Problem 703. Kth Largest Element in a Stream
Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...
- anaconda 的安装
进官网下载anaconda, 根据需要下载对应python版本Anaconda软件. https://www.anaconda.com/download/#windows 下载完双击 Anaconda ...
- 利用树莓派来安装opencv从而来调动摄像头工作(没有坑,超超自己试过)
超超最近参加了学校里一位特别厉害的老师讲的课(两天,我就从一个小白然后了解了树莓派以及Arduino这些我之前都没有了解过的东西,由于结课的需要,我们需要自己设计一个创意以及完成作品)所以才有了这篇文 ...
- python学习笔记(1)python中的注释和安装python
注释 目标 注释的作用 单行注释 多行注释 01注释的作用 在程序中对代码的标注说明,增强代码的可读性 以 # 开头,# 右边的所有东西都被当做说明文字,而不是真正要执行的程序,只起到辅助说明作用 为 ...