Mybatis-MapperProxy源码解析

Mybatis3.5.1源码分析

  1. Mybatis-SqlSessionFactoryBuilder,XMLConfigBuilder,XPathParser源码解析
  2. Mybatis-Configuration源码解析
  3. Mybatis-事务对象源码解析
  4. Mybatis-数据源源码解析
  5. Mybatis缓存策略源码解析
  6. Mybatis-DatabaseIdProvider源码解析
  7. Mybatis-TypeHandler源码解析
  8. Mybatis-Reflector源码解析
  9. Mybatis-ObjectFactory,ObjectWrapperFactory源码分析
  10. Mybatis-Mapper各类标签封装类源码解析
  11. Mybatis-XMLMapperBuilder,XMLStatmentBuilder源码分析
  12. Mybatis-MapperAnnotationBuilder源码分析
  13. [Mybatis-MetaObject,MetaClass源码解析]https://www.jianshu.com/p/f51fa552f30a)
  14. Mybatis-LanguageDriver源码解析
  15. Mybatis-SqlSource源码解析
  16. Mybatis-SqlNode源码解析
  17. Mybatis-KeyGenerator源码解析
  18. Mybatis-Executor源码解析
  19. Mybatis-ParameterHandler源码解析
  20. Mybatis-StatementHandler源码解析
  21. Mybatis-DefaultResultSetHandler(一)源码解析
  22. Mybatis-DefaultResultSetHandler(二)源码解析
  23. Mybatis-ResultHandler,Cursor,RowBounds 源码分析
  24. Mybatis-MapperProxy源码解析
  25. Mybatis-SqlSession源码解析
  26. Mybatis-Interceptor源码解析

MapperRegistry

/**
 *    Copyright 2009-2019 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.binding;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.ibatis.builder.annotation.MapperAnnotationBuilder;
import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;

/**
 * 映射器注册器
 * @author Clinton Begin
 * @author Eduardo Macarron
 * @author Lasse Voss
 */
public class MapperRegistry {

  /**
   * Mybatis全局配置信息
   */
  private final Configuration config;
  /**
   * 已注册的映射器接口类映射,key=>映射器接口类,value=>映射代理器工厂
   */
  private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<>();

  /**
   *
   * @param config Mybatis全局配置信息
   */
  public MapperRegistry(Configuration config) {
    this.config = config;
  }

  /**
   * 获取映射器
   * @return
   */
  @SuppressWarnings("unchecked")
  public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    //获取type对应的映射代理器工厂
    final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
    //如果映射代理器工厂为null
    if (mapperProxyFactory == null) {
      //抛出异常
      throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
    }
    try {
      //创建type代理对象
      return mapperProxyFactory.newInstance(sqlSession);
    } catch (Exception e) {
      //抛出异常
      throw new BindingException("Error getting mapper instance. Cause: " + e, e);
    }
  }

  /**
   * 判断是否已经进行注册该类
   * <p>
   *    从{@link #knownMappers}进行判断。
   * </p>
   * @param type Mapper.xml对应的接口类
   * @return 如果{@code type} 存在knownMappers中,返回true;否则返回false
   */
  public <T> boolean hasMapper(Class<T> type) {
    return knownMappers.containsKey(type);
  }

  /**
   * 将 Mapper.xml对应的接口类 加入到knownMappper中
   * @param type Mapper.xml对应的接口类
   */
  public <T> void addMapper(Class<T> type) {
    //只有接口才会进行注册...
    if (type.isInterface()) {
      //如果在knownMappers已经找到了对应的type,将抛出异常
      if (hasMapper(type)) {
        throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
      }
      //已加载完成标记
      boolean loadCompleted = false;
      try {
        //将type和对应的映射代理器工厂添加到knowMappers中
        knownMappers.put(type, new MapperProxyFactory<>(type));
        // It's important that the type is added before the parser is run
        // otherwise the binding may automatically be attempted by the
        // mapper parser. If the type is already known, it won't try.
        /**
         * 译文:
         * 让type在解析器运行之前添加到knownMappers这个很重要。
         * 否则,映射分析器可能会自动尝试绑定。如果type的已经添加到knownMappers后,它就不会尝试。
         */
        //新建一个 映射器注解构建器
        MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
        /**
         * 解析映射接口类,解析mapper标签下的所有方法和注解,并对解析出来的信息加以封装,
         * 然后添加到Mybatis全局配置信息中。然后重新解析Mybatis全局配置信息中未能完成解析的
         * Method重新解析
         */
        parser.parse();
        //设置已加载完成标记为true
        loadCompleted = true;
      } finally {
        //如果未能完成加载的接口映射类
        if (!loadCompleted) {
          //从knownMappers中移除接口映射类
          knownMappers.remove(type);
        }
      }
    }
  }

  /**
   * 获取已注册的映射接口类集合
   * @since 3.2.2
   */
  public Collection<Class<?>> getMappers() {
    //返回不能修改的Set
    return Collections.unmodifiableCollection(knownMappers.keySet());
  }

  /**
   * 添加映射器
   * <p>
   *     从{@link @packageName}中获取{@link @superType}的子类,并将这些子类传入{@link #addMapper(Class)}进行注册
   * </p>
   * @since 3.2.2
   */
  public void addMappers(String packageName, Class<?> superType) {
    //ResolverUtil:用于查找在类路径可用并满足任意条件的类。
    ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
    //查找在packageName下的superType的子类
    resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
    //获取匹配的类集合
    Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
    //遍历映射类集合
    for (Class<?> mapperClass : mapperSet) {
      //将 映射接口类 加入到knownMappper中
      addMapper(mapperClass);
    }
  }

  /**
   * 添加映射器
   * <p>
   *     将{@link #Object}传入{@link #addMappers(String, Class)},传入{@link #Object}也就意味着
   *     获取包中所有的类。
   * </p>
   * @since 3.2.2
   */
  public void addMappers(String packageName) {
    addMappers(packageName, Object.class);
  }

}

