/**
* @author Clinton Begin
*/
public abstract class BaseStatementHandler implements StatementHandler {
protected final Configuration configuration;
protected final ObjectFactory objectFactory;
protected final TypeHandlerRegistry typeHandlerRegistry;
protected final ResultSetHandler resultSetHandler;
protected final ParameterHandler parameterHandler;
protected final Executor executor;
protected final MappedStatement mappedStatement;
protected final RowBounds rowBounds;
protected BoundSql boundSql;
protected BaseStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
this.configuration = mappedStatement.getConfiguration();
this.executor = executor;
this.mappedStatement = mappedStatement;
this.rowBounds = rowBounds;
this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
this.objectFactory = configuration.getObjectFactory();
if (boundSql == null) { // issue #435, get the key before calculating the statement
generateKeys(parameterObject);
boundSql = mappedStatement.getBoundSql(parameterObject);
}
this.boundSql = boundSql;
this.parameterHandler = configuration.newParameterHandler(mappedStatement, parameterObject, boundSql);
this.resultSetHandler = configuration.newResultSetHandler(executor, mappedStatement, rowBounds, parameterHandler, resultHandler, boundSql);
}
public BoundSql getBoundSql() {
return boundSql;
}
public ParameterHandler getParameterHandler() {
return parameterHandler;
}
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);
}
}
protected abstract Statement instantiateStatement(Connection connection) throws SQLException;
protected void setStatementTimeout(Statement stmt) throws SQLException {
Integer timeout = mappedStatement.getTimeout();
Integer defaultTimeout = configuration.getDefaultStatementTimeout();
if (timeout != null) {
stmt.setQueryTimeout(timeout);
} else if (defaultTimeout != null) {
stmt.setQueryTimeout(defaultTimeout);
}
}
protected void setFetchSize(Statement stmt) throws SQLException {
Integer fetchSize = mappedStatement.getFetchSize();
if (fetchSize != null) {
stmt.setFetchSize(fetchSize);
}
}
protected void closeStatement(Statement statement) {
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
//ignore
}
}
protected void generateKeys(Object parameter) {
KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
ErrorContext.instance().store();
keyGenerator.processBefore(executor, mappedStatement, null, parameter);
ErrorContext.instance().recall();
}
}
mybatis源码之BaseStatementHandler
原创归田归田 博主文章分类:Mybatis入门及源码学习 ©著作权
文章标签 BaseStatementHandler sql 文章分类 云平台 云计算
©著作权归作者所有:来自51CTO博客作者归田归田的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
一文带你理解透MyBatis源码
MyBatis几乎成为了Java开发人员必须深入掌握的框架技术,今天,我们就一起来深入分析MyBatis源码。
sql 增删改查 数据库 JDK JVM -
Mybatis源码之Statement处理器BaseStatementHandler(二)
BaseStatementHandler是一个抽象类,并没有实现和CURD相关的类,只是更多的设置
mybatis StatementHandler BaseStatementHandler sql 实例化 -
myBatis源码之XMLConfigBuilder
/** * @author Clinton Begin */public class XMLConfigBuilder extends BaseBuilder { privder(R
mybatis XMLConfigBuilder xml 子节点 sed -
mybatis源码之SimpleStatementHandler
/** * @author Clinton Begin */public class SimpleStatementHandler extends BaseSta
SimpleStatementHandl sql ide sql语句 -
myBatis源码之Configuration
/** * @author Clinton Begin */ //mybatis配置文件对应的类,包含了配置
mybatis Configuration 数据 java xml -
Mybatis 源码分析之statement
结构执行顺序源码分析:org.apache.ibatis.executor.SimpleExecutor#doQuery参数处理:javabean =》 jdbc 参数
1024程序员节 statement mybaits 源码 sql -
myBatis源码学习之SqlSessionFactoryBuilder
/* * Builds {@link SqlSession} instances. * *//** * @author Clinton Begin */public class SqlSessi, n
SqlSessionFactoryBui mybatis sql xml 配置文件 -
myBatis源码学习之SqlSessionFactory
/** * Creates an {@link SqlSesion} out of a connection or a DataSource * * @author Cli
SqlSessionFactory mybatis sql 不可重复读 缓存