原文地址:http://blog.csdn.net/w_intercool/article/details/7893344

使用mybatis查寻数据,跟踪其执行流程

最开始执行的语句

[java] view plaincopyprint?
  1. this.getSqlSession().selectList("QUERY-QUESTION", data, rowBounds);

这里需要找到sqlsession是从哪里来的

getSqlSession是SqlSessionDaoSupport类里面的方法,该类通过spring的自动注入可以把sqlSessionTemplate注入进来,当然这里的sqlSessionTemplate是需要spring配置的

[java] view plaincopyprint?
  1. @Autowired(required = false)
  2. public final void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
  3. this.sqlSession = sqlSessionTemplate;
  4. this.externalSqlSession = true;
  5. }
[html] view plaincopyprint?
  1. <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  2. <constructor-arg index="0" ref="sqlSessionFactory"/>
  3. <constructor-arg index="1" value="BATCH"/>
  4. </bean>

@Autowired(required = false)是通过类型匹配来注入的,如果没有找到相应对,就不用注入

所以selectList方法为SqlSessionTemlate里面的,再看SqlSessionTemplage,里面的都是通过sqlSessionProxy来执行selectList方法的,也就是通过代理方式来的

[java] view plaincopyprint?
  1. public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
  2. PersistenceExceptionTranslator exceptionTranslator) {
  3. notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");
  4. notNull(executorType, "Property 'executorType' is required");
  5. this.sqlSessionFactory = sqlSessionFactory;
  6. this.executorType = executorType;
  7. this.exceptionTranslator = exceptionTranslator;
  8. this.sqlSessionProxy = (SqlSession) newProxyInstance(
  9. SqlSessionFactory.class.getClassLoader(),
  10. new Class[] { SqlSession.class },
  11. new SqlSessionInterceptor());
  12. }

这里用到了java的动态代理,详细可以见java api,有详细的说明

SqlSessionInterceptor实现了InvocationHandler,在invoke方法里面的开始有这样代码,那里是真正的sqlsession

[java] view plaincopyprint?
  1. final SqlSession sqlSession = getSqlSession(
  2. SqlSessionTemplate.this.sqlSessionFactory,
  3. SqlSessionTemplate.this.executorType,
  4. SqlSessionTemplate.this.exceptionTranslator);

跟踪geteSqlSession可以找到他的创建来源,见

[java] view plaincopyprint?
  1. SqlSession session = sessionFactory.openSession(executorType);

继续跟踪可以找到DefaultSqlSessionFactory里面的该方法

[java] view plaincopyprint?
  1. private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
  2. Transaction tx = null;
  3. try {
  4. final Environment environment = configuration.getEnvironment();
  5. final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
  6. tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
  7. final Executor executor = configuration.newExecutor(tx, execType, autoCommit);
  8. return new DefaultSqlSession(configuration, executor);
  9. } catch (Exception e) {
  10. closeTransaction(tx); // may have fetched a connection so lets call close()
  11. throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
  12. } finally {
  13. ErrorContext.instance().reset();
  14. }
  15. }

通过

[java] view plaincopyprint?
  1. final Executor executor = configuration.newExecutor(tx, execType, autoCommit);

你就知道executor是怎么回来的了

mybatis默认使用了cache,在创建exector时,外面就包了一层CacheExecutor,详细见

[java] view plaincopyprint?
  1. public Executor newExecutor(Transaction transaction, ExecutorType executorType, boolean autoCommit) {
  2. executorType = executorType == null ? defaultExecutorType : executorType;
  3. executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
  4. Executor executor;
  5. if (ExecutorType.BATCH == executorType) {
  6. executor = new BatchExecutor(this, transaction);
  7. } else if (ExecutorType.REUSE == executorType) {
  8. executor = new ReuseExecutor(this, transaction);
  9. } else {
  10. executor = new SimpleExecutor(this, transaction);
  11. }
  12. if (cacheEnabled) {
  13. executor = new CachingExecutor(executor, autoCommit);
  14. }
  15. executor = (Executor) interceptorChain.pluginAll(executor);
  16. return executor;
  17. }

CachingExecutor可以使mybatis先从缓存中提取数据,数据缓存中没有数据时才从数据库里面提取数据。

转载于:https://www.cnblogs.com/davidwang456/p/4693090.html