MapperProxy

/**
 *    Copyright 2009-2018 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.binding;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.ibatis.session.SqlSession;

/**
 * 映射代理器工厂类
 * @author Lasse Voss
 */
public class MapperProxyFactory<T> {

  /**
   * 映射器接口类
   */
  private final Class<T> mapperInterface;
  /**
   * 方法缓存Map,映射接口类方法对象-映射方法类对象
   */
  private final Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<>();

  /**
   *
   * @param mapperInterface 映射接口类
   */
  public MapperProxyFactory(Class<T> mapperInterface) {
    this.mapperInterface = mapperInterface;
  }

  /**
   * 获取映射接口类
   * @return {@link #mapperInterface}
   */
  public Class<T> getMapperInterface() {
    return mapperInterface;
  }

  /**
   * 方法缓存Map,映射接口类方法对象-映射方法类对象
   * @return {@link #mapperInterface}
   */
  public Map<Method, MapperMethod> getMethodCache() {
    return methodCache;
  }

  /**
   * 创建mapperInterface代理对象
   * @param mapperProxy 映射代理器对象
   * @return mapperInterface代理对象
   */
  @SuppressWarnings("unchecked")
  protected T newInstance(MapperProxy<T> mapperProxy) {
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  }

  /**
   * 创建mapperInterface代理对象
   * @param sqlSession 数据库对话对象
   * @return
   */
  public T newInstance(SqlSession sqlSession) {
    //新建mapperInterface的 映射代理器对象
    final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
    //创建mapperInterface代理对象
    return newInstance(mapperProxy);
  }

}

MapperProxy

/**
 *    Copyright 2009-2019 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.binding;

import java.io.Serializable;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Map;

import org.apache.ibatis.reflection.ExceptionUtil;
import org.apache.ibatis.session.SqlSession;

/**
 * 映射器代理
 * @author Clinton Begin
 * @author Eduardo Macarron
 */
public class MapperProxy<T> implements InvocationHandler, Serializable {

  private static final long serialVersionUID = -6424540398559729838L;
  /**
   * 数据库对话对象
   */
  private final SqlSession sqlSession;
  /**
   * 映射器接口类
   */
  private final Class<T> mapperInterface;
  /**
   * 方法缓存Map,映射接口类方法对象-映射方法类对象
   */
  private final Map<Method, MapperMethod> methodCache;

