您的位置:首页 > 移动开发

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: