乐趣区

关于java:mybatis源码分析二-执行过程

这边博客连接上一篇 mybatis 的 xml 解析的博客,在 xml 解析实现之后,首先会解析成一个 Configuration 对象,而后创立一个 DefaultSqlSessionFactory 的 session 工厂。在这所有的筹备过程实现之后,就能够开始对数据库的操作了。

首先看 openSession() 办法

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {final Environment environment = configuration.getEnvironment();
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
      final Executor executor = configuration.newExecutor(tx, execType);
      return new DefaultSqlSession(configuration, executor, autoCommit);
    } catch (Exception e) {closeTransaction(tx); // may have fetched a connection so lets call close()
      throw ExceptionFactory.wrapException("Error opening session.  Cause:" + e, e);
    } finally {ErrorContext.instance().reset();}
  }

首先,依据 configuration 中取出的 environment,而后获取一个 TransactionFactory,接着通过事务工厂新建一个事务对象,其实在这一个步骤,并没有对数据库进行操作 newTransaction 办法仅仅是返回了一个 Transaction 对象,这个对象蕴含了 Datasource, level,autocommit 这几个属性,并没有做其余操作。(这里我 xml 中配置了 JDBC 事务,具体看这个事务,而不是第三方的事务)。

接下来创立一个 Executor 对象

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    executorType = executorType == null ? defaultExecutorType : executorType;
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
    Executor executor;
    if (ExecutorType.BATCH == executorType) {executor = new BatchExecutor(this, transaction);
    } else if (ExecutorType.REUSE == executorType) {executor = new ReuseExecutor(this, transaction);
    } else {executor = new SimpleExecutor(this, transaction);
    }
    if (cacheEnabled) {executor = new CachingExecutor(executor);
    }
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
  }

以后必定是创立了一个默认的 Executor,就是 SimpleExecutor,而后往下,判断是否配置了开启缓存,是的话则通过装璜器模式创立一个 CachingExecutor,接着调用 interceptorChain.pluginAll 办法返回一个被层层代理的对象,这部分在上一篇博客中剖析过。返回 executor 对象,再接下来,new 了一个 DefaultSqlSession 再返回,至此 openSession 办法就执行完结了。接下来咱们就能够调用 DefaultSqlSession 的 select 或者 update 等办法操作数据库了,不过还是看比拟支流的办法。

BlogMapper mapper = session.getMapper(BlogMapper.class);
  Blog blog = mapper.selectBlog(101);

首先看 getMapper 办法,调用的是 configuration 对象中 mapRegister 的 getMapper 办法

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
    if (mapperProxyFactory == null) {throw new BindingException("Type" + type + "is not known to the MapperRegistry.");
    }
    try {return mapperProxyFactory.newInstance(sqlSession);
    } catch (Exception e) {throw new BindingException("Error getting mapper instance. Cause:" + e, e);
    }
  }

所以在上一篇博客中看到哪个 addMapper 办法,寄存的是一个 MapperProxyFactory 工厂,就是因为这里每次 getMapper 会从对应的工厂中创立代理,这里是 Proxy 动静代理

@SuppressWarnings("unchecked")
  protected T newInstance(MapperProxy<T> mapperProxy) {return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface}, mapperProxy);
  }

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

先返回,此时曾经获取到了 BlogMapper 的代理对象,而后执行 selectBlog 办法,这时候会执行到之前的代理办法中,找到之前的

MapperProxy 类的 invoke 办法

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

这里必定不是 Object 类,所以执行 cachedInvoker()

private MapperMethodInvoker cachedInvoker(Method method) throws Throwable {
    try {
      return methodCache.computeIfAbsent(method, m -> {if (m.isDefault()) {
          try {if (privateLookupInMethod == null) {return new DefaultMethodInvoker(getMethodHandleJava8(method));
            } else {return new DefaultMethodInvoker(getMethodHandleJava9(method));
            }
          } catch (IllegalAccessException | InstantiationException | InvocationTargetException
              | NoSuchMethodException e) {throw new RuntimeException(e);
          }
        } else {return new PlainMethodInvoker(new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()));
        }
      });
    } catch (RuntimeException re) {Throwable cause = re.getCause();
      throw cause == null ? re : cause;
    }
  }