关于mybatis里面的Executor--转载相关推荐

  1. Netty里面的Boss和Worker【Server篇】

    转载地址:https://my.oschina.net/bieber/blog/406799 最近在总结Dubbo关于Netty通信方面的实现,于是也就借此机会深入体会了一下Netty.一般启动Net ...

  2. PyTorch里面的torch.nn.Parameter()

    在刷官方Tutorial的时候发现了一个用法self.v = torch.nn.Parameter(torch.FloatTensor(hidden_size)),看了官方教程里面的解释也是云里雾里, ...

  3. jQuery里面的datepicker日期控件默认是显示英文的,如何显示中文或其他语言呢?...

    jQuery里面的datepicker日期控件默认是显示英文的,如何让他显示中文或其他呢? [官方的写法]: (1)引入JS文件: <script type="text/javascr ...

  4. Angularjs $scope 里面的$apply 方法 和 $watch 方法

    Angularjs $scope 里面的$apply 方法 和 $watch 方法 学习要点: 1. Angularjs $scope 里面的$apply 方法 2. Angularjs $scope ...

  5. 对于C#里面的this与base

    如果说你父类里面有一个成员比如int a;那么你子类里面也可以再定义一个int a,这个时候base.a和this.a表示的就不是一个变量了,如果说子类里面没有,那么base.a和this.a表示的都 ...

  6. 开启Windows8里面的Hyper-V虚拟机功能

    首先了解下什么是Hyper-V?也就是虚拟化技术,允许终端用户在同一台机器上运行多个操作系统,支持32位和64位系统,可以直接在Windows 8上创建自己的虚拟机.开启Hyper-V虚拟机需要更多的 ...

  7. js中json法创建对象(json里面的:相当于js里面的=)

    js中json法创建对象(json里面的:相当于js里面的=) 一.总结 json里面的:相当于js里面的= 4.json创建js对象解决命名冲突:多个人为同一个页面写js的话,命名冲突就有可能发生, ...

  8. 【转】详谈for循环里面的break和continue语句

    https://www.jb51.net/article/119099.htm 转载  更新时间:2017年07月20日 07:53:06   投稿:jingxian   我要评论 下面小编就为大家带 ...

  9. IOS -- UICollectionView里面的cell点击,点击一个cell改变其他cell的状态

    view.m - (void)initWithCreatorCollectView {//模拟数据 [self imitateData];float width = ([UIScreen mainSc ...

最新文章

  1. mysql model first,一个支持 CodeFirst/DbFirst/ModelFirst 的数据库小工具
  2. 如何让云存储更好为“我”所用
  3. MeteoInfoLab脚本示例:Streamline流线图
  4. xgboost学习率不能大于1的原因
  5. springboot 历史版本文档_Springboot看视频学太慢?来看看这份阿里深入实践Springboot文档...
  6. Nutanix 以现代化 IT 基础架构推动医共体建设
  7. 关于View的Animation无法停止问题
  8. 第4次作业类测试代码 021
  9. boostrap --- 弹窗modal的使用
  10. EPLAN 2.7 WIN 10 X64安装说明
  11. 五、文章详情页制作及跳转功能实现《iVX低代码/无代码个人博客制作》
  12. 我的世界java手机_我的世界Java安卓版下载_我的世界Java手机版官方下载_牛游戏网...
  13. python鼠标拖拽功能_python 基于selenium实现鼠标拖拽功能
  14. 为什么使用SSH框架?SSH和SSM框架如何选择
  15. android app 嵌入广告,流氓来了!如何拯救手机中嵌入广告的应用
  16. 局域网即时通讯软件_企业即时通讯软件需要符合哪些要求?
  17. python新打包工具,最好用的版本之一,不接受反驳
  18. Mybatis简介及其核心概念
  19. Glib学习(17) Key-value文件解析器 Key-value file parser
  20. java求sobel算子代码_sobel算子原理及opencv源码实现

热门文章

  1. linux c 多态原理,看了所谓的面向对象中靠继承多态实现的所谓重用 哥笑了
  2. css 实现app图标样式_界面按钮样式丑?不可能!16款css实现炫酷按钮
  3. vscode css提示_锋利码农武器之vscode
  4. java线程栅栏_Java 多线程基础 - CyclicBarrier
  5. 请求模式解决共享资源冲突
  6. vivox21升级鸿蒙,vivo X21i相机规格再升级,加持AI成又一拍照神器
  7. ubuntu android2.3 emulator,Android Studio 2.3 Ubuntu 16.10 emulator do not start
  8. c 语言程序设计阚道宏,C语言用宏实现静态多态
  9. 三层交换机如何封装trunk_锐捷交换机常用配置命令汇总
  10. SharedPreferences记住用户密码 态判断应用是否首次启动等