Mybatis源码分析:MapperMethod中内部静态类MethodSignature的作用
2019-07-22 00:47
471 查看
原文链接:http://www.cnblogs.com/zhengzuozhanglina/p/11223568.html
MethodSignature分析
MethodSignature为MapperMethod类提供了三个作用,获取待执行方法中的参数和@Param注解标注的参数名,获取标注有@MapKey的参数(@Mapkey作用在后续会讲到),方法的返回类型,获取SELECT操作时必要的标志位。以下是MethodSignature的所有属性。
- private final boolean returnsMany;//是否多值查询
- private final boolean returnsMap;//是否map查询
- private final boolean returnsVoid;//是否void查询
- private final boolean returnsCursor;//是否游标查询
- private final Class<?> returnType; //返回类型
- private final String mapKey;//获取mapKey的值
- private final Integer resultHandlerIndex;
- private final Integer rowBoundsIndex;
- private final ParamNameResolver paramNameResolver; //参数解析器
观察下列的代码,构造器首先获取返回类型,这个值通过类型参数解析器来获取入参mapperInterface本身及所有的父类和所有的接口来查找所要返回的类型,Mybatis提供了三种类型实现ParameterizedType,TypeVariable,GenericArrayType。通过返回的类型值来判定后续的SElECT将要做哪种查询,void查询,Map查询,游标查询,多值查询,这里就会给相关的属性进行打标,在mapperMethod执行exeute()方法时便会进行判断。构造器还会检查待执行的Mapper方法是否标注有@Mapkey的注解,如果有这意味着返回的结果类型是一个Map。
1 public MethodSignature(Configuration configuration, Class<?> mapperInterface, Method method) { 2 //解析返回的类型 ParameterizedType,TypeVariable,GenericArrayType 3 Type resolvedReturnType = TypeParameterResolver.resolveReturnType(method, mapperInterface); 4 if (resolvedReturnType instanceof Class<?>) { 5 this.returnType = (Class<?>) resolvedReturnType; 6 } else if (resolvedReturnType instanceof ParameterizedType) { 7 this.returnType = (Class<?>) ((ParameterizedType) resolvedReturnType).getRawType(); 8 } else { 9 this.returnType = method.getReturnType(); 10 } 11 this.returnsVoid = void.class.equals(this.returnType); 12 //是否是集合或者数组类型 13 this.returnsMany = configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray(); 14 //判断是否是游标类型 15 this.returnsCursor = Cursor.class.equals(this.returnType); 16 //获取MapKey注解 17 this.mapKey = getMapKey(method); 18 //判断mapkey是否不为NULL 19 this.returnsMap = this.mapKey != null; 20 this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class); 21 this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class); 22 //初始化参数名解析器 23 this.paramNameResolver = new ParamNameResolver(configuration, method); 24 }
转载于:https://www.cnblogs.com/zhengzuozhanglina/p/11223568.html
相关文章推荐
- mybatis源码分析之MapperMethod
- mybatis 源码学习之getMapper过程分析
- Mybatis3源码分析(19)-Mapper生成过程-示例
- Mybatis源码分析--MapperScannerConfigurer
- mybatis源码学习之执行过程分析(2)——config.xml配置文件和mapper.xml映射文件解析过程
- Mybatis3源码分析(21)-Mapper实现-动态代理
- Mybatis3源码分析(三):解析mapper的xml配置文件
- Mybatis3源码分析(二):扫描Mapper关联到spring IOC容器
- MyBatis-3.4.2-源码分析12:XML解析之mapperElement(root.evalNode("mappers"))
- Mybatis工作机制源码分析—初始化—mapper配置文件解析
- Mybatis源码分析之Mapper执行SQL过程(三)
- mybatis源码分析5 - mapper读写数据库完全解析
- Mybatis3源码分析(20)-Mapper实现-配置加载
- Mybatis3源码分析(21)-Mapper实现-动态代理
- mybatis源码学习之执行过程分析(3)——mapper接口的获取
- Mybatis Mapper接口是如何找到实现类的-源码分析
- Mybatis源码分析(一)--Mapper的动态代理
- Mybatis3源码分析(04)-加载Configuration-XMLMapperBuilder加载ResultMap
- Mybatis源码分析——Mapper与接口绑定源码分析
- Mybatis架构设计及源码分析-mapper.xml文件解析