  /**
   *
   * @param sqlSession 数据库对话对象
   * @param mapperInterface 映射器接口类
   * @param methodCache 方法缓存Map,映射接口类方法对象-映射方法类对象
   */
  public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
    this.sqlSession = sqlSession;
    this.mapperInterface = mapperInterface;
    this.methodCache = methodCache;
  }

  /**
   * 代理方法回调
   * @param proxy 代理后的实例对象
   * @param method 对象被调用方法
   * @param args 调用时的参数
   */
  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      //Method.getDeclaringClass:返回声明的该方法的类
      //如果声明method的类是Object
      if (Object.class.equals(method.getDeclaringClass())) {
        //直接执行
        return method.invoke(this, args);
        //Method.isDefault:如果此方法是默认方法,则返回true ; 返回false其他 默认方法是公共非抽象实例方法,即具有主体的非静态方法,以接口类型声明
        //如果method是默认方法
      } else if (method.isDefault()) {
        //执行默认方法
        return invokeDefaultMethod(proxy, method, args);
      }
    } catch (Throwable t) {
      //当(参数wrapped)属于InvocationTargetException和UndeclaredThrowableException的类型时,会从中 获取更加精确的异常抛出
      throw ExceptionUtil.unwrapThrowable(t);
    }
    //从缓存Map中获取MapperMethod对象
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    //执行SQL脚本,得到结果对象,然后转换成对应method返回类型的对象
    return mapperMethod.execute(sqlSession, args);
  }

  /**
   * /从缓存Map中获取method对应的MapperMethod对象
   * @param method 方法对象
   * @return method对应的MapperMethod对象
   */
  private MapperMethod cachedMapperMethod(Method method) {
    //从缓存Map中获取method对应的MapperMethod对象,如果没有,就新建一个MapperMethod对象
    return methodCache.computeIfAbsent(method, k -> new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()));
  }

  /**
   * 执行默认方法
   * @param proxy 代理后的实例对象
   * @param method 对象被调用方法
   * @param args 调用时的参数
   */
  private Object invokeDefaultMethod(Object proxy, Method method, Object[] args)
      throws Throwable {
    //获取MethodHandles.Lookup的接收Class和int的构造方法
    final Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class
        .getDeclaredConstructor(Class.class, int.class);
    //如果构造函数不能访问
    if (!constructor.isAccessible()) {
      //设置构造函数为可访问
      constructor.setAccessible(true);
    }
    //获取声明method的类
    final Class<?> declaringClass = method.getDeclaringClass();

    /**
     * constructor.newInstance(declaringClass,
     *             MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PROTECTED
     *                 | MethodHandles.Lookup.PACKAGE | MethodHandles.Lookup.PUBLIC)
     *                 :通过constructor构建MethodHandles.Lookup实例对象
     * MethodHandles.Lookup.unreflectSpecial:生成可以调用反射方法【declaringClass的method】的方法处理器
     * MethodHandle.bindTo(proxy):绑定需要放射方法的对象
     * MethodHandle.invokeWithArguments:传入args,反射方法
     */
    return constructor
        .newInstance(declaringClass,
            MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PROTECTED
                | MethodHandles.Lookup.PACKAGE | MethodHandles.Lookup.PUBLIC)
        .unreflectSpecial(method, declaringClass).bindTo(proxy).invokeWithArguments(args);
  }
}

MapperMethod

/**
 *    Copyright 2009-2019 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.binding;

import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.ibatis.annotations.Flush;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.mapping.StatementType;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.ParamNameResolver;
import org.apache.ibatis.reflection.TypeParameterResolver;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

/**
 * 映射方法
 * @author Clinton Begin
 * @author Eduardo Macarron
 * @author Lasse Voss
 * @author Kazuki Shimizu
 */
public class MapperMethod {

  /**
   *
   */
  private final SqlCommand command;
  private final MethodSignature method;

  /**
   *
   * @param mapperInterface 映射器接口类
   * @param method 方法对象
   * @param config Mybatis全局配置信息
   */
  public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {
    //SqlCommand:通过mapperInterface+ method 找到的 MapperStatement对象,保存着MapperStatement对象的Id和SqlCommandType
    //新建一个SqlCommand对象
    this.command = new SqlCommand(config, mapperInterface, method);
    // MethodSignature,封装着方法的返回类型的各个情况描述,指定类型的参数索引位置,以及参数名解析器
    //新建一个方法签名对象
    this.method = new MethodSignature(config, mapperInterface, method);
  }

