原文地址: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″>view plain<a class=”CopyToClipboard” title=”copy” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>copy<a class=”PrintSource” title=”print” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>print<a class=”About” title=”?” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>?
这里需要找到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″>view plain<a class=”CopyToClipboard” title=”copy” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>copy<a class=”PrintSource” title=”print” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>print<a class=”About” title=”?” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>?
<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″>view plain<a class=”CopyToClipboard” title=”copy” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>copy<a class=”PrintSource” title=”print” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>print<a class=”About” title=”?” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>?
@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″>view plain<a class=”CopyToClipboard” title=”copy” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>copy<a class=”PrintSource” title=”print” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>print<a class=”About” title=”?” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>?
- PersistenceExceptionTranslatorexceptionTranslator){
- notNull(sqlSessionFactory,
- }
这里用到了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″>view plain<a class=”CopyToClipboard” title=”copy” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>copy<a class=”PrintSource” title=”print” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>print<a class=”About” title=”?” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>?
- SqlSessionTemplate.
- 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″>view plain<a class=”CopyToClipboard” title=”copy” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>copy<a class=”PrintSource” title=”print” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>print<a class=”About” title=”?” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>?
继续跟踪可以找到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″>view plain<a class=”CopyToClipboard” title=”copy” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>copy<a class=”PrintSource” title=”print” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>print<a class=”About” title=”?” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>?
- Transactiontx=
- tx=transactionFactory.newTransaction(environment.getDataSource(),level,autoCommit);
- closeTransaction(tx);
- }
- }
通过
[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″>view plain<a class=”CopyToClipboard” title=”copy” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>copy<a class=”PrintSource” title=”print” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>print<a class=”About” title=”?” href=”http://blog.csdn.net/w_intercool/article/details/7893344″>?
- executorType=executorType==
- Executorexecutor;
- executor=
- executor=
- executor=
- }
CachingExecutor可以使mybatis先从缓存中提取数据,数据缓存中没有数据时才从数据库里面提取数据。