首页 > 移动平台 > 详细

Mybatis源码分析:MapperMethod中内部静态类MethodSignature的作用

时间:2019-07-22 10:16:24      阅读:77      评论:0      收藏:0      [点我收藏+]

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     }

 

Mybatis源码分析:MapperMethod中内部静态类MethodSignature的作用

原文:https://www.cnblogs.com/zhengzuozhanglina/p/11223568.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!