日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

关于mybatis里面的Executor--转载

發布時間:2025/4/5 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于mybatis里面的Executor--转载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

使用mybatis查尋數據,跟蹤其執行流程

最開始執行的語句

?

[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來執行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的動態代理,詳細可以見java api,有詳細的說明

    ?

    SqlSessionInterceptor實現了InvocationHandler,在invoke方法里面的開始有這樣代碼,那里是真正的sqlsession

    ?

    [java]?view plaincopyprint?
  • final?SqlSession?sqlSession?=?getSqlSession(??
  • ?????????SqlSessionTemplate.this.sqlSessionFactory,??
  • ?????????SqlSessionTemplate.this.executorType,??
  • ?????????SqlSessionTemplate.this.exceptionTranslator);??
  • 跟蹤geteSqlSession可以找到他的創建來源,見

    ?

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

  • 繼續跟蹤可以找到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);?//?may?have?fetched?a?connection?so?lets?call?close()??
  • ????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,在創建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先從緩存中提取數據,數據緩存中沒有數據時才從數據庫里面提取數據。

    轉載于:https://www.cnblogs.com/davidwang456/p/4693090.html

    總結

    以上是生活随笔為你收集整理的关于mybatis里面的Executor--转载的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。