mybatis

mybatis 参数解析流程附相关案例

2021-10-28  本文已影响0人  晴天哥_王志

系列

开篇

参数解析过程

解析方法参数的别名

public class MapperMethod {

  private final SqlCommand command;
  private final MethodSignature method;

  // mapperInterface会对应的mapper的 interface 接口
  public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {
    this.command = new SqlCommand(config, mapperInterface, method);
    this.method = new MethodSignature(config, mapperInterface, method);
  }

  public static class MethodSignature {

    // 省略其他不强相关的代码
    private final SortedMap<Integer, String> params;
    private final boolean hasNamedParameters;

    // 解析方法签名
    public MethodSignature(Configuration configuration, Method method) {
      // 判断是否有@Param的注解
      this.hasNamedParameters = hasNamedParams(method);
      // 通过getParams来获取对应的参数值
      this.params = Collections.unmodifiableSortedMap(getParams(method, this.hasNamedParameters));
    }

    // 解析方法的参数
    private SortedMap<Integer, String> getParams(Method method, boolean hasNamedParameters) {
      final SortedMap<Integer, String> params = new TreeMap<Integer, String>();

      // 1、获取方法的参数类型
      final Class<?>[] argTypes = method.getParameterTypes();

      // 2、遍历方法的参数类型构建参数下标和别名的映射关系
      for (int i = 0; i < argTypes.length; i++) {
        if (!RowBounds.class.isAssignableFrom(argTypes[i]) && !ResultHandler.class.isAssignableFrom(argTypes[i])) {
          // 默认以参数的下标值作为value值
          String paramName = String.valueOf(params.size());
          // 如果有注解就遍历该参数对应的注解名字
          if (hasNamedParameters) {
            paramName = getParamNameFromAnnotation(method, i, paramName);
          }
          params.put(i, paramName);
        }
      }

      // 返回的params以参数下标作为key,以下标或者别名作为value
      return params;
    }

    // 从@Param注解中获取对应的别名
    private String getParamNameFromAnnotation(Method method, int i, String paramName) {
      // 遍历方法的所有参数注解,getParameterAnnotations返回的是一个二维数组
      // 第一维是参数的下标,第二维是改参数对应的多个注解
      final Object[] paramAnnos = method.getParameterAnnotations()[i];

      // 如果存在@Param注解就获取注解对应的别名
      for (Object paramAnno : paramAnnos) {
        if (paramAnno instanceof Param) {
          paramName = ((Param) paramAnno).value();
          break;
        }
      }
      return paramName;
    }
}

构建方法参数对象

public class MapperMethod {

  public static class MethodSignature {

    // 解析入参返回mybatis内部处理的参数
    public Object convertArgsToSqlCommandParam(Object[] args) {
      final int paramCount = params.size();

      // 1、参数为空的场景直接返回
      if (args == null || paramCount == 0) {
        return null;

      // 2、没有@Param注解修饰且只有一个参数的场景
      } else if (!hasNamedParameters && paramCount == 1) {
        return args[params.keySet().iterator().next().intValue()];

      // 3、其他的场景,包含@Param注解或参数个数大于1
      } else {
        final Map<String, Object> param = new ParamMap<Object>();
        int i = 0;
        // 遍历所有的方法参数,以别名或者下标作为key,value未
        for (Map.Entry<Integer, String> entry : params.entrySet()) {
          // entry.getValue()返回的是@Param对应的别名 或 参数的顺序
          param.put(entry.getValue(), args[entry.getKey().intValue()]);

          // 额外添加param0、param1作为key,value为对应的参数值
          final String genericParamName = "param" + String.valueOf(i + 1);
          if (!param.containsKey(genericParamName)) {
            param.put(genericParamName, args[entry.getKey()]);
          }
          i++;
        }
        return param;
      }
    }
}

解析方法参数的过程

public class MapperProxy<T> implements InvocationHandler, Serializable {

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      if (Object.class.equals(method.getDeclaringClass())) {
        return method.invoke(this, args);
      } else if (isDefaultMethod(method)) {
        return invokeDefaultMethod(proxy, method, args);
      }
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }

    // 针对被调用的方法 method 创建对应的 MapperMethod对象。
    final MapperMethod mapperMethod = cachedMapperMethod(method);

    // 通过mapperMethod执行 execute 动作
    return mapperMethod.execute(sqlSession, args);
  }

  private MapperMethod cachedMapperMethod(Method method) {
    MapperMethod mapperMethod = methodCache.get(method);
    if (mapperMethod == null) {
      mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
      methodCache.put(method, mapperMethod);
    }
    return mapperMethod;
  }
}
public class MapperMethod {

