diff --git a/docs/Mybatis/Mybatis-MapperMethod.md b/docs/Mybatis/HuiFer-Mybatis-MapperMethod.md similarity index 100% rename from docs/Mybatis/Mybatis-MapperMethod.md rename to docs/Mybatis/HuiFer-Mybatis-MapperMethod.md diff --git a/docs/Mybatis/HuiFer-Mybatis-MethodSignature.md b/docs/Mybatis/HuiFer-Mybatis-MethodSignature.md new file mode 100644 index 0000000..fa8f8be --- /dev/null +++ b/docs/Mybatis/HuiFer-Mybatis-MethodSignature.md @@ -0,0 +1,176 @@ +# MethodSignature +- `org.apache.ibatis.binding.MapperMethod.MethodSignature` +```java + /** + * 方法签名 + */ + public static class MethodSignature { + + /** + * 返回值是否多个 + */ + private final boolean returnsMany; + /** + * 返回值是不是map + */ + private final boolean returnsMap; + /** + * 返回值是否 void + */ + private final boolean returnsVoid; + /** + * 返回的是否是一个游标 + */ + private final boolean returnsCursor; + /** + * 返回值是否是 optional + */ + private final boolean returnsOptional; + /** + * 返回类型 + */ + private final Class returnType; + /** + * map key + */ + private final String mapKey; + private final Integer resultHandlerIndex; + private final Integer rowBoundsIndex; + /** + * 参数解析 + */ + private final ParamNameResolver paramNameResolver; + + public MethodSignature(Configuration configuration, Class mapperInterface, Method method) { + Type resolvedReturnType = TypeParameterResolver.resolveReturnType(method, mapperInterface); + if (resolvedReturnType instanceof Class) { + this.returnType = (Class) resolvedReturnType; + } else if (resolvedReturnType instanceof ParameterizedType) { + 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 = new ParamNameResolver(configuration, method); + } + + /** + * 方法主要是把方法参数转换为SQL命令参数。 + * + * @param args + * @return + */ + public Object convertArgsToSqlCommandParam(Object[] args) { + return paramNameResolver.getNamedParams(args); + } + + /** + * 是否有 {@link RowBounds} + * + * @return + */ + public boolean hasRowBounds() { + return rowBoundsIndex != null; + } + + public RowBounds extractRowBounds(Object[] args) { + return hasRowBounds() ? (RowBounds) args[rowBoundsIndex] : null; + } + + /** + * 是否uresultHandler + * + * @return + */ + public boolean hasResultHandler() { + return resultHandlerIndex != null; + } + + public ResultHandler extractResultHandler(Object[] args) { + return hasResultHandler() ? (ResultHandler) args[resultHandlerIndex] : null; + } + + public String getMapKey() { + return mapKey; + } + + public Class getReturnType() { + return returnType; + } + + public boolean returnsMany() { + return returnsMany; + } + + public boolean returnsMap() { + return returnsMap; + } + + public boolean returnsVoid() { + return returnsVoid; + } + + public boolean returnsCursor() { + return returnsCursor; + } + + /** + * 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 + */ + public boolean returnsOptional() { + return returnsOptional; + } + + /** + * 获取参数名 + * {@link RowBounds} + * + * @param method mapper 方法 + * @param paramType + * @return + */ + private Integer getUniqueParamIndex(Method method, Class paramType) { + Integer index = null; + // 获取参数类型 + final Class[] argTypes = method.getParameterTypes(); + for (int i = 0; i < argTypes.length; i++) { + if (paramType.isAssignableFrom(argTypes[i])) { + if (index == null) { + index = i; + } else { + throw new BindingException(method.getName() + " cannot have multiple " + paramType.getSimpleName() + " parameters"); + } + } + } + return index; + } + + /** + * 获取 {@link MapKey} 注解数据 + * + * @param method + * @return + */ + private String getMapKey(Method method) { + String mapKey = null; + if (Map.class.isAssignableFrom(method.getReturnType())) { + final MapKey mapKeyAnnotation = method.getAnnotation(MapKey.class); + if (mapKeyAnnotation != null) { + mapKey = mapKeyAnnotation.value(); + } + } + return mapKey; + } + } + +``` \ No newline at end of file diff --git a/docs/Mybatis/Mybatis-ParamNameResolver.md b/docs/Mybatis/HuiFer-Mybatis-ParamNameResolver.md similarity index 100% rename from docs/Mybatis/Mybatis-ParamNameResolver.md rename to docs/Mybatis/HuiFer-Mybatis-ParamNameResolver.md diff --git a/docs/Mybatis/Mybatis-Reflector.md b/docs/Mybatis/HuiFer-Mybatis-Reflector.md similarity index 100% rename from docs/Mybatis/Mybatis-Reflector.md rename to docs/Mybatis/HuiFer-Mybatis-Reflector.md diff --git a/docs/Mybatis/Mybatis-log.md b/docs/Mybatis/HuiFer-Mybatis-log.md similarity index 100% rename from docs/Mybatis/Mybatis-log.md rename to docs/Mybatis/HuiFer-Mybatis-log.md