您的位置:首页 > 其它

第一篇-SSM框架+ehcache详细配置(适合新手)

2018-03-06 16:35 260 查看
最近打算把常用的缓存技术总体复习一下,也希望帮助到和我遇到一样问题的亲们。
1.前言为了提高MyBatis的性能,有时候我们需要加入缓存支持,目前用的比较多的缓存莫过于ehcache缓存了,ehcache性能强大,而且位各种应用都提供了解决方案,在此我们主要是做查询缓存,提高查询的效率.2.环境Maven+Idea+Mysql最好是自己曾经搭建过ssm框架的人,然后你可以在这基础上进行整合。没有也没关系,后面会给源码的,哈哈哈。
3.需要的依赖包<!-- ehcache 相关依赖 -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.1.3</version>
</dependency>
<!--Mybatis-ehcache-->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.0.3</version>
</dependency>4.新建ehcache.xml(resources目录下) 直接拷贝过去就ok<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!-- java.io.tmpdir:Java临时目录。指定一个文件目录,当EhCache把数据写到硬盘上或者系统jvm内存时,将把数据写到这个文件目录下 -->
<diskStore path="java.io.tmpdir"/>
<!-- 设定缓存的默认数据过期策略 -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="10"
timeToLiveSeconds="10"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU"
diskExpiryThreadIntervalSeconds="120"/>
<!--
配置自定义缓存
maxElementsInMemory:缓存中允许创建的最大对象数
eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前,
两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,
如果该值是 0 就意味着元素可以停顿无穷长的时间。
timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
overflowToDisk:内存不足时,是否启用磁盘缓存。
memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。
-->
<!-- 自定义缓存策略-学生信息缓存容器对应策略-->
</ehcache>5.在mapper.xml加上一句话<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
<?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.peace.pms.dao.UserDao">
<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
<resultMap type="com.peace.pms.entity.User" id="UserResult">
<result property="username" column="username"/>
<result property="password" column="password"/>
</resultMap>
<select id="getall" resultType="com.peace.pms.entity.User">
select* from user
</select>
</mapper>
6.Spring进来了,怎么把ehcache和spring弄一起?
在application-context.xml加入下面的东东:<!-- 开启spring缓存 -->
<cache:annotation-driven cache-manager="cacheManager"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:encache.xml"></property>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache"></property>
</bean>其实就已经好了,是不是很简单呢。那怎么就知道成功呢,下面就是见证奇迹的时刻:
1.运行项目
2.访问http://localhost:8080/pms/user/list/





补一下sql:-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('11', '11');
INSERT INTO `user` VALUES ('22', '22');
INSERT INTO `user` VALUES ('33', '33');
源码:http://download.csdn.net/download/m0_37499059/10271960
你csdn没积分的话,就留下邮箱,我看到了肯定及时发给你,亲
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ssm ehcache 入门实例
相关文章推荐