computeIfAbsent,这是 jdk8 的语法,大略就是看 map 中有没有这个 key,没有就新建一个并返回新建的这个,有就间接返回,所以这里就是对办法会做一个缓存。当初是第一次执行,必定是没有,所以会执行前面的创立办法。m.isDefault 这些是兼容 jdk8 以上的接口的默认办法,实现是间接运行那个默认办法。

间接看 PlainMethodInvoker,进入 new MapperMethod

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

先看 SqlCommand 的创立

public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {final String methodName = method.getName();
      final Class<?> declaringClass = method.getDeclaringClass();
      MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass,
          configuration);
      if (ms == null) {if (method.getAnnotation(Flush.class) != null) {
          name = null;
          type = SqlCommandType.FLUSH;
        } else {throw new BindingException("Invalid bound statement (not found):"
              + mapperInterface.getName() + "." + methodName);
        }
      } else {name = ms.getId();
        type = ms.getSqlCommandType();
        if (type == SqlCommandType.UNKNOWN) {throw new BindingException("Unknown execution method for:" + name);
        }
      }
    }

首先,先从 configuration 中查找出对应的 MappedStatement,查找的过程是这样的,先查看以后的类是否存在对应的 MappedStatement,如果有间接返回,否则从父类中查找是否有对应的 MappedStatement

private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName,
        Class<?> declaringClass, Configuration configuration) {String statementId = mapperInterface.getName() + "." + methodName;
      if (configuration.hasStatement(statementId)) {return configuration.getMappedStatement(statementId);
      } else if (mapperInterface.equals(declaringClass)) {return null;}
      for (Class<?> superInterface : mapperInterface.getInterfaces()) {if (declaringClass.isAssignableFrom(superInterface)) {
          MappedStatement ms = resolveMappedStatement(superInterface, methodName,
              declaringClass, configuration);
          if (ms != null) {return ms;}
        }
      }
      return null;
    }
  }

SqlCommand 创立实现之后,再看 MethodSignature 的创立

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();
      }
      // 返回类型是否是 void
      this.returnsVoid = void.class.equals(this.returnType);
      // 返回类型是否是汇合
      this.returnsMany = configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray();
      // 返回类型是否是游标
      this.returnsCursor = Cursor.class.equals(this.returnType);
      // 返回类型是否是 Optional
      this.returnsOptional = Optional.class.equals(this.returnType);
      // 如果有 @Mapkey, 返回 mapKey
      this.mapKey = getMapKey(method);
      // 是否是 Map
      this.returnsMap = this.mapKey != null;
      // 找到第几个参数是 RowBounds
      this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class);
      // 找到第几个参数是 ResultHandler
      this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class);
      // 参数解析器, 解析 @Param 中的名称
      this.paramNameResolver = new ParamNameResolver(configuration, method);
    }

创立实现之后,返回,调用 PlainMethodInvoker 的 invoke 办法,办法中调用的是 mapperMethod 的 execute

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);
          if (method.returnsOptional()
              && (result == null || !method.getReturnType().equals(result.getClass()))) {result = Optional.ofNullable(result);
          }
        }
        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;
  }

这里看 select 办法

result = sqlSession.selectOne(command.getName(), param);
@Override
  public <T> T selectOne(String statement, Object parameter) {
    // Popular vote was to return null on 0 results and throw exception on too many.
    List<T> list = this.selectList(statement, parameter);
    if (list.size() == 1) {return list.get(0);
    } else if (list.size() > 1) {throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found:" + list.size());
    } else {return null;}
  }
public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
    try {MappedStatement ms = configuration.getMappedStatement(statement);
      return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
    } catch (Exception e) {throw ExceptionFactory.wrapException("Error querying database.  Cause:" + e, e);
    } finally {ErrorContext.instance().reset();}
  }

在这里调用 executor 的 query 办法,因为这里是个装璜者对象,所以看 CachingExecutor 的 query 办法

public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {BoundSql boundSql = ms.getBoundSql(parameterObject);
    CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);
    return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
  }
@Override
  public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)
      throws SQLException {Cache cache = ms.getCache();
    if (cache != null) {flushCacheIfRequired(ms);
      if (ms.isUseCache() && resultHandler == null) {ensureNoOutParams(ms, boundSql);
        @SuppressWarnings("unchecked")
        List<E> list = (List<E>) tcm.getObject(cache, key);
        if (list == null) {list = delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
          tcm.putObject(cache, key, list); // issue #578 and #116
        }
        return list;
      }
    }
    return delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
  }

