我们接着上篇文章来分析,上篇文章我们分析到doQuery()方法
1.1 SimpleExecutor
@Override
public List 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);
}
}
上面这段代码是 SimpleExecutor 中负责查询的,我们可以看到 该方法调用的是 StatementHandler 接口的query(stmt, resultHandler); 方法来执行的查询,并返回的是一个List 集合。接下来我们就来看一下StatementHandler 的query 方法。
1.2 StatementHandler
/**
* @author Clinton Begin
接口定义
*/
public interface StatementHandler {
Statement prepare(Connection connection, Integer transactionTimeout)
throws SQLException;
void parameterize(Statement statement)
throws SQLException;
void batch(Statement statement)
throws SQLException;
int update(Statement statement)
throws SQLException;
List query(Statement statement, ResultHandler resultHandler)
throws SQLException;
Cursor queryCursor(Statement statement)
throws SQLException;
BoundSql getBoundSql();
ParameterHandler getParameterHandler();
}
上面代码是 StatementHandler 接口的顶层定义,我们通过类图来看一下实现类:
看到这个类图结构是否很熟悉,前面我们分析的 Executor 接口也是这样的类图结构,所以我们可以猜测 StatementHandler 在整个实现 query() 方法逻辑中也使用的 委派模式和模板方法。我们在这里 不需要知道这个类图上的每个类都是什么意思,我们不关注这个,我们关注的是 整个 结构体系,然后有目的的去 看具体方法干了什么事,比如我们当前是关注 查询语句的执行,那么我们就只看 有关查询的逻辑就可以了。
1.3 StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
// doQuery() 中创建 的StatementHandler
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
我们需要从这句代码入手,找到StatementHandler 具体的实现类是哪一个,这就是我们的目的,找到具体的实现类以后,才知道 handler.query(stmt, resultHandler);这句代码应该是怎么样的逻辑。接下来我们就来看看 newStatementHandler() 方法。
1.4 configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
//又回到了 configuration对象了
configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
// Configuration
public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
// RoutingStatementHandler
StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);
statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);
return statementHandler;
}
上面这段代码就是 创建StatementHandler 对象的逻辑。逻辑简单 我们可以看出 是 StatementHandler -> RoutingStatementHandler 。所以我们接下来可以去看 RoutingStatementHandler 中的 query() 方法了。
1.5 RoutingStatementHandler
/**
* @author Clinton Begin
*/
public class RoutingStatementHandler implements StatementHandler {
private final StatementHandler delegate;
public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
switch (ms.getStatementType()) {// PREPARED,这里我是通过debug 得出的答案,
//我们会在后面的文章中分析 原因,
// 大致思路是 通过传入的 statement 如(User user = sqlSession.selectOne("com.example.bootmybatis.dao.UserMapper.selectByPrimaryKey", 1);)
//statement=com.example.bootmybatis.dao.UserMapper.selectByPrimaryKey
// 会去拿到 map 集合(从配置文件中初始化的)中对应的那个 MappedStatement 对象,该对象里就封装了statementType 。
case STATEMENT:
delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
break;
case PREPARED:// 初始这里,这里也采用了策略模式来获取 StatementHandler 对象。通过 外部传入的 type 创建不同的对象。
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());
}
}
@Override
public Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException {
return delegate.prepare(connection, transactionTimeout);
}
@Override
public void parameterize(Statement statement) throws SQLException {
delegate.parameterize(statement);
}
@Override
public void batch(Statement statement) throws SQLException {
delegate.batch(statement);
}
@Override
public int update(Statement statement) throws SQLException {
return delegate.update(statement);
}
// 执行 查询,这里我们就知道 delegate-> PreparedStatementHandler。
@Override
public List query(Statement statement, ResultHandler resultHandler) throws SQLException {
return delegate.query(statement, resultHandler);
}
@Override
public Cursor queryCursor(Statement statement) throws SQLException {
return delegate.queryCursor(statement);
}
@Override
public BoundSql getBoundSql() {
return delegate.getBoundSql();
}
@Override
public ParameterHandler getParameterHandler() {
return delegate.getParameterHandler();
}
}
这里我们就知道 delegate-> PreparedStatementHandler。所以 query方法中的具体delegate.query(statement, resultHandler);我们应该是 去看 PreparedStatementHandler的对应的query方法。 后面我们会继续分析。到这里我们分析出 了PreparedStatementHandler,从名称上看我们就知道里面肯定有 PreparedStatement 对象。