关于mybatis里面的Executor
来源:互联网 发布:网络神曲排行榜2017 编辑:程序博客网 时间:2024/05/06 15:59
使用mybatis查寻数据,跟踪其执行流程
这里用到了java的动态代理,详细可以见java api,有详细的说明
继续跟踪可以找到DefaultSqlSessionFactory里面的该方法
通过
CachingExecutor可以使mybatis先从缓存中提取数据,数据缓存中没有数据时才从数据库里面提取数据
最开始执行的语句
this.getSqlSession().selectList("QUERY-QUESTION", data, rowBounds);
这里需要找到sqlsession是从哪里来的
getSqlSession是SqlSessionDaoSupport类里面的方法,该类通过spring的自动注入可以把sqlSessionTemplate注入进来,当然这里的sqlSessionTemplate是需要spring配置的
@Autowired(required = false) public final void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSession = sqlSessionTemplate; this.externalSqlSession = true; }
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg index="0" ref="sqlSessionFactory"/><constructor-arg index="1" value="BATCH"/></bean>
@Autowired(required = false)是通过类型匹配来注入的,如果没有找到相应对,就不用注入
所以selectList方法为SqlSessionTemlate里面的,再看SqlSessionTemplage,里面的都是通过sqlSessionProxy来执行selectList方法的,也就是通过代理方式来的
public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator) { notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required"); notNull(executorType, "Property 'executorType' is required"); this.sqlSessionFactory = sqlSessionFactory; this.executorType = executorType; this.exceptionTranslator = exceptionTranslator; this.sqlSessionProxy = (SqlSession) newProxyInstance( SqlSessionFactory.class.getClassLoader(), new Class[] { SqlSession.class }, new SqlSessionInterceptor()); }
这里用到了java的动态代理,详细可以见java api,有详细的说明
SqlSessionInterceptor实现了InvocationHandler,在invoke方法里面的开始有这样代码,那里是真正的sqlsession
final SqlSession sqlSession = getSqlSession( SqlSessionTemplate.this.sqlSessionFactory, SqlSessionTemplate.this.executorType, SqlSessionTemplate.this.exceptionTranslator);
跟踪geteSqlSession可以找到他的创建来源,见
SqlSession session = sessionFactory.openSession(executorType);
继续跟踪可以找到DefaultSqlSessionFactory里面的该方法
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, autoCommit); return new DefaultSqlSession(configuration, executor); } 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(); } }
通过
final Executor executor = configuration.newExecutor(tx, execType, autoCommit);
你就知道executor是怎么回来的了
mybatis默认使用了cache,在创建exector时,外面就包了一层CacheExecutor,详细见
public Executor newExecutor(Transaction transaction, ExecutorType executorType, boolean autoCommit) { executorType = executorType == null ? defaultExecutorType : executorType; executorType = executorType == null ? ExecutorType.SIMPLE : executorType; Executor 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, autoCommit); } executor = (Executor) interceptorChain.pluginAll(executor); return executor; }
CachingExecutor可以使mybatis先从缓存中提取数据,数据缓存中没有数据时才从数据库里面提取数据
- 关于mybatis里面的Executor
- 关于mybatis里面的Executor
- Mybatis的Executor介绍(一)
- 关于AsyncTask的Executor的一些研究
- Mybatis中的Executor
- Mybatis 之 Executor
- Mybatis的Executor介绍(二)——批处理
- mybatis里面的时间段写法
- mybatis里面的模糊查询
- mybatis 动态sql里面关于时间区间查询的表示方式
- Java Executor并发框架(十三)Executor框架线程池关于异常的处理
- Mybatis执行Executor(一)
- 关于Executor 接口
- 关于spark Executor OOM的问题引发的联想
- 关于java的future和Executor接口的理解
- Topology运行的秘密:关于worker,executor和task
- 关于Executor框架相关接口和类的作用
- Mybatis 里面ResultMap的使用情况
- 使用TextMate打开GB2312,BIG等亚洲编码文件
- USACO 蔡勒公式 (计算任何一日属一星期中哪一日的算法)
- javascript中几种页面跳转方式
- Oracle中快速导入导出数据库的几种方式
- 何谓OHCI、UHCI、EHCI?
- 关于mybatis里面的Executor
- Kinect调查报告(一)
- 程序员修炼之路(五)李开复:算法的力量|万变不离其宗
- java对象序列化的概念和实现
- ArcGIS Engine10.0轻松入门级教程(4)——基本功能开发
- 句柄
- Delphi发展历程
- Kinect调查报告(二)
- linux设备驱动之I/O端口与I/O内存