生活随笔
收集整理的這篇文章主要介紹了
关于mybatis里面的Executor--转载
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
原文地址:http://blog.csdn.net/w_intercool/article/details/7893344
使用mybatis查尋數(shù)據(jù),跟蹤其執(zhí)行流程
最開始執(zhí)行的語句
?
[java]?view plaincopyprint?
this.getSqlSession().selectList("QUERY-QUESTION",?data,?rowBounds);?? 這里需要找到sqlsession是從哪里來的
getSqlSession是SqlSessionDaoSupport類里面的方法,該類通過spring的自動注入可以把sqlSessionTemplate注入進來,當然這里的sqlSessionTemplate是需要spring配置的
?
[java]?view plaincopyprint?
@Autowired(required?=?false)??public?final?void?setSqlSessionTemplate(SqlSessionTemplate?sqlSessionTemplate)?{????this.sqlSession?=?sqlSessionTemplate;????this.externalSqlSession?=?true;??}?? ?
[html]?view plaincopyprint?
<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來執(zhí)行selectList方法的,也就是通過代理方式來的
?
[java]?view plaincopyprint?
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的動態(tài)代理,詳細可以見java api,有詳細的說明
?
SqlSessionInterceptor實現(xiàn)了InvocationHandler,在invoke方法里面的開始有這樣代碼,那里是真正的sqlsession
?
[java]?view plaincopyprint?
final?SqlSession?sqlSession?=?getSqlSession(???????????SqlSessionTemplate.this.sqlSessionFactory,???????????SqlSessionTemplate.this.executorType,???????????SqlSessionTemplate.this.exceptionTranslator);?? 跟蹤geteSqlSession可以找到他的創(chuàng)建來源,見
?
[java]?view plaincopyprint?
SqlSession?session?=?sessionFactory.openSession(executorType);??
繼續(xù)跟蹤可以找到DefaultSqlSessionFactory里面的該方法
?
?
[java]?view plaincopyprint?
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);?????throw?ExceptionFactory.wrapException("Error?opening?session.??Cause:?"?+?e,?e);????}?finally?{??????ErrorContext.instance().reset();????}??}??
通過
[java]?view plaincopyprint?
final?Executor?executor?=?configuration.newExecutor(tx,?execType,?autoCommit);?? ?
你就知道executor是怎么回來的了
mybatis默認使用了cache,在創(chuàng)建exector時,外面就包了一層CacheExecutor,詳細見
?
[java]?view plaincopyprint?
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先從緩存中提取數(shù)據(jù),數(shù)據(jù)緩存中沒有數(shù)據(jù)時才從數(shù)據(jù)庫里面提取數(shù)據(jù)。
轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/4693090.html
總結(jié)
以上是生活随笔為你收集整理的关于mybatis里面的Executor--转载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。