1.简单项目:
我这里有一个简单的Springboot的Web项目,需要添加Springboot整合mybatis或者是mybatisPlus的依赖,这里我就以mybatis为例了,mybatisPlus跟mybatis是差不多的,首先添加依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
Springboot的主启动类:使用MapperScan注解配置了一个包扫描的路径。
@SpringBootApplication
@MapperScan("com.atlx")
public class Springboot2DemoApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot2DemoApplication.class, args);
}
}
如果我们不使用@MapperScan注解配置包扫描的路径的话,Springboot在启动的时候也会去扫描Mapper接口,但是扫描的路径跟Springboot启动扫描类生成BeanDefinition的路径是一样的,并且还只会去扫描加了@Mapper注解的类。但是如果我们使用了@MapperScan注解并且配置了扫描的路径的话那么Springboot就会根据我们配置的路径去扫描Mapper接口。
所以下面我们的分析就是根据有没有加@MapperScan注解来分析。
2.没有加@MapperScan注解:
如果一个Springboot项目中没有加@MapperScan注解的话,那么在扫描Mapper接口的时候Springboot回去扫描加了@Mapper注解的接口,下面我们通过源码进行分析:
首先在idea中找到Springboot整合mybatis的依赖包:

找到里

最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



