MyBatis Getting Started Instance (ii)--adding Ehcache cache support

Source: Internet
Author: User

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
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:nonamespaceschemalocation=". /bin/ehcache.xsd ">
  4. <!--
  5. Unique identification of the Name:cache
  6. Maxelementsinmemory: Maximum number of cached objects in memory
  7. Maxelementsondisk: Maximum number of cache objects on disk, if 0 means infinity
  8. Eternal:element is permanently valid, one but set, timeout will not work
  9. Overflowtodisk: Configuring this property, Ehcache will write element to disk when the number of element in memory reaches Maxelementsinmemory
  10. 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
  11. 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
  12. Diskpersistent: Whether to cache virtual machine Restart period data
  13. Diskexpirythreadintervalseconds: Disk failed thread run time interval, default is 120 seconds
  14. DISKSPOOLBUFFERSIZEMB: This parameter sets the buffer size of the Diskstore (disk cache). The default is 30MB. Each cache should have its own buffer
  15. 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)
  16. -->
  17. <defaultcache overflowtodisk="true" eternal="false"/>
  18. <diskstore path="D:/cache" />
  19. <!--
  20. <cache name="Zzugxy" overflowtodisk="true" eternal="false"
  21. timetoidleseconds= "timetoliveseconds=" " maxelementsinmemory= "
  22. maxelementsondisk= " diskpersistent=" true " diskexpirythreadintervalseconds="
  23. " diskspoolbuffersizemb="memorystoreevictionpolicy= " LRU" />
  24. -->
  25. </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
  1. <? XML version= "1.0" encoding="UTF-8" ?>
  2. <!  DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="Com.qiuqiu.dao.PersonDao">
  4. <!--The following two <cache> label two select one, the first one can output the log, the second does not output the log--
  5. <cache type="Org.mybatis.caches.ehcache.LoggingEhcache"/>
  6. <!--<cache type= "Org.mybatis.caches.ehcache.EhcacheCache"/> --
  7. <Select id= "Selectuserbyid" parametertype="int" resulttype=" Org.qiuqiu.vo.Person ">
  8. SELECT * from person where Id=#{id}
  9. </Select>
  10. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.