  /**
   * 执行SQL脚本,得到结果对象,然后转换成对应method返回类型的对象
   * @param sqlSession 数据库会话对象
   * @param args 参数对象数组
   * @return
   */
  public Object execute(SqlSession sqlSession, Object[] args) {
    Object result;
    //判断Sql指令类型
    switch (command.getType()) {
      //插入指令
      case INSERT: {
        //构建sql脚本参数名-参数对象映射集合;
        Object param = method.convertArgsToSqlCommandParam(args);
        //执行插入Sql脚本,然后将更新记录数转换成method所需要的返回类型对象
        result = rowCountResult(sqlSession.insert(command.getName(), param));
        break;
      }
      //修改指令
      case UPDATE: {
        //构建sql脚本参数名-参数对象映射集合;
        Object param = method.convertArgsToSqlCommandParam(args);
        //执行修改Sql脚本,然后将更新记录数转换成method所需要的返回类型对象
        result = rowCountResult(sqlSession.update(command.getName(), param));
        break;
      }
      //删除指令
      case DELETE: {
        ///构建sql脚本参数名-参数对象映射集合;
        Object param = method.convertArgsToSqlCommandParam(args);
        //执行删除Sql脚本,然后将更新记录数转换成method所需要的返回类型对象
        result = rowCountResult(sqlSession.delete(command.getName(), param));
        break;
      }
      //查询指令
      case SELECT:
        //如果method返回类型为void 而且 method有ResultHandler类型参数
        if (method.returnsVoid() && method.hasResultHandler()) {
          //用ResultHandler执行查询sql
          executeWithResultHandler(sqlSession, args);
          //结果对象赋值为null
          result = null;
          //如果method返回类型为Collection的子类或者返回类型是数组
        } else if (method.returnsMany()) {
          /**
           * 执行查询SQL,默认返回结果对象集合,但如果method返回类型为数组,该方法就会自动将结果对象集合
           * 转换成数组;如果method返回类型为自定义的Collection实现类,该方法也会将结果对象集合的元素添加到
           * 自定义的Collection实现类对象中
           */
          result = executeForMany(sqlSession, args);
          //如果method的返回类型是Map
        } else if (method.returnsMap()) {
          //执行查询SQL,返回结果对象映射赋值给result
          result = executeForMap(sqlSession, args);
          //如果method返回类型是Cursor
        } else if (method.returnsCursor()) {
          //执行查询SQL,返回结果对象游标赋值给result
          result = executeForCursor(sqlSession, args);
        } else {
          //构建sql脚本参数名-参数对象映射集合
          Object param = method.convertArgsToSqlCommandParam(args);
          //执行查询SQL,返回结果对象赋值给result
          result = sqlSession.selectOne(command.getName(), param);
          //如果method返回类型为Optional 且 结果对象为null 或者 method返回类型不等于结果对象类型
          if (method.returnsOptional()
              && (result == null || !method.getReturnType().equals(result.getClass()))) {
            //如果对象即可能是 null 也可能是非 null,使用 ofNullable() 方法可以防止空指针异常
            result = Optional.ofNullable(result);
          }
        }
        break;
        //刷新指令
      case FLUSH:
        //执行全部等待批处理语句,并将结果封装到BatchResult集合中,然后赋值reuslt
        result = sqlSession.flushStatements();
        break;
        //其他指令
      default:
        //抛出异常
        throw new BindingException("Unknown execution method for: " + command.getName());
    }
    //如果结果对象为null 且 method的返回类型为原始类型 且 method的返回类型不是void
    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;
  }

  /**
   * 将{@code rowCount} 转换成method所需要的返回类型对象
   * @param rowCount 更新记录数
   * @return method所需要的返回类型对象
   */
  private Object rowCountResult(int rowCount) {
    final Object result;
    //如果method返回类型为Void
    if (method.returnsVoid()) {
      //直接返回null
      result = null;
      //如果method返回类型为Integer类型,或者method返回类型为int类型
    } else if (Integer.class.equals(method.getReturnType()) || Integer.TYPE.equals(method.getReturnType())) {
      //将rowCount赋值给result
      result = rowCount;
      //如果method返回类型为Long类型,或者method返回类型为long类型
    } else if (Long.class.equals(method.getReturnType()) || Long.TYPE.equals(method.getReturnType())) {
      //强转rowCount为long,将rowCount赋值给result
      result = (long)rowCount;
      //如果method返回类型为Boolean类型,或者method返回类型为boolean类型
    } else if (Boolean.class.equals(method.getReturnType()) || Boolean.TYPE.equals(method.getReturnType())) {
      //判断rowCount是否大于0,并将判断结果赋值到result
      result = rowCount > 0;
      //如果上面所支持的类型都不匹配
    } else {
      //抛出异常
      throw new BindingException("Mapper method '" + command.getName() + "' has an unsupported return type: " + method.getReturnType());
    }
    return result;
  }

  /**
   * 用ResultHandler执行查询sql
   * @param sqlSession 数据库会话对象
   * @param args 参数对象
   */
  private void executeWithResultHandler(SqlSession sqlSession, Object[] args) {
    //获取command.name对应的MappedStatement对象
    MappedStatement ms = sqlSession.getConfiguration().getMappedStatement(command.getName());
    //如果ms的StatementType不是存储过程 且 ms的返回映射的第一个类型是void
    if (!StatementType.CALLABLE.equals(ms.getStatementType())
        && void.class.equals(ms.getResultMaps().get(0).getType())) {
      //抛出异常
      throw new BindingException("method " + command.getName()
          + " needs either a @ResultMap annotation, a @ResultType annotation,"
          + " or a resultType attribute in XML so a ResultHandler can be used as a parameter.");
    }
    //构建sql脚本参数名-参数对象映射集合
    Object param = method.convertArgsToSqlCommandParam(args);
    //如果method有定义RowBound类型参数
    if (method.hasRowBounds()) {
      //获取RowBounds对象
      RowBounds rowBounds = method.extractRowBounds(args);
      //获取ResultHandler对象,然后执行查询SQL脚本,并将结果对象赋值到ResultHandlerd对象中
      sqlSession.select(command.getName(), param, rowBounds, method.extractResultHandler(args));
    } else {
      //获取ResultHandler对象,然后执行查询SQL脚本,并将结果对象赋值到ResultHandlerd对象中
      sqlSession.select(command.getName(), param, method.extractResultHandler(args));
    }
  }

