亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

mybatis一級緩存的問題

spring整合mybatis后,sqlSeesion被sprinhIoc容器管理起來。假如第一次查詢數據庫用到sqlSeesion1,第二次我還是進行相同的查詢操作,Ioc容器會使用上次給我的sqlSession1(也就是使用緩存),還是會重新創建一個新的sqlSession。

正在回答

2 回答

關于緩存問題:

http://www.cnblogs.com/zemliu/archive/2013/08/05/3239014.html

執行了2次sql查詢,看似我們使用了同一個sqlSession,但是實際上因為我們的dao繼承了SqlSessionDaoSupport,而SqlSessionDaoSupport內部sqlSession的實現是使用用動態代理實現的,這個動態代理sqlSessionProxy使用一個模板方法封裝了select()等操作,每一次select()查詢都會自動先執行openSession(),執行完close()以后調用close()方法,相當于生成了一個新的session實例,所以我們無需手動的去關閉這個session()(關于這一點見下面mybatis的官方文檔),當然也無法使用mybatis的一級緩存,也就是說mybatis的一級緩存在spring中是沒有作用的.

官方文檔摘要

MyBatis?SqlSession?provides you with specific methods to handle transactions programmatically. But when using MyBatis-Spring your beans will be injected with a Spring managed?SqlSession?or a Spring managed mapper. That means that Spring will?always?handle your transactions.

You cannot call?SqlSession.commit(),?SqlSession.rollback()?or?SqlSession.close()?over a Spring managed?SqlSession. If you try to do so, a?UnsupportedOperationException?exception will be thrown. Note these methods are not exposed in injected mapper classes.


0 回復 有任何疑惑可以回復我~

會重新創建一個新的sqlSession,因為每次的數據庫操作,一般都是以下四步:

1,獲取SqlSession對象

2,調用數據庫操作的方法

3,提交事務

4,關閉SqlSession

0 回復 有任何疑惑可以回復我~
#1

朱小懸 提問者

查詢操作不需要提交事務吧?
2017-05-09 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

mybatis一級緩存的問題

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號