mybatis源碼分析之MapperMethod

上一篇:java

mybatis源碼分析之mapper動態代理sql

http://www.javashuo.com/article/p-cbabmbsj-hv.htmlmybatis

MapperMethod與MapperProxy,MapperProxyFactory,MapperRegistry,Configuration之間的關係app

分析mapper動態代理的時候能夠看出最終執行的是mapperMethod.execute源碼分析

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      //當執行的方法是繼承自Object時執行this裏的相應方法
      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);
    }
    //最終執行的是mapperMethod.execute
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    return mapperMethod.execute(sqlSession, args);
  }

接着分析mapperMethod.execute()this

首先看MapperProxy中cachedMapperMethod(method).net

/**
 * methodCache中已經存在傳入的參數method
 * 則從methodCache中取MapperMethod,
 * 不然根據method生成MapperMethod實例並存儲到methodCache中
 *
 * @param method
 * @return
 */
  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;
  }

MapperProxy中methodCache代理

private final Map<Method, MapperMethod> methodCache;

追溯methodCache的來源code

//MapperProxy構造方法  
public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
    this.sqlSession = sqlSession;
    this.mapperInterface = mapperInterface;
    this.methodCache = methodCache;
  }

MapperProxyFactory中blog

private final Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<Method, MapperMethod>();  

public T newInstance(SqlSession sqlSession) {
    final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
    return newInstance(mapperProxy);
  }

從MapperProxyFactory的源碼中能夠看出methodCache最初是一個空的ConcurrentHashMap,

從MapperProxy中cachedMapperMethod(method)能夠看出

 methodCache中已經存在傳入的參數method, 則從methodCache中取MapperMethod, 不然根據method生成MapperMethod實例並存儲到methodCache中.

接着看MapperMethod的源碼

構造方法

/**
   * @param mapperInterface 接口
   * @param method 調用的方法
   * @param config 配置
   */
  public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {
    this.command = new SqlCommand(config, mapperInterface, method);
    this.method = new MethodSignature(config, mapperInterface, method);
  }

建立了SqlCommand和MethodSignature實例

command type

public enum SqlCommandType {
  UNKNOWN, INSERT, UPDATE, DELETE, SELECT, FLUSH;
}

MapperMethod中最重要的方法

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;
  }

首先調用MethodSignature裏的

public Object convertArgsToSqlCommandParam(Object[] args)

處理參數.而後,

若是command type是

INSERT, UPDATE, DELETE

則直接調用sqlSession的相應方法

若是command type是

SELECT

則根據返回結果調用sqlSession裏相應的查詢方法.

至此MapperMethod從建立到執行的過程都大體分析了一遍.

相關文章
相關標籤/搜索