  /**
   * 执行查询SQL,默认返回结果对象集合,但如果method返回类型为数组,该方法就会自动将结果对象集合
   * 转换成数组;如果method返回类型为自定义的Collection实现类,该方法也会将结果对象集合的元素添加到
   * 自定义的Collection实现类对象中
   * @param sqlSession 数据库会话对象
   * @param args 参数对象数组
   * @param <E> 结果对象类型
   * @return 结果对象集合
   */
  private <E> Object executeForMany(SqlSession sqlSession, Object[] args) {
    //定义一个结果对象集合
    List<E> result;
    //构建sql脚本参数名-参数对象映射集合
    Object param = method.convertArgsToSqlCommandParam(args);
    //如果method有定义RowBound类型参数
    if (method.hasRowBounds()) {
      //获取RowBounds对象
      RowBounds rowBounds = method.extractRowBounds(args);
      //传入rowBound对象,执行查询SQL,返回结果对象集合赋值给result
      result = sqlSession.selectList(command.getName(), param, rowBounds);
    } else {
      //执行查询SQL,返回结果对象集合赋值给result
      result = sqlSession.selectList(command.getName(), param);
    }
    // issue #510 Collections & arrays support
    //如果method的返回类型不是result的父类
    if (!method.getReturnType().isAssignableFrom(result.getClass())) {
      //如果method的返回类型是数组
      if (method.getReturnType().isArray()) {
        //将集合转换成数组,然后返回出去
        return convertToArray(result);
      } else {
        //将 list 转换成 method返回类型,method返回类型必须是Collection的子类,否则会抛出异常。然后返回出去
        return convertToDeclaredCollection(sqlSession.getConfiguration(), result);
      }
    }
    //直接返回结果对象集合
    return result;
  }

  /**
   * 执行查询SQL,返回结果对象游标
   * @param sqlSession 数据库会话
   * @param args 参数对象数组
   * @param <T> 结果对象类型
   * @return 结果对象游标
   */
  private <T> Cursor<T> executeForCursor(SqlSession sqlSession, Object[] args) {
    //定义一个结果对象游标
    Cursor<T> result;
    //构建sql脚本参数名-参数对象映射集合
    Object param = method.convertArgsToSqlCommandParam(args);
    //如果method有定义RowBound类型参数
    if (method.hasRowBounds()) {
      //获取RowBounds对象
      RowBounds rowBounds = method.extractRowBounds(args);
      //传入rowBound对象,执行查询SQL,返回结果对象游标赋值给result
      result = sqlSession.selectCursor(command.getName(), param, rowBounds);
    } else {
      //执行查询SQL,返回结果对象游标赋值给result
      result = sqlSession.selectCursor(command.getName(), param);
    }
    return result;
  }

  /**
   * 将 {@code list} 转换成 method返回类型,method返回类型必须是Collection的子类,否则
   * 会抛出异常。
   * @param config Mybatis全局配置项
   * @param list 结果对象集合
   * @param <E> 结果对象类型
   * @return method返回类型对象
   */
  private <E> Object convertToDeclaredCollection(Configuration config, List<E> list) {
    //使用对象工厂创建method返回类型的实例对象
    Object collection = config.getObjectFactory().create(method.getReturnType());
    //新建一个collection的元对象
    MetaObject metaObject = config.newMetaObject(collection);
    /**
     * 将list的元素添加到collection里,addAll只有CollectionWrapper有实现,也只有
     * method的返回类型是继承Collection才会引用CcllectionWrapper,而其他ObjectWrapper调用allAll都会抛出异常
     */
    metaObject.addAll(list);

    return collection;
  }

  /**
   * 将集合转换成数组。
   * @param list 结果对象集合
   * @param <E> 结果对象类型
   * @return 数组
   */
  @SuppressWarnings("unchecked")
  private <E> Object convertToArray(List<E> list) {
    //调用该方法的时候,method的返回类型已经确定是数组类型
    //获取method返回类型数组的元素类型
    Class<?> arrayComponentType = method.getReturnType().getComponentType();
    //构建一个元素类型为arrayComponentType,长度为list.size()的数组对象
    Object array = Array.newInstance(arrayComponentType, list.size());
    //如果arryComponentType为原始类型(boolean、char、byte、short、int、long、float、double)
    if (arrayComponentType.isPrimitive()) {
      //遍历集合
      for (int i = 0; i < list.size(); i++) {
        //将list第i个位置的元素,赋值给array第i个位置的元素里
        Array.set(array, i, list.get(i));
      }
      return array;
    } else {
      //调用List.toArray方法进行转换成数组
      return list.toArray((E[])array);
    }
  }