首先从 MappedStatement 中获取 Cache,这个 Cache 在解析 xml 的时候就曾经创立了,如果获取到的不是 null,那么首先执行 flushCacheIfRequired,这个是通过在解析 xml 的时候判断是不是 select 决定的,除了 select 语句都是 true, 执行革除缓存,接下来从缓存中获取,如果有缓存,间接返回,如果没有,就执行查问。

接着看委托类的实现,这个委托类的实现在 BaseExecutor 中

public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());
    if (closed) {throw new ExecutorException("Executor was closed.");
    }
    if (queryStack == 0 && ms.isFlushCacheRequired()) {clearLocalCache();
    }
    List<E> list;
    try {
      queryStack++;
      list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;
      if (list != null) {handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
      } else {list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
      }
    } finally {queryStack--;}
    if (queryStack == 0) {for (DeferredLoad deferredLoad : deferredLoads) {deferredLoad.load();
      }
      // issue #601
      deferredLoads.clear();
      if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {
        // issue #482
        clearLocalCache();}
    }
    return list;
  }

这里又是一个 Cache, 不过这个 Cache 是 mybatis 内置的 Cache,这就是常说的一级缓存,而这个一级缓存的革除,从代码上看,首先是配置了 LocalCacheScope 是 STATEMENT 的时候,默认是 Session,而后就是当执行了 close 办法的时候。

再接着往下看,如果没有命中缓存,就会继续执行查询方法

private <E> List<E> queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
    List<E> list;
    localCache.putObject(key, EXECUTION_PLACEHOLDER);
    try {list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
    } finally {localCache.removeObject(key);
    }
    localCache.putObject(key, list);
    if (ms.getStatementType() == StatementType.CALLABLE) {localOutputParameterCache.putObject(key, parameter);
    }
    return list;
  }
@Override
  public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
    Statement stmt = null;
    try {Configuration configuration = ms.getConfiguration();
      StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
      stmt = prepareStatement(handler, ms.getStatementLog());
      return handler.query(stmt, resultHandler);
    } finally {closeStatement(stmt);
    }
  }

doQuery 办法是实现类中的办法,以后是 SimpleExecutor,先看 StatementHandler 的获取

public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);
    statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);
    return statementHandler;
  }
public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {switch (ms.getStatementType()) {
      case STATEMENT:
        delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
        break;
      case PREPARED:
        delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
        break;
      case CALLABLE:
        delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
        break;
      default:
        throw new ExecutorException("Unknown statement type:" + ms.getStatementType());
    }

  }

首先看 RoutingStatementHandler,是通过不同的 StatementType 创立不同的 Handler 处理器,MappedStatement 新建默认是 PREPARED,CALLABLE 是存储过程,STATEMENT 就不说了,所以失常状况下创立的都是 PreparedStatementHandler,进入构造方法

public PreparedStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {super(executor, mappedStatement, parameter, rowBounds, resultHandler, boundSql);
  }
protected BaseStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {this.configuration = mappedStatement.getConfiguration();
    this.executor = executor;
    this.mappedStatement = mappedStatement;
    this.rowBounds = rowBounds;

    this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
    this.objectFactory = configuration.getObjectFactory();

    if (boundSql == null) { // issue #435, get the key before calculating the statement
      generateKeys(parameterObject);
      boundSql = mappedStatement.getBoundSql(parameterObject);
    }

    this.boundSql = boundSql;

    this.parameterHandler = configuration.newParameterHandler(mappedStatement, parameterObject, boundSql);
    this.resultSetHandler = configuration.newResultSetHandler(executor, mappedStatement, rowBounds, parameterHandler, resultHandler, boundSql);
  }

重点是最初两行,参数处理器和后果处理器的创立

public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
    parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
    return parameterHandler;
  }
public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler,
      ResultHandler resultHandler, BoundSql boundSql) {ResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);
    resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);
    return resultSetHandler;
  }

创立之后,两者都有一个操作,就是应用 interceptorChain.pluginAll 进行了包装代理,

返回到 newStatementHandler,interceptorChain.pluginAll 对 RoutingStatementHandler 同样做了一个包装代理, 持续返回

再往下看 prepareStatement 办法

