Java關于MyBatis緩存詳解
什么是 MyBatis 緩存
使⽤緩存可以減少 Java 應⽤與數(shù)據(jù)庫的交互次數(shù),從而提升程序的運行效率。⽐如查詢出 id = 1 的對象,第⼀次查詢出之后會自動將該對象保存到緩存中,當下⼀次查詢時,直接從緩存中取出對象即可, 無需再次訪問數(shù)據(jù)庫。
MyBatis 緩存分類
1.⼀級緩存:SqlSession級別,默認開啟,并且不能關閉.(默認開啟)
操作數(shù)據(jù)庫時需要創(chuàng)建 SqlSession 對象,在對象中有⼀個 HashMap ⽤于存儲緩存數(shù)據(jù),不同的 SqlSession 之間緩存數(shù)據(jù)區(qū)域是互不影響的。 ⼀級緩存的作用域是 SqlSession 范圍的,當在同⼀個 SqlSession 中執(zhí)⾏兩次相同的 SQL 語句事,第⼀ 次執(zhí)行完畢會將結(jié)果保存到緩存中,第⼆次查詢時直接從緩存中獲取。 需要注意的是,如果 SqlSession 執(zhí)行了 DML 操作(insert、update、delete),MyBatis 必須將緩存清空以保證數(shù)據(jù)的準確性。
2.二級緩存:Mapper 級別,默認關閉,可以開啟
使⽤⼆級緩存時,多個 SqlSession 使⽤同⼀個 Mapper 的 SQL 語句操作數(shù)據(jù)庫,得到的數(shù)據(jù)會存在⼆ 級緩存區(qū),同樣是使⽤ HashMap 進⾏數(shù)據(jù)存儲,相⽐較于⼀級緩存,⼆級緩存的范圍更⼤,多個 SqlSession 可以共⽤⼆級緩存,⼆級緩存是跨 SqlSession 的。 ⼆級緩存是多個 SqlSession 共享的,其作⽤域是 Mapper 的同⼀個 namespace,不同的 SqlSession 兩次執(zhí)⾏相同的 namespace 下的 SQL 語句,參數(shù)也相等,則第⼀次執(zhí)⾏成功之后會將數(shù)據(jù)保存到⼆級 緩存中,第⼆次可直接從⼆級緩存中取出數(shù)據(jù)。
二級緩存如何使用
1.MyBatis 自帶的二級緩存
1.1config.xml 配置開啟⼆級緩存
settings> <!-- 打印SQL--> <setting name="logImpl" value="STDOUT_LOGGING" /> <!-- 開啟延遲加載 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 開啟⼆級緩存 --> <setting name="cacheEnabled" value="true"/> </settings>
1.2Mapper.xml 中配置⼆級緩存
<cache></cache>
1.3實體類實現(xiàn)序列化接口
@Data @AllArgsConstructor @NoArgsConstructor public class Account implements Serializable { private long id; private String username; private String password; private int age; }
2.ehcache 二級緩存(第三方)
2.1pom.xml 添加相關依賴
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.4.3</version> </dependency>
2.2添加 ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <diskStore/> <defaultCache maxElementsInMemory="1000" maxElementsOnDisk="10000000" eternal="false" overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> </defaultCache> </ehcache>
2.3config.xml 配置開啟⼆級緩存
<settings> <!-- 打印SQL--> <setting name="logImpl" value="STDOUT_LOGGING" /> <!-- 開啟延遲加載 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 開啟⼆級緩存 --> <setting name="cacheEnabled" value="true"/> </settings>
2.4 Mapper.xml 中配置⼆級緩存
<cache type="org.mybatis.caches.ehcache.EhcacheCache"> <!-- 緩存創(chuàng)建之后,最后⼀次訪問緩存的時間⾄緩存失效的時間間隔 --> <property name="timeToIdleSeconds" value="3600"/> <!-- 緩存⾃創(chuàng)建時間起⾄失效的時間間隔 --> <property name="timeToLiveSeconds" value="3600"/> <!-- 緩存回收策略,LRU表示移除近期使⽤最少的對象 --> <property name="memoryStoreEvictionPolicy" value="LRU"/> </cache>
到此這篇關于Java關于MyBatis緩存詳解的文章就介紹到這了,更多相關Java MyBatis 緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
ElasticSearch之索引模板滾動索引實現(xiàn)詳解
這篇文章主要為大家介紹了ElasticSearch之索引模板滾動索引實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04關于Socket的解析以及雙方即時通訊的java實現(xiàn)方法
本篇文章主要介紹了關于Socket的解析以及雙方通訊的java實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03