mybatis 源码分析二

时间:2024-10-11 19:37:38

1.SqlSession下的四大对象

Executor、StatementHandler、ParameterHandler、ResultSetHandler

StatementHandler的作用是使用数据库的Statement(PreparedStatement)执行操作

ParameterHandler是用来处理SQL参数的

ResultSetHandler是进行数据集的封装返回处理的

2.mybatis中有3中执行器:

SIMPLE——简易执行器,默认

REUSE——是一种能够执行重用预处理语句的执行器

BATCH ——执行器重用语句和批量更新,批量专用的执行器

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null; DefaultSqlSession var8;
try {
Environment environment = this.configuration.getEnvironment();
TransactionFactory transactionFactory = this.getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
Executor executor = this.configuration.newExecutor(tx, execType, autoCommit);
var8 = new DefaultSqlSession(this.configuration, executor);
} catch (Exception var12) {
this.closeTransaction(tx);
throw ExceptionFactory.wrapException("Error opening session. Cause: " + var12, var12);
} finally {
ErrorContext.instance().reset();
} return var8;
}

  

public Executor newExecutor(Transaction transaction, ExecutorType executorType, boolean autoCommit) {
executorType = executorType == null ? this.defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Object executor;
if (ExecutorType.BATCH == executorType) {
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this, transaction);
} else {
executor = new SimpleExecutor(this, transaction);
} if (this.cacheEnabled) {
executor = new CachingExecutor((Executor)executor, autoCommit);
} Executor executor = (Executor)this.interceptorChain.pluginAll(executor);
return executor;
}