In order to improve the performance of mybatis, sometimes we need to join the cache support, the current use of more cache than Ehcache cache, Ehcache performance, and a variety of applications have provided a solution, where we mainly do query caching, improve the efficiency of the query.
MyBatis on the official website to download the integrated Ehcache document to see, said too simple, for beginners difficult to understand, and the inside said is not very clear, after a toss, finally will Ehcache joined.
The official web site provides a Mybatis-ehcache.jar package for consolidating the Ehcache cache, and the documentation also explains the need for a Ehcache-core.jar package, in addition to the two packages which are also required, which the authorities do not specify, The following are all the Ehcache related packages that need to be added:
1.Ehcache-core-2.4.4.jar
2.mybatis-ehcache-1.0.0.jar
3.slf4j-api-1.6.1.jar
4.slf4j-log4j12-1.6.2.jar
In addition to the MyBatis jar package, Log4j,mysql driver, these people should know.
After adding the above package to the project, create a new file name, which must be ehcache.xml and placed below the classpath with the following contents
XML code
- <? XML version= "1.0" encoding="UTF-8"?>
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:nonamespaceschemalocation=". /bin/ehcache.xsd ">
- <!--
- Unique identification of the Name:cache
- Maxelementsinmemory: Maximum number of cached objects in memory
- Maxelementsondisk: Maximum number of cache objects on disk, if 0 means infinity
- Eternal:element is permanently valid, one but set, timeout will not work
- Overflowtodisk: Configuring this property, Ehcache will write element to disk when the number of element in memory reaches Maxelementsinmemory
- Timetoidleseconds: Sets the allowable idle time before the element expires. The optional attribute is used only when element is not permanently valid, and the default value is 0, which is idle time infinity
- Timetoliveseconds: Sets the element to allow survival time before failure. The maximum time is between the creation time and the expiration time. Used only when element is not permanently valid, the default is 0. That is, element survival time Infinity
- Diskpersistent: Whether to cache virtual machine Restart period data
- Diskexpirythreadintervalseconds: Disk failed thread run time interval, default is 120 seconds
- DISKSPOOLBUFFERSIZEMB: This parameter sets the buffer size of the Diskstore (disk cache). The default is 30MB. Each cache should have its own buffer
- Memorystoreevictionpolicy: When the maxelementsinmemory limit is reached, Ehcache will clean up the memory according to the specified policy. The default policy is LRU (least recently used). You can set it to FIFO (first in, out) or LFU (less used)
- -->
- <defaultcache overflowtodisk="true" eternal="false"/>
- <diskstore path="D:/cache" />
- <!--
- <cache name="Zzugxy" overflowtodisk="true" eternal="false"
- timetoidleseconds= "timetoliveseconds=" " maxelementsinmemory= "
- maxelementsondisk= " diskpersistent=" true " diskexpirythreadintervalseconds="
- " diskspoolbuffersizemb="memorystoreevictionpolicy= " LRU" />
- -->
- </ehcache>
<?xml version= "1.0" encoding= "UTF-8"? ><ehcache xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: Nonamespaceschemalocation= ". /bin/ehcache.xsd "><!--name:cache unique identity maxelementsinmemory: Maximum number of cache objects in memory Maxelementsondisk: Maximum number of cached objects on disk, If 0 indicates whether Infinity Eternal:element is permanently valid, but set, timeout will not work Overflowtodisk: Configure this property when the number of element in memory reaches Maxelementsinmemory The Ehcache element is written to disk Timetoidleseconds: sets the allowable idle time for the element before it expires. The optional attribute is used only when element is not permanently valid, and the default value is 0, which is infinite idle time timetoliveseconds: Sets the element to allow time to survive before it expires. The maximum time is between the creation time and the expiration time. Used only when element is not permanently valid, the default is 0. That is, element survival time Infinity Diskpersistent: Whether to cache virtual machine Restart period Data diskexpirythreadintervalseconds: Disk failed thread run time interval, The default is 120 seconds DISKSPOOLBUFFERSIZEMB: This parameter sets the buffer size of the Diskstore (disk cache). The default is 30MB. Each cache should have its own buffer memorystoreevictionpolicy: When the maxelementsinmemory limit is reached, Ehcache will clean up the memory according to the specified policy. The default policy is LRU (least recently used). You can set it to FIFO (first-in, in/out) or LFU (less used)--><defaultcache overflowtodisk= "true" Eternal= "false"/><diskstore path= "D :/cache "/><!--<cache name=" Zzugxy "overflowtodisk=" true "Eternal=" false "Timetoidleseconds= "timetoliveseconds=" "maxelementsinmemory=" "maxelementsondisk=" diskpersistent= "true" diskexpirythreadintervalseconds= "diskspoolbuffersizemb=" memorystoreevictionpolicy= "LRU"/>-->< /ehcache>
This file is Ehcache's configuration file, the above comments have been made clear, here I use the default configuration
At this point Ehcache has been configured, then only need to add the following in the mapper configuration file you want to cache, the result of the query statement will be cached
XML code
- <? XML version= "1.0" encoding="UTF-8" ?>
- <! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="Com.qiuqiu.dao.PersonDao">
- <!--The following two <cache> label two select one, the first one can output the log, the second does not output the log--
- <cache type="Org.mybatis.caches.ehcache.LoggingEhcache"/>
- <!--<cache type= "Org.mybatis.caches.ehcache.EhcacheCache"/> --
- <Select id= "Selectuserbyid" parametertype="int" resulttype=" Org.qiuqiu.vo.Person ">
- SELECT * from person where Id=#{id}
- </Select>
- </mapper>
<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >< Mapper namespace= "Com.qiuqiu.dao.PersonDao" ><!--the following two <cache> label two select one, the first one can output the log, the second does not output a log--><cache Type= "Org.mybatis.caches.ehcache.LoggingEhcache"/><!--<cache type= " Org.mybatis.caches.ehcache.EhcacheCache "/>--><select id=" Selectuserbyid "parametertype=" int "resulttype= "Org.qiuqiu.vo.Person" >select * from person where id=#{id} </select></mapper>
This caches the various results in the mapper. No changes needed in the program
Java enterprise-Class generic rights security framework source SPRINGMVC MyBatis or Hibernate+ehcache Shiro Druid Bootstrap HTML5
"Java Framework source code download"
MyBatis Getting Started Instance (ii)--adding Ehcache cache support