/**
* @author Clinton Begin
*/
public class ReuseExecutor extends BaseExecutor {
private final Map<String, Statement> statementMap = new HashMap<String, Statement>();
public ReuseExecutor(Configuration configuration, Transaction transaction) {
super(configuration, transaction);
}
@Override
public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
Statement stmt = prepareStatement(handler, ms.getStatementLog());
return handler.update(stmt);
}
@Override
public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
Statement stmt = prepareStatement(handler, ms.getStatementLog());
return handler.<E>query(stmt, resultHandler);
}
@Override
public List<BatchResult> doFlushStatements(boolean isRollback) throws SQLException {
for (Statement stmt : statementMap.values()) {
closeStatement(stmt);
}
statementMap.clear();
return Collections.emptyList();
}
private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
Statement stmt;
BoundSql boundSql = handler.getBoundSql();
String sql = boundSql.getSql();
if (hasStatementFor(sql)) {
stmt = getStatement(sql);
} else {
Connection connection = getConnection(statementLog);
stmt = handler.prepare(connection);
putStatement(sql, stmt);
}
handler.parameterize(stmt);
return stmt;
}
private boolean hasStatementFor(String sql) {
try {
return statementMap.keySet().contains(sql) && !statementMap.get(sql).getConnection().isClosed();
} catch (SQLException e) {
return false;
}
}
private Statement getStatement(String s) {
return statementMap.get(s);
}
private void putStatement(String sql, Statement stmt) {
statementMap.put(sql, stmt);
}
}
mybati源码之ReuseExecutor
原创归田归田 博主文章分类:Mybatis入门及源码学习 ©著作权
©著作权归作者所有:来自51CTO博客作者归田归田的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
MySQL全文索引源码剖析之Insert语句执行过程
全文索引是信息检索领域的一种常用的技术手段,用于全文搜索问题。
全文索引 mysql MySQL Insert语句 -
Mybatis执行ReuseExecutor(五)
ReuseExecutor顾名思义就是重复使用执行,其定义了一个Map,将执行的sql作为key,
mybatis executor ReuseExecutor sql sql语句 -
MyBatis源码-解读Executor的三个实现类之ReuseExecutor(重用执行器)
文章目录PreExecutor 执行器接口继承关系ReuseExecutor(重用执行器)
ReuseExecutor 重用执行器 mybatis Executor sql -
JDK源码之HashMap源码解析
HashMap
JavaHashMap JDK -
JDK源码之TreeMap源码解析
JDK源码之TreeMap源码解析
Java TreeMap 源码 -
AutoLine源码分析之入口源码
什么是AutoLine开源平台AutoLine开源平台是一个开源自动化测试解决方案
python java linux mysql android