在SimpleExecutor中,执行SQL时调用preareStatement()方法来对statement进行初始化及参数设置。
private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
Statement stmt;
Connection connection = getConnection(statementLog);
//初始化
stmt = handler.prepare(connection);
//参数设置`
handler.parameterize(stmt);
return stmt;
}
这里PreparedStatementHandler为例。详细分析这两个过程。
Statement初始化
这是BaseStatementHandler.prepare()方法
public Statement prepare(Connection connection) throws SQLException {
ErrorContext.instance().sql(boundSql.getSql());
Statement statement = null;
try {
//通过connection得到一个statement
statement = instantiateStatement(connection);
//设置执行超时时间
setStatementTimeout(statement);
setFetchSize(statement);
return statement;
} catch (SQLExcept