MyBatis3.3.0文档学习总结(四) --Executor
来源:互联网 发布:sql 复制表 编辑:程序博客网 时间:2024/06/05 07:44
1:openSessionFromDataSource
通过openSession()最终调用的是 openSessionFromDataSource,源码如下:
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null; try { final Environment environment = configuration.getEnvironment(); final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); final Executor executor = configuration.newExecutor(tx, execType); return new DefaultSqlSession(configuration, executor, autoCommit); } catch (Exception e) { closeTransaction(tx); // may have fetched a connection so lets call close() throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
说明:ExecutorType(执行器类型)类型有 SIMPLE(默认),REUSE,BATCH。
TransactionIsolationLevel 隔离级别5种 NONE(Connection.TRANSACTION_NONE), READ_COMMITTED(Connection.TRANSACTION_READ_COMMITTED), READ_UNCOMMITTED(Connection.TRANSACTION_READ_UNCOMMITTED), REPEATABLE_READ(Connection.TRANSACTION_REPEATABLE_READ), SERIALIZABLE(Connection.TRANSACTION_SERIALIZABLE);
通过源码看出首先是从configuration获取environment,接着创建Transaction,最后通过 configuration.newExecutor创建Executor。
public Executor newExecutor(Transaction transaction, ExecutorType executorType) { executorType = executorType == null ? defaultExecutorType : executorType; executorType = executorType == null ? ExecutorType.SIMPLE : executorType; Executor executor; //根据executorType创建不同的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 (cacheEnabled) { executor = new CachingExecutor(executor); } executor = (Executor) interceptorChain.pluginAll(executor); return executor; }
根据executorType创建对应的Executor,从源码可以看出他有BatchExecutor、ReuseExecutor、CachingExecutor、SimpleExecutor。那么Executor是做什么的呢?
2:Executor
Executor是接口,是对于Statement的封装,我们看下Executor,他是真正执行sql的地方。
public interface Executor { ResultHandler NO_RESULT_HANDLER = null; int update(MappedStatement ms, Object parameter) throws SQLException; <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException; <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException; List<BatchResult> flushStatements() throws SQLException; void commit(boolean required) throws SQLException; void rollback(boolean required) throws SQLException; CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql); boolean isCached(MappedStatement ms, CacheKey key); void clearLocalCache(); void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType); Transaction getTransaction(); void close(boolean forceRollback); boolean isClosed(); void setExecutorWrapper(Executor executor);}
上面源码我可以看到Executor接口定义了update 、query、commit、rollback等方法.我们拿其中的SimpleExecutor类里面的doQuery方法看下源码.
@Override public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { Statement stmt = null; try { Configuration configuration = ms.getConfiguration(); StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql); stmt = prepareStatement(handler, ms.getStatementLog()); return handler.<E>query(stmt, resultHandler); } finally { closeStatement(stmt); } }
我们看看StatementHandler 的一个实现类 PreparedStatementHandler(这也是我们最常用的,封装的是PreparedStatement), 看看它使怎么去处理的:
@Override public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException { PreparedStatement ps = (PreparedStatement) statement; ps.execute(); return resultSetHandler.<E> handleResultSets(ps); }
以上是sql底层执行的基本流程,说的直白一点就是所以sql底层都交给了Excutor。
0 0
- MyBatis3.3.0文档学习总结(四) --Executor
- MyBatis3.3.0文档学习总结(二)
- MyBatis3.3.0文档学习总结(三) --sqlSession深入源码
- MyBatis3.3.0文档学习总结(三续) --sqlSession深入源码
- Mybatis3源码分析(四):总结
- Mybatis3.3.0异常总结
- Java Executor 框架学习总结
- Java Executor 框架学习总结
- Java Executor 框架学习总结
- Java Executor 框架学习总结
- mybatis3学习笔记(一)
- 学习总结(四)
- Mybatis3学习
- Halcon PDF文档(hdevelop_users_guide)学习总结之四——关于3D显示模式
- MyBatis学习总结(8)——Mybatis3.x与Spring4.x整合
- MyBatis3官方文档地址
- 跟着angularjs2官方文档学习(四)
- Cassandra文档学习(四:cassandra.yaml)
- Android--SQLite数据库分页
- 线程池调整真的很重要
- redis2
- 龙书D3D11 Demo配置(VS2015+win10)之Init Direct3D项目
- 页面背景图片
- MyBatis3.3.0文档学习总结(四) --Executor
- 初了解—直流电机
- windows下安装php_memcache扩展
- 转:JSP中out.write()和out.print()的区别
- ajax实现给JavaScript中全局变量赋值
- 获取 路径n中方法和方式 + 文件的读取。。
- CSS3下的@font-face规则
- 3n+1猜想 (有错·应使用递归)
- Caused by: org.apache.ibatis.binding.BindingException: Parameter 'offset' not found. Availabl