private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
    Statement stmt;
    Connection connection = getConnection(statementLog);
    stmt = handler.prepare(connection, transaction.getTimeout());
    handler.parameterize(stmt);
    return stmt;
  }

首先获取一个连贯,接着执行 handler 的 prepare 办法,办法中调用的是委托类也就是 PrepareStatementHandler 的 prepare 办法,其是由父类实现的

@Override
  public Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException {ErrorContext.instance().sql(boundSql.getSql());
    Statement statement = null;
    try {statement = instantiateStatement(connection);
      setStatementTimeout(statement, transactionTimeout);
      setFetchSize(statement);
      return statement;
    } catch (SQLException e) {closeStatement(statement);
      throw e;
    } catch (Exception e) {closeStatement(statement);
      throw new ExecutorException("Error preparing statement.  Cause:" + e, e);
    }
  }

这就是创立了一个 JDBC 的 statement, 接下来返回继续执行 parameterize 办法

public void parameterize(Statement statement) throws SQLException {parameterHandler.setParameters((PreparedStatement) statement);
  }

能够看到调用的是之前创立的 ParameterHandler 的 setParameters 办法,把参数设置到 statement 中,这里须要留神的是,尽管 ParameterHandler 被 plugins 代理比 RoutingStatementHandler 晚,然而实际上 ParameterHandler 办法的调用是在前面,所以拦挡的程序也在前面。

再往下看,接着会调用 query 办法

public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {PreparedStatement ps = (PreparedStatement) statement;
    ps.execute();
    return resultSetHandler.handleResultSets(ps);
  }

这里就是 JDBC 的执行了,最初看 ResultSetHandler 执行的 handleResultSets 办法

public List<Object> handleResultSets(Statement stmt) throws SQLException {ErrorContext.instance().activity("handling results").object(mappedStatement.getId());

    final List<Object> multipleResults = new ArrayList<>();

    int resultSetCount = 0;
    ResultSetWrapper rsw = getFirstResultSet(stmt);

    List<ResultMap> resultMaps = mappedStatement.getResultMaps();
    int resultMapCount = resultMaps.size();
    validateResultMapsCount(rsw, resultMapCount);
    while (rsw != null && resultMapCount > resultSetCount) {ResultMap resultMap = resultMaps.get(resultSetCount);
      handleResultSet(rsw, resultMap, multipleResults, null);
      rsw = getNextResultSet(stmt);
      cleanUpAfterHandlingResultSet();
      resultSetCount++;
    }

    String[] resultSets = mappedStatement.getResultSets();
    if (resultSets != null) {while (rsw != null && resultSetCount < resultSets.length) {ResultMapping parentMapping = nextResultMaps.get(resultSets[resultSetCount]);
        if (parentMapping != null) {String nestedResultMapId = parentMapping.getNestedResultMapId();
          ResultMap resultMap = configuration.getResultMap(nestedResultMapId);
          handleResultSet(rsw, resultMap, null, parentMapping);
        }
        rsw = getNextResultSet(stmt);
        cleanUpAfterHandlingResultSet();
        resultSetCount++;
      }
    }

    return collapseSingleResultList(multipleResults);
  }

这段代码大略就是获取出 resultsmap,而后对后果进行解析,所以重点是 handeResultSet 办法

private void handleResultSet(ResultSetWrapper rsw, ResultMap resultMap, List<Object> multipleResults, ResultMapping parentMapping) throws SQLException {
    try {if (parentMapping != null) {handleRowValues(rsw, resultMap, null, RowBounds.DEFAULT, parentMapping);
      } else {if (resultHandler == null) {DefaultResultHandler defaultResultHandler = new DefaultResultHandler(objectFactory);
          handleRowValues(rsw, resultMap, defaultResultHandler, rowBounds, null);
          multipleResults.add(defaultResultHandler.getResultList());
        } else {handleRowValues(rsw, resultMap, resultHandler, rowBounds, null);
        }
      }
    } finally {// issue #228 (close resultsets)
      closeResultSet(rsw.getResultSet());
    }
  }

首先判断 parentMapping 是否 null,null 就是最外层的 resultMap, 而后判断是否有自定义的 resultHandler,有的话用自定义的,没有就用默认的,解析实现之后返回,一路回到 SimpleExecutor 中,最初会执行 closeStatement 办法敞开连贯。

返回后果,执行完结,mybatis 的执行流程也就完结了。

 

关注公众号:java 宝典

退出移动版