  /**
   * 执行查询SQL,返回结果对象映射。
   * @param sqlSession 数据库会话对象
   * @param args 参数对象数组
   * @param <K> method的MapKey注解定义的属性对应的值
   * @param <V> 结果对象
   * @return 结果对象映射
   */
  private <K, V> Map<K, V> executeForMap(SqlSession sqlSession, Object[] args) {
    //定义一个Map类型的结果对象映射
    Map<K, V> result;
    //构建sql脚本参数名-参数对象映射集合
    Object param = method.convertArgsToSqlCommandParam(args);
    //如果method有定义RowBound类型参数
    if (method.hasRowBounds()) {
      //获取RowBounds对象
      RowBounds rowBounds = method.extractRowBounds(args);
      //传入rowBound对象以及method里的MapKey注解定义的值,执行查询SQL,返回结果对象映射赋值给result
      result = sqlSession.selectMap(command.getName(), param, method.getMapKey(), rowBounds);
    } else {
      //传入method里的MapKey注解定义的值,执行查询SQL,返回结果对象映射赋值给result
      result = sqlSession.selectMap(command.getName(), param, method.getMapKey());
    }
    return result;
  }

  /**
   * 参数Map
   * <p>
   *     HashMap的子类,对获取集合没有元素时会抛出异常。
   * </p>
   * @param <V> Object
   */
  public static class ParamMap<V> extends HashMap<String, V> {

    private static final long serialVersionUID = -2212268410512043556L;

    @Override
    public V get(Object key) {
      //没有找到相应的key会抛出 BindingException
      if (!super.containsKey(key)) {
        throw new BindingException("Parameter '" + key + "' not found. Available parameters are " + keySet());
      }
      return super.get(key);
    }

  }

  /**
   * SQL命令
   * <p>
   *     通过mapperInterface+ method 找到的 MapperStatement对象,保存着MapperStatement对象的Id和SqlCommandType
   * </p>
   */
  public static class SqlCommand {

    /**
     * 通过mapperInterface+ method 找到的 MapperStatement对象的Id
     */
    private final String name;
    /**
     * 通过mapperInterface+ method 找到的 MapperStatement对象的SQL脚本类型
     */
    private final SqlCommandType type;