  private final SqlCommand command;
  private final MethodSignature method;

  public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {
    this.command = new SqlCommand(config, mapperInterface, method);
    this.method = new MethodSignature(config, mapperInterface, method);
  }

  public Object execute(SqlSession sqlSession, Object[] args) {
    Object result;
    switch (command.getType()) {
      case INSERT: {
        // 执行前会进行参数转换
      Object param = method.convertArgsToSqlCommandParam(args);
        result = rowCountResult(sqlSession.insert(command.getName(), param));
        break;
      }
      case UPDATE: {
        // 执行前会进行参数转换
        Object param = method.convertArgsToSqlCommandParam(args);
        result = rowCountResult(sqlSession.update(command.getName(), param));
        break;
      }
      case DELETE: {
        // 执行前会进行参数转换
        Object param = method.convertArgsToSqlCommandParam(args);
        result = rowCountResult(sqlSession.delete(command.getName(), param));
        break;
      }
      case SELECT:
        if (method.returnsVoid() && method.hasResultHandler()) {
          executeWithResultHandler(sqlSession, args);
          result = null;
        } else if (method.returnsMany()) {
          result = executeForMany(sqlSession, args);
        } else if (method.returnsMap()) {
          result = executeForMap(sqlSession, args);
        } else if (method.returnsCursor()) {
          result = executeForCursor(sqlSession, args);
        } else {
          Object param = method.convertArgsToSqlCommandParam(args);
          result = sqlSession.selectOne(command.getName(), param);
        }
        break;
      case FLUSH:
        result = sqlSession.flushStatements();
        break;
      default:
        throw new BindingException("Unknown execution method for: " + command.getName());
    }
    if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
      throw new BindingException("Mapper method '" + command.getName() 
          + " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
    }
    return result;
  }


  private <E> Object executeForMany(SqlSession sqlSession, Object[] args) {
    List<E> result;
    // 执行前会进行参数转换
    Object param = method.convertArgsToSqlCommandParam(args);
    if (method.hasRowBounds()) {
      RowBounds rowBounds = method.extractRowBounds(args);
      result = sqlSession.<E>selectList(command.getName(), param, rowBounds);
    } else {
      result = sqlSession.<E>selectList(command.getName(), param);
    }
    // issue #510 Collections & arrays support
    if (!method.getReturnType().isAssignableFrom(result.getClass())) {
      if (method.getReturnType().isArray()) {
        return convertToArray(result);
      } else {
        return convertToDeclaredCollection(sqlSession.getConfiguration(), result);
      }
    }
    return result;
  }

  private <T> Cursor<T> executeForCursor(SqlSession sqlSession, Object[] args) {
    Cursor<T> result;
    // 执行前会进行参数转换
    Object param = method.convertArgsToSqlCommandParam(args);
    if (method.hasRowBounds()) {
      RowBounds rowBounds = method.extractRowBounds(args);
      result = sqlSession.<T>selectCursor(command.getName(), param, rowBounds);
    } else {
      result = sqlSession.<T>selectCursor(command.getName(), param);
    }
    return result;
  }
}

特殊处理集合对象

public class DefaultSqlSession implements SqlSession {

  @Override
  public int update(String statement, Object parameter) {
    try {
      dirty = true;
      MappedStatement ms = configuration.getMappedStatement(statement);
      return executor.update(ms, wrapCollection(parameter));
    } catch (Exception e) {
      throw ExceptionFactory.wrapException("Error updating database.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }

  private Object wrapCollection(final Object object) {
    if (object instanceof Collection) {
      StrictMap<Object> map = new StrictMap<Object>();
      map.put("collection", object);
      if (object instanceof List) {
        map.put("list", object);
      }
      return map;
    } else if (object != null && object.getClass().isArray()) {
      StrictMap<Object> map = new StrictMap<Object>();
      map.put("array", object);
      return map;
    }
    return object;
  }
}

参数案例

场景一:
List<Object> batchSelectByXxx(@Param("xxx") List<String> xxxList)
参数映射:构建以xxx为key,xxxList为value的Map对象。

场景二:
int batchInsert(List<Object> xxxList)
参数映射:构建以list/collection为key,xxxList为value的Map对象

场景三:
int update(Object record)
参数映射:构建以Object为参数对象,没有Map。

场景四:
List<Object> listByParam(@Param("paramA") String strA, @Param("paramB") String strB)
参数映射:构建以paramA为key,strA为value;以paramB为key,strB为value的Map对象。

场景五:
List<Object> listByParam(String strA, String strB)
参数映射,构建以0为key,strA为value;以1为key,strB未value的Map对象。
上一篇下一篇

猜你喜欢

热点阅读