乐趣区

关于mysql:Mybatis源码分析六执行sql

获取 SqlSession 后,下一步就是执行 sql.

User user=sqlSession.selectOne("last.soul.mapper.UserMapper.selectById",map);

DefaultSqlSession 的次要性能就是实现增删改查性能,以及它们的重载办法。就查问来说,最初都会调用 select 办法,而后改装成 selectOne,selectMap 等,代码如下:

/**
   * 
   * @param statement sql 语句 ID=xxxMapper.xml 文件中的 namespace+sql 标签的 id.
   *                  如:last.soul.mapper.UserMapper.selectById
   * @param parameter sql 的参数
   * @param rowBounds 分页信息
   * @param <E>
   * @return
   */
  @Override
  public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
    try {
      //sql 相干音讯的包装对象,比方:sql 语句,返回类型,是否应用缓存等
      MappedStatement ms = configuration.getMappedStatement(statement);
      // 执行 sql
      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 是 CachingExecutor, 执行的是 CachingExecutor 的 query 办法。

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;
      }
    }
    //delegate 是 SimpleExecutor 对象
    return delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
  }

query 办法中次要逻辑是判断是否有缓存,有缓存返回缓存值,否则执行 delegate 对象的 query 办法,即 SimpleExecutor 对象的 query 办法,然而你会发现该类中没有些办法,那它执行的就是他的父类 BaseExecutor 的 query 办法。query 办法又会调用它的公有办法 queryFromDatabase。

queryFromDatabase 源代码如下:

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

其中最要害的代码就是调用了子类 (SimpleExecutor 类,CachingExecutor 类没有实现该办法) 的 doQuery 办法。源代码如下:

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

这个办法的次要逻辑是创立 StatementHandler,而后调用它的 query 办法。关上 configuration.newStatementHandler, 咱们发现生成的是 RoutingStatementHandler 对象。源代码如下:

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

所以 SimpleExecutor 类 doQuery()办法最初一行执行的 query 办法是执行的咱们发现生成的是 RoutingStatementHandler 看的 query 办法。它的源代码如下:

public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {return delegate.query(statement, resultHandler);
  }

delegate 代理的是哪个 StatementHandler 对象呢? 这个对象在 RoutingStatementHandler 构造方法中赋值的。

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

  }

ms.getStatementType()在本例中应用的是默认值。MappedStatement 的外部类 Builder 源代码中。

      mappedStatement.statementType = StatementType.PREPARED;

所以 delegate 为 PreparedStatementHandler 对象,最终调用的也是 PreparedStatementHandler 下的 query 办法,源代码如下:

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

逻辑比拟清晰,1、执行 sql。2、解决返回后果。

退出移动版