关于@MapperScan包扫描的坑及解决
作者:Toor 时间:2023-02-13 02:45:46
@MapperScan包扫描的坑
在使用通用mapper执行查询时,由于不太注意顺手就导了spring的包:
import org.mybatis.spring.annotation.MapperScan;
结果就异常:
tk.mybatis.mapper.provider.base.BaseSelectProvider:xxxx
找了半天才发现是包的问题,应该导mybatis的MapperScan而不是spring中的包,正确的包名:
import tk.mybatis.spring.annotation.MapperScan;
手写一个@MapperScan扫描器
@MapperScan
1.@MapperScan这个注解是由MyBatis提供的;
2.只能使用在类上;
3.主要功能是扫描到指定包下接口的生成Class对象
注解使用在类上,指定value的值可以指定扫描的包,把扫描到的包中的接口,生成 * 注入到Spring的ioc容器中;
自己手写该注解的思路
1.需要使用一个类,把使用这个注解的类加载加载(把Class对象注册进来);
2.解析这个类上是否有@MapperScan注解;
3.然后解析@MapperScan注解上的value值得到接口的路径;
4.根据路径扫描有哪些接口;
5.根据接口名和classpath路径,把接口加载进来;
6.把接口的class对象保存在List中;
#7.接下来就是Mybatis框架生成 * ;
注解:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface BeanScan {
String value();
}
//生成MapperScan这个类的处理逻辑
public class MapperScan {
//通过解析BeanScan来得到文件目录
Class<BeanScan> beanScanClass;
/**
*Class这个参数是使用@MapperScan这个类的class对象
*/
public ArrayList<Class> scan(Class aClass) throws ClassNotFoundException {
//创建一个ArrayList存放生成的接口的class对象
ArrayList<Class> mapperName = new ArrayList<>();
//解析传入的aclass对象得到@MapperSCan这个注解
//BeanScan这个我们定义的@mapperScan注解
BeanScan BeanScan = (com.scan.BeanScan) aClass.getDeclaredAnnotation(BeanScan.class);
//通过BeanScan对象的到接口的路径
String path = BeanScan.value();
//获得当前的类加载器(可以用类加载器得到classpath,然后使用File对象操作文件)
ClassLoader classLoader = aClass.getClassLoader();
//接口的路径是"."转换成"\"
String replacePath = path.replace(".", "\\");
//通过类加载器获取当前文件的绝对路径
URL resource = classLoader.getResource(replacePath);
//通过文件的绝对路径把文件编程File对象
String file = resource.getFile();
File file1 = new File(file);
//判断File对象是否是目录
if(file1.isDirectory()){
//把File对象下的文件名称取出来
File[] files = file1.listFiles();
for (File f :files) {
//字符串拼接操作(把字符串拼接成系统类加载器可以加载的格式)
String name1 = file1.getName();
String name = f.getName();
String pathName = name1+"."+name;
String substring = pathName.substring(0, pathName.indexOf(".class"));
//把接口的全限定名称传入生成class对象,放入到list集合中
Class<?> aClass1 = ClassLoader.loadClass(substring);
mapperName.add(aClass1);
System.out.println(aClass1);
}
}
return mapperName;
}
}
#有了这个注解后,我们就不用手动去传入接口的class对象来生成 *
只需要在指定的文件下在创建Mapper接口,系统会自动的去扫描;
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
来源:https://blog.csdn.net/lamar_quin/article/details/85240294
标签:@MapperScan,扫描
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Eclipse+Java+Swing实现斗地主游戏(代码)
2023-08-17 18:10:40
![](https://img.aspxhome.com/file/2023/2/113452_0s.png)
SpringCloud如何创建一个服务提供者provider
2023-08-01 01:56:33
![](https://img.aspxhome.com/file/2023/3/57813_0s.png)
SpringBoot SSO轻松实现(附demo)
2022-04-05 02:24:33
java 将一个数组逆序输出的方法
2023-05-09 09:29:01
C#使用Task实现异步方法
2022-09-02 20:26:53
![](https://img.aspxhome.com/file/2023/6/70436_0s.jpg)
详解Android性能优化之启动优化
2023-07-16 06:00:07
![](https://img.aspxhome.com/file/2023/1/104651_0s.jpg)
C#开发WinForm之DataGridView开发详解
2023-06-25 06:31:35
![](https://img.aspxhome.com/file/2023/7/83257_0s.png)
Android学习笔记之蓝牙功能
2022-05-19 05:07:44
![](https://img.aspxhome.com/file/2023/4/101874_0s.jpg)
详解C#扩展方法原理及其使用
2022-12-20 03:55:22
![](https://img.aspxhome.com/file/2023/4/87054_0s.png)
java(包括springboot)读取resources下文件方式实现
2021-06-03 20:16:06
![](https://img.aspxhome.com/file/2023/4/68184_0s.png)
Java Swing编写一个简单的计算器软件
2023-08-12 06:24:04
![](https://img.aspxhome.com/file/2023/3/57953_0s.png)
android实现蓝牙文件发送的实例代码,支持多种机型
2023-08-17 11:07:56
![](https://img.aspxhome.com/file/2023/2/128962_0s.png)
springmvc处理模型数据Map过程解析
2022-04-24 01:06:01
![](https://img.aspxhome.com/file/2023/8/129278_0s.png)
springboot实现公众号接收回复消息和超过5秒被动回复消息
2022-10-16 04:40:48
![](https://img.aspxhome.com/file/2023/4/119194_0s.jpg)
java实现学生成绩档案管理系统
2023-06-29 22:48:43
spring mvc中直接注入的HttpServletRequst安全吗
2021-12-29 07:48:16
![](https://img.aspxhome.com/file/2023/6/63506_0s.png)
Struts中使用validate()输入校验方法详解
2023-03-02 19:10:41
![](https://img.aspxhome.com/file/2023/4/63744_0s.png)
Flutter实现底部和顶部导航栏
2021-10-26 20:15:53
![](https://img.aspxhome.com/file/2023/5/130445_0s.jpg)
JAVA并发中VOLATILE关键字的神奇之处详解
2021-08-15 01:51:38
Ubuntu16.04下配置VScode的C/C++开发环境
2023-10-22 19:21:17
![](https://img.aspxhome.com/file/2023/6/123856_0s.jpg)