    /**
     *
     * @param configuration Mybatis全局配置信息
     * @param mapperInterface 映射器接口类
     * @param method 方法对象
     */
    public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
      //获取方法名
      final String methodName = method.getName();
      //获取声明method的类
      final Class<?> declaringClass = method.getDeclaringClass();
      //获取 mapperInterface+ methodName 对应的 MappedStatement对象
      MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass,
          configuration);
      //如果ms对象为null
      if (ms == null) {
        //如果method加上了Flush注解
        if (method.getAnnotation(Flush.class) != null) {
          //将 通过mapperInterface+ method 找到的 MapperStatement对象的Id 置为null
          name = null;
          //Sql命令类型设置成FLUSH
          type = SqlCommandType.FLUSH;
          //如果没有加上Flush注解
        } else {
          //抛出异常
          throw new BindingException("Invalid bound statement (not found): "
              + mapperInterface.getName() + "." + methodName);
        }
      } else {
        //保存 通过mapperInterface+ method 找到的 MapperStatement对象的Id
        name = ms.getId();
        //保存 ms的Sql命令类型
        type = ms.getSqlCommandType();
        //如果ms的Sql命令类型为UNKNOW
        if (type == SqlCommandType.UNKNOWN) {
          //抛出异常
          throw new BindingException("Unknown execution method for: " + name);
        }
      }
    }

    /**
     * 获取 通过mapperInterface+ method 找到的 MapperStatement对象的Id
     * @return {@link #name}
     */
    public String getName() {
      return name;
    }

    /**
     * 获取 通过mapperInterface+ method 找到的 MapperStatement对象的SQL脚本类型
     * @return {@link #type}
     */
    public SqlCommandType getType() {
      return type;
    }

    /**
     * 获取 {@code mapperInterface} + {@code methodName} 对应的 MappedStatement对象
     * @param mapperInterface 映射器接口类
     * @param methodName 方法名
     * @param declaringClass 声明method的类
     * @param configuration Mybatis全局配置信息
     * @return {@code mapperInterface} + {@code methodName} 对应的 MappedStatement对象
     */
    private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName,
        Class<?> declaringClass, Configuration configuration) {
      //拼装statementId,取映射器接口包名+类名+'.'+方法名
      String statementId = mapperInterface.getName() + "." + methodName;
      //如果存在statementId对应的MappedStatement对象
      if (configuration.hasStatement(statementId)) {
        //取出对应statementId的MappedStatement对象,并返回
        return configuration.getMappedStatement(statementId);
        //如果映射器接口类等于声明method的类
      } else if (mapperInterface.equals(declaringClass)) {
        //直接返回null
        return null;
      }
      //遍历映射器接口类继承的所有接口
      for (Class<?> superInterface : mapperInterface.getInterfaces()) {
        //如果声明method的类是superInterface的父类
        if (declaringClass.isAssignableFrom(superInterface)) {
          //递归获取 superInterface + methodName 对应的 MappedStatement对象
          MappedStatement ms = resolveMappedStatement(superInterface, methodName,
              declaringClass, configuration);
          //如果ms不为null
          if (ms != null) {
            //返回ms
            return ms;
          }
        }
      }
      //连从继承的接口中去找都找不到时,就返回null
      return null;
    }
  }

  /**
   * 方法签名,封装着方法的返回类型的各个情况描述,指定类型的参数索引位置,以及参数名解析器
   */
  public static class MethodSignature {

    /**
     * 如果返回类型为Collection的子类或者返回类型是数组,returnsMany为true
     */
    private final boolean returnsMany;
    /**
     * 如果 {@link #mapKey} 不为null,returnMap为true
     */
    private final boolean returnsMap;
    /**
     * 如果返回类型为Void,returnsVoid为true
     */
    private final boolean returnsVoid;
    /**
     * 如果返回类型为Cursor类型,returnCursor为true
     */
    private final boolean returnsCursor;
    /**
     * 如果返回类型为Optional类型,returnsOptional为true
     */
    private final boolean returnsOptional;
    /**
     * 方法的返回类型
     */
    private final Class<?> returnType;
    /**
     * 只有当method的返回类型为Map,且有加上MapKey注解,就返回MapKey注解所定义的值;否则返回null
     */
    private final String mapKey;
    /**
     * resultHandler类型参数在方法中索引位置
     */
    private final Integer resultHandlerIndex;
    /**
     * rowBounds类型参数在方法中索引位置
     */
    private final Integer rowBoundsIndex;
    /**
     * 参数名解析器: 参数名称解析器,用于构建sql脚本参数名+参数对象映射集合
     */
    private final ParamNameResolver paramNameResolver;

    /**
     *
     * @param configuration Mybatis全局配置信息
     * @param mapperInterface 映射器接口类
     * @param method 方法对象
     */
    public MethodSignature(Configuration configuration, Class<?> mapperInterface, Method method) {
      //获取method在mapperInterface中的返回类型
      Type resolvedReturnType = TypeParameterResolver.resolveReturnType(method, mapperInterface);
      //如果返回类型为Class实例
      if (resolvedReturnType instanceof Class<?>) {
        //将返回类型强转为Class类型,赋值给returnType
        this.returnType = (Class<?>) resolvedReturnType;
        //如果返回类型为参数化类型,参数化类型:https://blog.csdn.net/JustBeauty/article/details/81116144
      } else if (resolvedReturnType instanceof ParameterizedType) {
        //将返回类型强转为ParameterizedType类型,然后取出声明的类型,如List<T>,getRawType得到的就是List,然后强转声明类型为Class类型,赋值给returnType
        this.returnType = (Class<?>) ((ParameterizedType) resolvedReturnType).getRawType();
      } else {
        //取出方法的返回类型
        this.returnType = method.getReturnType();
      }
      //如果返回类型为Void,returnsVoid为true
      this.returnsVoid = void.class.equals(this.returnType);
      //如果返回类型为Collection的子类或者返回类型是数组,returnsMany为true
      this.returnsMany = configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray();
      //如果返回类型为Cursor类型,returnCursor为true
      this.returnsCursor = Cursor.class.equals(this.returnType);
      //如果返回类型为Optional类型,returnsOptional为true
      this.returnsOptional = Optional.class.equals(this.returnType);
      //只有当method的返回类型为Map,且有加上MapKey注解,就返回MapKey注解所定义的值;否则返回null
      this.mapKey = getMapKey(method);
      //如果mapKey不为null,returnMap为true
      this.returnsMap = this.mapKey != null;
      //获取RowBounds.class 在 method 的参数类型数组中的唯一位置,赋值给rowBoundsIndex
      this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class);
      //获取ResultHandler.class 在 method 的参数类型数组中的唯一位置,赋值给rowBoundsIndex
      this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class);
      //新建一个参数名解析器: 参数名称解析器,用于构建sql脚本参数名+参数对象映射集合
      this.paramNameResolver = new ParamNameResolver(configuration, method);
    }

    /**
     * 将参数对象转换为sql脚本参数,返回sql脚本参数名-参数对象映射集合;
     * @param args 参数对象数值
     * @return  sql脚本参数名-参数对象映射集合
     */
    public Object convertArgsToSqlCommandParam(Object[] args) {
      return paramNameResolver.getNamedParams(args);
    }

    /**
     * 是否存在RowRounds类型参数
     * @return 存在返回true;否则返回false
     */
    public boolean hasRowBounds() {
      //如果rowBounds类型参数在方法中索引位置不为null
      return rowBoundsIndex != null;
    }

    /**
     * 提取RowBounds对象
     * @param args 参数对象数组
     * @return RowBounds对象
     */
    public RowBounds extractRowBounds(Object[] args) {
      //如果存在RowRounds类型参数,返回rowBoundsIndex的参数对象;否则返回null
      return hasRowBounds() ? (RowBounds) args[rowBoundsIndex] : null;
    }

    /**
     * 是否存在ResultHandler类型参数
     * @return 存在返回true;否则返回false
     */
    public boolean hasResultHandler() {
      //如果ResultHandler类型参数在方法中索引位置不为null
      return resultHandlerIndex != null;
    }

    /**
     * 提取ResultHandler对象
     * @param args 参数对象数组
     * @return ResultHandler对象
     */
    public ResultHandler extractResultHandler(Object[] args) {
      //如果存在ResultHandler类型参数,返回resultHandlerIndex的参数对象;否则返回null
      return hasResultHandler() ? (ResultHandler) args[resultHandlerIndex] : null;
    }

    /**
     * 只有当method的返回类型为Map,且有加上MapKey注解,就返回MapKey注解所定义的值;否则返回null
     */
    public String getMapKey() {
      return mapKey;
    }

    /**
     * 方法的返回类型
     */
    public Class<?> getReturnType() {
      return returnType;
    }

    /**
     * 如果返回类型为Collection的子类或者返回类型是数组,returnsMany为true
     */
    public boolean returnsMany() {
      return returnsMany;
    }

    /**
     * 如果 {@link #mapKey} 不为null,returnMap为true
     */
    public boolean returnsMap() {
      return returnsMap;
    }

    /**
     *如果返回类型为Void,returnsVoid为true
     */
    public boolean returnsVoid() {
      return returnsVoid;
    }

    /**
     *  如果返回类型为Cursor类型,returnCursor为true
     */
    public boolean returnsCursor() {
      return returnsCursor;
    }

    /**
     * 如果返回类型为Optional类型,returnsOptional为true<br/>
     * 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;
    }

    /**
     * 获取 {@code paramType} 在 {@code method} 的参数类型数组中的唯一位置
     * @param method 方法对象
     * @param paramType 参数类型
     * @return {@code paramType} 在 {@code method} 的参数类型数组中的唯一位置,如果出现多个paramType的位置,会抛出异常
     */
    private Integer getUniqueParamIndex(Method method, Class<?> paramType) {
      Integer index = null;
      //获取method的参数类型数组
      final Class<?>[] argTypes = method.getParameterTypes();
      //遍历参数类型数组
      for (int i = 0; i < argTypes.length; i++) {
        //如果paramType为argType[i]的父类或者相同
        if (paramType.isAssignableFrom(argTypes[i])) {
          //如果位置为null
          if (index == null) {
            //将argType[i]的所在位置赋值给index
            index = i;
          } else {
            //如果出现多个paramType的位置,抛出异常
            throw new BindingException(method.getName() + " cannot have multiple " + paramType.getSimpleName() + " parameters");
          }
        }
      }
      return index;
    }

    /**
     * 获取MapKey
     * @param method 方法对象
     * @return 只有当method的返回类型为Map,且有加上MapKey注解,就返回MapKey注解所定义的值;否则返回null
     */
    private String getMapKey(Method method) {
      String mapKey = null;
      //如果method的返回类型是Map的子类
      if (Map.class.isAssignableFrom(method.getReturnType())) {
        //获取method中的MapKey注解
        final MapKey mapKeyAnnotation = method.getAnnotation(MapKey.class);
        //如果method有加上MapKey注解
        if (mapKeyAnnotation != null) {
          //获取MapKey注解的值
          mapKey = mapKeyAnnotation.value();
        }
      }
      //只要当method的返回类型为Map,且有加上MapKey注解,mapKey才不为null
      return mapKey;
    }
  }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,980评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,422评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,130评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,553评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,408评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,326评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,720评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,373评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,678评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,722评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,486评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,335评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,738评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,283评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,692评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,893评论 2 335

推荐阅读更多精彩内容