关于mybatis里面的Executor–转载

原文地址:http://blog.csdn.net/w_intercool/article/details/7893344使用mybatis查寻数据,跟踪其执行流程最开始执行的语句[java]view plaincopyprint?this.getSqlSession().selectList(QUERY-QUESTION,data,rowBounds);这里需要找到sqlsession是从哪里来的

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

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

最开始执行的语句

<div class=”dp-highlighter bg_java”>
<div class=”bar”>
<div class=”tools”>
[java]<a class=”ViewSource” title=”view plain” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;view plain<a class=”CopyToClipboard” title=”copy” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;copy<a class=”PrintSource” title=”print” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;print<a class=”About” title=”?” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;?

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

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

<div class=”dp-highlighter bg_java”>
<div class=”bar”>
<div class=”tools”>
[java]<a class=”ViewSource” title=”view plain” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;view plain<a class=”CopyToClipboard” title=”copy” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;copy<a class=”PrintSource” title=”print” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;print<a class=”About” title=”?” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;?

<div class=”dp-highlighter bg_html”>
<div class=”bar”>
<div class=”tools”>
[html]<a class=”ViewSource” title=”view plain” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;view plain<a class=”CopyToClipboard” title=”copy” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;copy<a class=”PrintSource” title=”print” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;print<a class=”About” title=”?” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;?

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

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

<div class=”dp-highlighter bg_java”>
<div class=”bar”>
<div class=”tools”>
[java]<a class=”ViewSource” title=”view plain” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;view plain<a class=”CopyToClipboard” title=”copy” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;copy<a class=”PrintSource” title=”print” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;print<a class=”About” title=”?” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;?

  1. PersistenceExceptionTranslatorexceptionTranslator){
  2. notNull(sqlSessionFactory,
  3. }

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

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

<div class=”dp-highlighter bg_java”>
<div class=”bar”>
<div class=”tools”>
[java]<a class=”ViewSource” title=”view plain” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;view plain<a class=”CopyToClipboard” title=”copy” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;copy<a class=”PrintSource” title=”print” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;print<a class=”About” title=”?” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;?

  1. SqlSessionTemplate.
  2. SqlSessionTemplate.

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

<div class=”dp-highlighter bg_java”>
<div class=”bar”>
<div class=”tools”>
[java]<a class=”ViewSource” title=”view plain” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;view plain<a class=”CopyToClipboard” title=”copy” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;copy<a class=”PrintSource” title=”print” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;print<a class=”About” title=”?” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;?

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

<div class=”dp-highlighter bg_java”>
<div class=”bar”>
<div class=”tools”>
[java]<a class=”ViewSource” title=”view plain” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;view plain<a class=”CopyToClipboard” title=”copy” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;copy<a class=”PrintSource” title=”print” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;print<a class=”About” title=”?” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;?

  1. Transactiontx=
  2. tx=transactionFactory.newTransaction(environment.getDataSource(),level,autoCommit);
  3. closeTransaction(tx);
  4. }
  5. }

通过

[java]

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

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

<div class=”dp-highlighter bg_java”>
<div class=”bar”>
<div class=”tools”>
[java]<a class=”ViewSource” title=”view plain” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;view plain<a class=”CopyToClipboard” title=”copy” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;copy<a class=”PrintSource” title=”print” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;print<a class=”About” title=”?” href=”http://blog.csdn.net/w_intercool/article/details/7893344″&gt;?

  1. executorType=executorType==
  2. Executorexecutor;
  3. executor=
  4. executor=
  5. executor=
  6. }

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

作者: dawei

【声明】:永州站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部