最近在集成springMVC+mybatis +ehcache 整合碰到了很多坑希望对想了解的同学有一定的帮助
来源:互联网 发布:js两数字相加 编辑:程序博客网 时间:2024/05/23 12:15
在这里springMVC+mybatis如何集成我暂时不多说,之后有时间会更新。
集成ehcache只要分为四步:
第一步: 编写ehcache配置文件ehcache.xml当然名字自己可以随便起,只要保证在后面引用配置文件的时候能够书写正确就OK。
<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"monitoring="autodetect" dynamicConfig="true"><diskStore path="java.io.tmpdir"/> <!-- <diskStore path="java.io.tmpdir" /> --><defaultCache maxElementsInMemory="10000" eternal="false"timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"diskPersistent="false" diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LRU" /><cache name="baseCache" maxElementsInMemory="10000" eternal="false"timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"diskPersistent="false" diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LFU" /></ehcache><!-- 1.必须要有的属性: name: cache的名字,用来识别不同的cache,必须惟一。 maxElementsInMemory: 内存管理的缓存元素数量最大限值。 maxElementsOnDisk: 硬盘管理的缓存元素数量最大限值。默认值为0,就是没有限制。 eternal: 设定元素是否持久话。若设为true,则缓存元素不会过期。 overflowToDisk: 设定是否在内存填满的时候把数据转到磁盘上。 2.下面是一些可选属性: timeToIdleSeconds: 设定元素在过期前空闲状态的时间,只对非持久性缓存对象有效。默认值为0,值为0意味着元素可以闲置至无限长时间。 timeToLiveSeconds: 设定元素从创建到过期的时间。其他与timeToIdleSeconds类似。 diskPersistent: 设定在虚拟机重启时是否进行磁盘存储,默认为false.(我的直觉,对于安全小型应用,宜设为true)。 diskExpiryThreadIntervalSeconds: 访问磁盘线程活动时间。 diskSpoolBufferSizeMB: 存入磁盘时的缓冲区大小,默认30MB,每个缓存都有自己的缓冲区。 memoryStoreEvictionPolicy: 元素逐出缓存规则。共有三种,Recently Used (LRU)最近最少使用,为默认。 First In First Out (FIFO),先进先出。Less Frequently Used(specified as LFU)最少使用 -->
第二步:编写ehcache整合spring配置文件 applicationContext-ehcache.xml 名字同上可以自己进行更改
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 开启spring缓存 --> <cache:annotation-driven cache-manager="cacheManager"/> <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml"/> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehcacheManager"/> <property name="transactionAware" value="true"/> </bean></beans>第三步 将配置文件引入mvc的配置文件中 mvc-servlet.xml 因个人项目而定名字只是在我代码中是这样命名的
<import resource="applicationContext-ehcache.xml" />
此时文件都在同一级如果配置文件在项目管理上存在分包分文件夹的情况要特别注意
第四步 使用注解的方式在dao层或者service的实现层中对应的添加 @Cacheable(value="baseCache",key="'Login'")
示例代码:
import javax.annotation.Resource;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;import com.cy.ssm.mapper.UserMapper;import com.cy.ssm.beans.UserBean;import com.cy.ssm.service.ILoginService;@Servicepublic class LoginServiceImpl implements ILoginService {//private static Logger logger = LoggerFactory//.getLogger(LoginServiceImpl.class);@Resourceprivate UserMapper um;@Override@Cacheable(value="baseCache",key="'Login'")public UserBean Login(String username, String password) {return um.login(username, password);}}
注:ehcache注解用法
//将查询到的数据缓存到myCache中,并使用方法名称加上参数中的userNo作为缓存的关键 //通常更新操作只需刷新缓存中的某个值,所以为了准确的清除特定的缓存,故定义了这个唯一的密钥,从而不会影响其它缓存值 @Cacheable (value = "myCache" ,key = "'get'+#userNo" )
@CacheEvict (value ="myCache" ,key ="'get'+#userNo") 更新操作根绝用户编号更新缓存中的数据
@CacheEvict (value = “myCache” ,allEntries = true ) 清除缓存中所有数据
阅读全文
1 0
- 最近在集成springMVC+mybatis +ehcache 整合碰到了很多坑希望对想了解的同学有一定的帮助
- 一个涉及比较多的正则表达式, 希望可以帮助大家对正则有更多了解.
- 最近在学习windows 编程,了解了很多的基础的东西,遇见了很多的问题
- Hibernate的配置在myeclipse中的一个坑,希望帮助同样碰到问题的人
- 简单的整理了一下分页,希望对初学者有帮助
- 做了个C#的Hotkey简单封装,希望对大家有帮助
- 很棒一个差分约束系统的理解,希望对你有帮助,果断转了
- spring ,springMVC,Mybatis和ehcache整合遇到的问题
- GIS在灾害应急中的作用(希望对512地震的抗灾有帮助)
- 黑马程序员_张老师的交通红绿灯项目的制作和学习希望对刚开始学的同学能有帮助
- 我常去的几个站点,希望对同志们有帮助
- 发扬 cuike519的POWERDATAGRID!希望对各位有帮助!
- 从好友收罗来的,希望对自己有帮助。
- 一些创业的心得,希望对其他创业者有帮助
- php写的上传功能(希望对大家有帮助)
- 我的五年总结,希望对大家有帮助
- CAD的一些命令,希望对大家有帮助哦!!!
- 2010-11-20北京电信调查与调研笔试题~~~刚刚考完的,希望对以后的同学有帮助
- 分段上升子序列,二次DP (poj1239)(留坑待完善。。。)
- VUzzer研究01-VUzzer的安装和编译
- 序列化对单例的破坏
- Nginx是如何处理每一个请求的
- HTML实体
- 最近在集成springMVC+mybatis +ehcache 整合碰到了很多坑希望对想了解的同学有一定的帮助
- S0.4 二值图
- Linux之 xstart调用 x11vnc远程图形化桌面
- 作者声明
- TabLayout详解(实时更新,未完待续...)
- 关于JPA的简介
- syslog-ng详解——安装syslog-ng
- JRockit引起的证书问题
- js闭包