组件学习
KeyGenerator
image.png
ResultHandler
image.png
BaseBuilder
image.png
SqlNode
image.png
TokenHandler
image.png
ResultSetHandler
image.png
StatementHandler
image.png
PreparedStatementHandler
public List query(Statement statement, ResultHandler resultHandler)
throws SQLException {
PreparedStatement ps = (PreparedStatement) statement;
// 看到了熟悉的jdbc的代码 :)
ps.execute();
//resultSetHandler 处理返回结果
return resultSetHandler.handleResultSets(ps);
}
Cache
image.png
plugin 钩子
interceptorChain.pluginAll(statementHandler)
public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) {
StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler);
statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);
return statementHandler;
}
SimpleExecutor
public int doUpdate(MappedStatement ms, Object parameter)
throws SQLException {
Statement stmt = null;
try {
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null);
stmt = prepareStatement(handler);
return handler.update(stmt);
} finally {
closeStatement(stmt);
}
}
public List doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
Statement stmt = null;
try {
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, rowBounds, resultHandler);
stmt = prepareStatement(handler);
return handler.query(stmt, resultHandler);
} finally {
closeStatement(stmt);
}
}
开始调用到jdbc 操作
- class::BaseStatementHandler
public Statement prepare(Connection connection)
throws SQLException {
ErrorContext.instance().sql(boundSql.getSql());
Statement statement = null;
try {
statement = instantiateStatement(connection);
setStatementTimeout(statement);
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);
}
}
TypeDiscriminator 类型鉴别器
调用堆栈
- selectOne
image.png