A simple implementation of spring MVC + MyBatis's Ehcache

Source: Internet
Author: User

As a result of these two days the SPRINGMVC and MyBatis built web framework is then ready to be cached data, simply record the

Get ready:

Googlecode's Ehcache

This can be downloaded in the https://code.google.com/p/ehcache-spring-annotations/, downloaded after the use of the jar package

    

Note Pack Ehcache-spring-annotations-1.2.0.jar in the downloaded Compression pack  

Since some of the other packages are already in spring MVC, you just need to add these in.

Configure Ehcache.xml

The new configuration ehcache.xml in the SRC directory is as follows:

<?XML version= "1.0" encoding= "UTF-8"?><!--/** * * Cache configuration * @author YQ * @date 2014.9.10 * */ - <EhcacheXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:nonamespaceschemalocation= "Http://ehcache.org/ehcache.xsd"UpdateCheck= "false">   <DiskstorePath= "Java.io.tmpdir" />   <DefaultcacheEternal= "false"maxelementsinmemory= "+"Overflowtodisk= "false"diskpersistent= "false"Timetoidleseconds= "0"Timetoliveseconds= "All"Memorystoreevictionpolicy= "LRU" />     <Cachename= "Customercache"Eternal= "false"maxelementsinmemory= "+"Overflowtodisk= "false"diskpersistent= "false"Timetoidleseconds= "0"Timetoliveseconds= "+"Memorystoreevictionpolicy= "LRU" />    </Ehcache>  

You can have multiple caches in this configuration to easily manage the cache in our program.

Configure Spring.xml
1  <Ehcache:annotation-drivenCache-manager= "Ehcachemanager" />   2  <BeanID= "Ehcachemanager"class= "Org.springframework.cache.ehcache.EhCacheManagerFactoryBean">    3        < Propertyname= "Configlocation"value= "Classpath:ehcache.xml" />    4    </Bean>  

The next step is to try out the cache on our code Service tier:

@Cacheable (CacheName ="Customercache")     PublicCustomer Getcustomerbycid (intCID) {System. out. Print ("----------------------------------------------------------"); returnCD.    Getcustomerbycid (CID); }    //Modify Customer@TriggersRemove (CacheName ={"Customercache"},removeall=true)     Public intUpdateCustomer (Customer cus) {returnCD.    UpdateCustomer (cus); }

Note that the import of the package is the two

Import com.googlecode.ehcache.annotations.Cacheable; import Com.googlecode.ehcache.annotations.TriggersRemove;

The above is already built.

When we call the Getcustomerbycid method in the service two times, the console has only one SQL print to indicate success.

Debug-dispatcherservlet with name ' myproject-dispatcher ' processing GET request for [/jxc/customer/getcustomerbycid] Debug-looking up handler method for Path/customer/getcustomerbyciddebug-returning handler method [public java.lang.St Ring Com.ly.jxc.controller.CustomerController.GetCustomerByCid (Java.lang.string,org.springframework.ui.modelmap )]debug-returning cached instance of singleton Bean ' Customercontroller ' debug-last-modified value for [/jxc/customer/g ETCUSTOMERBYCID] is: -1debug-generated key ' 369036164508828 ' for Invocation:ReflectiveMethodInvocation:public com.ly. Jxc.entity.Customer com.ly.jxc.service.CustomerService.GetCustomerByCid (int); Target is of class [Com.ly.jxc.service.CustomerService]---------------------------------------------------------- Debug-creating a new sqlsessiondebug-sqlsession [[[email protected]] is not registered for synchronization Becaus E synchronization is not activedebug-fetching JDBC Connection from Datasourcedebug-jdbc Connection [Jdbc:mysql://192.168.1.6:3306/lyjxc?useunicode=true&characterencoding=utf-8, [email  Protected], Mysql-ab JDBC Driver] won't is managed by Springdebug-ooo Using Connection [jdbc:mysql://192.168.1.6:3306/lyjxc?useunicode=true &characterencoding=utf-8, [email protected], Mysql-ab JDBC driver]debug-==> preparing:select * from Custom Er where cid=? DEBUG-==> parameters:18 (Integer) debug-closing non transactional sqlsession  [[email protected]] Debug-returning JDBC Connection to Datasourcedebug-rendering view [com.ly.jxc.util.FreeMarkerPath:name ' SETTINGS/ADDC Ustomer '; URL [settings/addcustomer.html]] in Dispatcherservlet with Name ' Myproject-dispatcher ' debug-added model object ' Custome R ' of type [Com.ly.jxc.entity.Customer] to request in view with Name ' Settings/addcustomer ' debug-added model object ' SPR Ingmacrorequestcontext ' of type [Org.springframework.web.servlet.support.RequestContext] to request in view with name ' Settings/addcustomer ' debug-added model object ' webRoot ' of type [java.lang.String] to request in view with name ' Setting S/addcustomer ' debug-added model object ' Org.springframework.validation.BindingResult.Customer ' of type [ Org.springframework.validation.BeanPropertyBindingResult] to request in view with Name ' Settings/addcustomer ' DEBUG- Rendering freemarker template [settings/addcustomer.html] in Freemarkerview ' Settings/addcustomer ' DEBUG-"settings/ Addcustomer.html "[" ZH_cn ", gbk,parsed] using cached since E:\vss\Project\Java\ly_Smbf\WebRoot\Settings\addCustomer.html didn ' t change. Debug-successfully completed requestdebug-returning Cached instance of singleton beans ' sqlsessionfactory ' Debug-dispa Tcherservlet with name ' myproject-dispatcher ' processing GET request for [/jxc/customer/getcustomerbycid]debug-looking Up handler method for Path/customer/getcustomerbyciddebug-returning handler method [public java.lang.String com.ly.jxc. Controller. Customercontroller.getcustomerbycid (java.lang.string,org.springframework.ui.modelmap)]debug-returning Cached Instance of singleton Bean ' Customercontroller ' debug-last-modified value for [/jxc/customer/getcustomerbycid] is: 1debug-generated key ' 369036164508828 ' for invocation:reflectivemethodinvocation: Public Com.ly.jxc.entity.Customer com.ly.jxc.service.CustomerService.GetCustomerByCid (int); Target is of class [Com.ly.jxc.service.CustomerService] debug-rendering view [com.ly.jxc.util.FreeMarkerPath:name ' Settings/addcustomer '; URL [settings/addcustomer.html]] in Dispatcherservlet with Name ' Myproject-dispatcher ' debug-added model object ' Custome R ' of type [Com.ly.jxc.entity.Customer] to request in view with Name ' Settings/addcustomer ' debug-added model object ' SPR Ingmacrorequestcontext ' of type [Org.springframework.web.servlet.support.RequestContext] to request in view with name ' Settings/addcustomer ' debug-added model object ' webRoot ' of type [java.lang.String] to request in view with name ' Setting S/addcustomer ' debug-added model object ' Org.springframework.validation.BindingResult.Customer ' of type [ Org.springframework.validation.BeanPropertyBindingResult] to request in view with Name ' Settings/addcustomer ' DEBUG- Rendering freemarker template [settings/addcustomer.html] in Freemarkerview ' Settings/addcustomer ' DEBUG-"settings/ Addcustomer.html "[" ZH_CN ", gbk,parsed] cached copy not yet stale; Using cached. Debug-succesSfully completed requestdebug-returning Cached instance of singleton beans ' sqlsessionfactory ' 

  

The above is a simple record.

A simple implementation of spring MVC + MyBatis's Ehcache

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.