什么是 MyBatis 緩存
使?緩存可以減少 Java 應(yīng)?與數(shù)據(jù)庫的交互次數(shù),從而提升程序的運(yùn)行效率。?如查詢出 id = 1 的對(duì)象,第?次查詢出之后會(huì)自動(dòng)將該對(duì)象保存到緩存中,當(dāng)下?次查詢時(shí),直接從緩存中取出對(duì)象即可, 無需再次訪問數(shù)據(jù)庫。
MyBatis 緩存分類
1、?級(jí)緩存:SqlSession 級(jí)別,默認(rèn)開啟,并且不能關(guān)閉。(默認(rèn)開啟)
操作數(shù)據(jù)庫時(shí)需要?jiǎng)?chuàng)建 SqlSession 對(duì)象,在對(duì)象中有?個(gè) HashMap ?于存儲(chǔ)緩存數(shù)據(jù),不同的 SqlSession 之間緩存數(shù)據(jù)區(qū)域是互不影響的。 ?級(jí)緩存的作用域是 SqlSession 范圍的,當(dāng)在同?個(gè) SqlSession 中執(zhí)?兩次相同的 SQL 語句事,第? 次執(zhí)行完畢會(huì)將結(jié)果保存到緩存中,第?次查詢時(shí)直接從緩存中獲取。 需要注意的是,如果 SqlSession 執(zhí)行了 DML 操作(insert、update、delete),MyBatis 必須將緩存清空以保證數(shù)據(jù)的準(zhǔn)確性。
2、二級(jí)緩存:Mapper 級(jí)別,默認(rèn)關(guān)閉,可以開啟。
使??級(jí)緩存時(shí),多個(gè) SqlSession 使?同?個(gè) Mapper 的 SQL 語句操作數(shù)據(jù)庫,得到的數(shù)據(jù)會(huì)存在? 級(jí)緩存區(qū),同樣是使? HashMap 進(jìn)?數(shù)據(jù)存儲(chǔ),相?較于?級(jí)緩存,?級(jí)緩存的范圍更?,多個(gè) SqlSession 可以共??級(jí)緩存,?級(jí)緩存是跨 SqlSession 的。 ?級(jí)緩存是多個(gè) SqlSession 共享的,其作?域是 Mapper 的同?個(gè) namespace,不同的 SqlSession 兩次執(zhí)?相同的 namespace 下的 SQL 語句,參數(shù)也相等,則第?次執(zhí)?成功之后會(huì)將數(shù)據(jù)保存到?級(jí) 緩存中,第?次可直接從?級(jí)緩存中取出數(shù)據(jù)。
二級(jí)緩存如何使用
1、MyBatis 自帶的二級(jí)緩存
1.1config.xml 配置開啟?級(jí)緩存
1
2
3
4
5
6
7
8
|
settings> <!-- 打印SQL--> < setting name = "logImpl" value = "STDOUT_LOGGING" /> <!-- 開啟延遲加載 --> < setting name = "lazyLoadingEnabled" value = "true" /> <!-- 開啟?級(jí)緩存 --> < setting name = "cacheEnabled" value = "true" /> </ settings > |
1.2Mapper.xml 中配置?級(jí)緩存
1
|
< cache ></ cache > |
1.3實(shí)體類實(shí)現(xiàn)序列化接口
1
2
3
4
5
6
7
8
9
|
@Data @AllArgsConstructor @NoArgsConstructor public class Account implements Serializable { private long id; private String username; private String password; private int age; } |
2、ehcache 二級(jí)緩存(第三方)
2.1pom.xml 添加相關(guān)依賴
1
2
3
4
5
6
7
8
9
10
|
< 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< 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 配置開啟?級(jí)緩存
1
2
3
4
5
6
7
8
|
< settings > <!-- 打印SQL--> < setting name = "logImpl" value = "STDOUT_LOGGING" /> <!-- 開啟延遲加載 --> < setting name = "lazyLoadingEnabled" value = "true" /> <!-- 開啟?級(jí)緩存 --> < setting name = "cacheEnabled" value = "true" /> </ settings > |
2.4 Mapper.xml 中配置?級(jí)緩存
1
2
3
4
5
6
7
8
|
< cache type = "org.mybatis.caches.ehcache.EhcacheCache" > <!-- 緩存創(chuàng)建之后,最后?次訪問緩存的時(shí)間?緩存失效的時(shí)間間隔 --> < property name = "timeToIdleSeconds" value = "3600" /> <!-- 緩存?創(chuàng)建時(shí)間起?失效的時(shí)間間隔 --> < property name = "timeToLiveSeconds" value = "3600" /> <!-- 緩存回收策略,LRU表示移除近期使?最少的對(duì)象 --> < property name = "memoryStoreEvictionPolicy" value = "LRU" /> </ cache > |
以上就是深層剖析java開發(fā)中MyBayis緩存的詳細(xì)內(nèi)容,更多關(guān)于Mybatis緩存剖析的資料請(qǐng)關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://blog.csdn.net/DrLai/article/details/119786974