MyBatis高級篇之整合ehcache緩存框架
一、前言
MyBatis為我們提供了Cache接口,也提供了一些實現類,進入Cache接口源碼,可以看到緩存對於MyBatis來說就是一個Map,比較簡陋。但是大家都知道MyBatis是一個專注於持久層框架,與數據庫打交道MyBatis是很專業的,但是對於緩存它就略顯不足,即便如此MyBatis卻提供了Cache接口的標准規范,誰做緩存專業,誰就來實現它,提供了很好的擴展性。本節我們將介紹了一下MyBatis與Ehcache框架的整合。
二、准備
Eclipse版本:Luna Service Release 1 (4.4.1)
MyBatis版本:org.mybatis.mybatis-3.2.8
JDK版本:1.7.0_79
Ehcache版本:ehcache-core-2.6.11.jar
mybatis-ehcache整合版本:mybatis-ehcache-1.1.0.jar
三、案例
♠參照<<MyBatis基礎篇之簡單入門>>搭建Maven工程MyBatisIntegrateEhcache
♠搭建完工程后,需要往工程里面引入ehcache相關的jar包
引入核心包
<!-- ehcache核心jar包 --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.11</version> </dependency>
引入mybatis-ehcache整合包,如果不知道怎么引入,可以參考官方文檔,如下圖:
官方地址:http://www.mybatis.org/ehcache-cache/ 有興趣可以看一下。
♠完整的Pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.queen.mybatis</groupId> <artifactId>MyBatisIntegrateEhcache</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- 單元測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.4</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.4</version> </dependency> <!-- ehcache核心jar包 --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.11</version> </dependency> <!-- MyBatis與ehcache整合jar包 --> <dependency> <groupId>org.mybatis.caches</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.1.0</version> </dependency> </dependencies> </project>
♠下面添加配置,都是按照官方文檔來配置的
修改EmpMapper.xml文件,添加如下配置
引入ehcache.xml文件,完整配置說明如下:
<?xml version="1.0" encoding="UTF-8" ?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <!-- java.io.tmpdir - Default temp file path 默認的 temp 文件目錄 maxElementsInMemory:內存中最大緩存對象數. maxElementsOnDisk:磁盤中最大緩存對象數,若是0表示無窮大. eternal:Element是否永久有效,一但設置了,timeout將不起作用. overflowToDisk:配置此屬性,當內存中Element數量達到maxElementsInMemory時,Ehcache將會Element寫到磁盤中 timeToIdleSeconds:設置Element在失效前的允許閑置時間。僅當element不是永久有效時使用,可選屬性,默認值是0, 也就是可閑置時間無窮大 timeToLiveSeconds:設置Element在失效前允許存活時間。最大時間介於創建時間和失效時間之間。僅當element不是永久有效時使用, 默認是0.也就是element存活時間無窮大. diskPersistent:是否緩存虛擬機重啟期數據。(這個虛擬機是指什么虛擬機一直沒看明白是什么,有高人還希望能指點一二) diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒。 diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每個Cache都應該有自己的一個緩沖區. --> <diskStore path="E:\20170819\ehcache" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> </ehcache>
ehcache.xml放置路徑
如上,就是我們整合ehcache所需要的步驟,下面我們來簡單的測試一下
@Test public void testEhCache() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmpMapper mapper = openSession.getMapper(EmpMapper.class); Emp emp01 = mapper.findEmpById(1); System.out.println(emp01.toString()); Emp emp02 = mapper.findEmpById(1); System.out.println(emp02.toString()); System.out.println(emp01 == emp02); } finally { openSession.close(); } }
測試結果如下
2017-08-19 11:27:18,090 [main] [com.queen.mybatis.mapper.EmpMapper.findEmpById]-[DEBUG] ==> Preparing: select id,emp_name empName,emp_email empEmail, dept_id deptId from t_emp where id = ?
2017-08-19 11:27:18,144 [main] [com.queen.mybatis.mapper.EmpMapper.findEmpById]-[DEBUG] ==> Parameters: 1(Integer)
2017-08-19 11:27:18,175 [main] [com.queen.mybatis.mapper.EmpMapper.findEmpById]-[DEBUG] <== Total: 1 Emp [id=1, empName=queen3aasd21, empEmail=queen123@sina.com, deptId=1] 2017-08-19 11:27:18,177 [main] [com.queen.mybatis.mapper.EmpMapper]-[DEBUG] Cache Hit Ratio [com.queen.mybatis.mapper.EmpMapper]: 0.0 Emp [id=1, empName=queen3aasd21, empEmail=queen123@sina.com, deptId=1] true 2017-08-19 11:27:18,178 [main] [net.sf.ehcache.store.disk.Segment]-[DEBUG] put added 0 on heap
控制台打印了一段SQL,第二次查詢直接從緩存中獲取。
從上面的打印結果,我們好像無法判斷是否真的用到了ehcache緩存,但可以看一下緩存路徑下是否有數據就知道了
找到該路徑,可以確信使用了ehcache緩存
四、總結
MyBatis與Ehcache整合步驟總結如下:
- 引入第三方緩存包
- 導入與第三方緩存整合的適配包
- 在mapper.xml映射文件中引入<cache type=”org.mybatis.caches.ehcache.EhcacheCache”/>即可
- 引入ehcache.xml文件,設置相關屬性配置
至此,我們關於MyBatis高級篇之整合ehcache緩存框架介紹完畢。
博客地址:http://www.marsitman.com/mybatis/mybatis-ehcache.html
版權聲明:本文為博主原創文章,允許轉載,但轉載必須標明出處。