Skip to content

Latest commit

 

History

History
181 lines (162 loc) · 5.58 KB

Mybatis-MethodSignature.md

File metadata and controls

181 lines (162 loc) · 5.58 KB

MethodSignature

  • Author: HuiFer
  • Description: 该文介绍 mybatis MethodSignature 类
  • 源码阅读工程: SourceHot-Mybatis
  • org.apache.ibatis.binding.MapperMethod.MethodSignature
/** * 方法签名 */publicstaticclassMethodSignature { /** * 返回值是否多个 */privatefinalbooleanreturnsMany; /** * 返回值是不是map */privatefinalbooleanreturnsMap; /** * 返回值是否 void */privatefinalbooleanreturnsVoid; /** * 返回的是否是一个游标 */privatefinalbooleanreturnsCursor; /** * 返回值是否是 optional */privatefinalbooleanreturnsOptional; /** * 返回类型 */privatefinalClass<?> returnType; /** * map key */privatefinalStringmapKey; privatefinalIntegerresultHandlerIndex; privatefinalIntegerrowBoundsIndex; /** * 参数解析 */privatefinalParamNameResolverparamNameResolver; publicMethodSignature(Configurationconfiguration, Class<?> mapperInterface, Methodmethod) { TyperesolvedReturnType = TypeParameterResolver.resolveReturnType(method, mapperInterface); if (resolvedReturnTypeinstanceofClass<?>) { this.returnType = (Class<?>) resolvedReturnType; } elseif (resolvedReturnTypeinstanceofParameterizedType) { this.returnType = (Class<?>) ((ParameterizedType) resolvedReturnType).getRawType(); } else { this.returnType = method.getReturnType(); } this.returnsVoid = void.class.equals(this.returnType); this.returnsMany = configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray(); this.returnsCursor = Cursor.class.equals(this.returnType); this.returnsOptional = Optional.class.equals(this.returnType); this.mapKey = getMapKey(method); this.returnsMap = this.mapKey != null; this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class); this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class); this.paramNameResolver = newParamNameResolver(configuration, method); } /** * 方法主要是把方法参数转换为SQL命令参数。 * * @param args * @return */publicObjectconvertArgsToSqlCommandParam(Object[] args) { returnparamNameResolver.getNamedParams(args); } /** * 是否有 {@link RowBounds} * * @return */publicbooleanhasRowBounds() { returnrowBoundsIndex != null; } publicRowBoundsextractRowBounds(Object[] args) { returnhasRowBounds() ? (RowBounds) args[rowBoundsIndex] : null; } /** * 是否有 resultHandler * * @return */publicbooleanhasResultHandler() { returnresultHandlerIndex != null; } publicResultHandlerextractResultHandler(Object[] args) { returnhasResultHandler() ? (ResultHandler) args[resultHandlerIndex] : null; } publicStringgetMapKey() { returnmapKey; } publicClass<?> getReturnType() { returnreturnType; } publicbooleanreturnsMany() { returnreturnsMany; } publicbooleanreturnsMap() { returnreturnsMap; } publicbooleanreturnsVoid() { returnreturnsVoid; } publicbooleanreturnsCursor() { returnreturnsCursor; } /** * return whether return type is {@code java.util.Optional}. * * @return return {@code true}, if return type is {@code java.util.Optional} * @since 3.5.0 */publicbooleanreturnsOptional() { returnreturnsOptional; } /** * 获取参数名 * {@link RowBounds} * * @param method mapper 方法 * @param paramType * @return */privateIntegergetUniqueParamIndex(Methodmethod, Class<?> paramType) { Integerindex = null; // 获取参数类型finalClass<?>[] argTypes = method.getParameterTypes(); for (inti = 0; i < argTypes.length; i++) { if (paramType.isAssignableFrom(argTypes[i])) { if (index == null) { index = i; } else { thrownewBindingException(method.getName() + " cannot have multiple " + paramType.getSimpleName() + " parameters"); } } } returnindex; } /** * 获取 {@link MapKey} 注解数据 * * @param method * @return */privateStringgetMapKey(Methodmethod) { StringmapKey = null; if (Map.class.isAssignableFrom(method.getReturnType())) { finalMapKeymapKeyAnnotation = method.getAnnotation(MapKey.class); if (mapKeyAnnotation != null) { mapKey = mapKeyAnnotation.value(); } } returnmapKey; } }
close