前言

使用SpringMVC4集成ehcache来缓存服务器数据。

开发环境

SpringMVC4、ehcache2.6、

项目结构

SpringMVC 集成ehcache

1、pom.xml

//除了SpringMVC 、sql server相关就是ehcache-core
<dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache-core</artifactId><version>2.6.11</version></dependency>

2、springmvc-servlet.xml

主要就是开启cache注解,同时导入spring cache命名空间。

<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsd"><!--从配置文件加载数据库信息--><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations" value="classpath:config/jdbc.properties"/><property name="fileEncoding" value="UTF-8"/></bean><!--配置数据源,这里使用Spring默认--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${sqlserver.driver}"/><property name="url" value="${sqlserver.url}"/><property name="username" value="${sqlserver.username}"/><property name="password" value="${sqlserver.password}"/></bean><!--扫描Mapper--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.autohome.dao"/></bean><!--配置sqlSessionFactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="configLocation" value="classpath:springmvc-mybatis.xml"/><property name="dataSource" ref="dataSource"/></bean><!--启用缓存注解--><cache:annotation-driven cache-manager="cacheManager"/><bean id="cacheManagerFactory" 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="cacheManagerFactory"/></bean><!--启用最新的注解器、映射器--><mvc:annotation-driven/><!--使用默认的Servlet来响应静态资源--><mvc:default-servlet-handler/><!--扫描Controller注解类--><context:component-scan base-package="com.autohome.controller" /><!--扫描Service注解类--><context:component-scan base-package="com.autohome.service"/><!--jsp视图解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"/><property name="suffix" value=".jsp"/></bean></beans>

3、ehcache.xml

xsd我配置以后一直报红,不知道怎么回事,后来试着运行了下发现不影响程序运行,索性就这么用着

<?xml version="1.0" encoding="gbk"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd"><diskStore path="java.io.tmpdir" /><defaultCachemaxElementsInMemory="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="false"/><cache name="myCache"eternal="false"maxElementsInMemory="10000"overflowToDisk="false"timeToIdleSeconds="0"timeToLiveSeconds="0"memoryStoreEvictionPolicy="LFU"/></ehcache>

3、Service层里调用Dao层时使用缓存注解

在listAllUser方法上使用Cacheable注解,第一次运行时缓存里还没数据控制台会打印query from database...再次调用则直接读取缓存数据

@Cacheable(value = "myCache")public List<User> listAllUser() {System.out.println("query from database...");return userDao.listAllUser();}

  

4、Controller里调用

@Controller
@RequestMapping("/User")
public class UserController {@AutowiredUserService userService;@RequestMapping("/index")public ModelAndView index(){System.out.println("size:"+userService.listAllUser().size());ModelAndView mav =new ModelAndView("index");return mav;}
}

  

转载于:https://www.cnblogs.com/sword-successful/p/6710106.html

SpringMVC4集成ehcache相关推荐

  1. MyBatis-24MyBatis缓存配置【集成EhCache】

    文章目录 概述 EhCache概述 特点 EhCache架构图 示例 1.添加mybatis-ehcache依赖 2. 配置EhCache 3.修改PrivilegeMapper.xml中的缓存配置 ...

  2. 集成Ehcache用来缓存表以后,怎么设置缓存刷新时间

    问答 集成Ehcache用来缓存表以后,怎么设置缓存刷新时间 发布于 217天前  作者 老司机  93 次浏览  复制  上一个帖子  下一个帖子  标签: 无 集成Ehcache用来缓存表以后,怎 ...

  3. 一文彻底搞懂Mybatis系列(十六)之MyBatis集成EhCache

    MyBatis集成EhCache 一.MyBatis集成EhCache 1.引入mybatis整合ehcache的依赖 2.类根路径下新建ehcache.xml,并配置 3.POJO类 Clazz 4 ...

  4. spring boot集成ehcache 2.x 用于hibernate二级缓存

    spring boot集成ehcache 2x 用于hibernate二级缓存 项目依赖 Ehcache简介 hibernate二级缓存配置 ehcache配置文件 ehcache事件监听 注解方式使 ...

  5. springboot2.3.4集成EhCache缓存框架完整代码

    代码部分 pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="ht ...

  6. Spring Caching集成Ehcache

    Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.在应用中用于常常需要读取的数据交换,而不是通过DB DAO数据交换(cache不占用DB宝贵的NIO,直接交换堆内存). 整合S ...

  7. 01_MyBatis EHCache集成及所需jar包,ehcache.xml配置文件参数配置及mapper中的参数配置

     1 与mybatis集成时需要的jar ehcache-core-2.6.5.jar mybatis-ehcache-1.0.2.jar Mybatis.日志.EHCache所需要的jar包如下 ...

  8. springboot+mybatis集成自定义缓存ehcache用法笔记

    今天小编给大家整理了springboot+mybatis集成自定义缓存ehcache用法笔记,希望对大家能有所办帮助! 一.ehcache介绍 EhCache 是一个纯Java的进程内缓存管理框架,属 ...

  9. SpringBoot集成Cache缓存(Ehcache缓存框架,注解方式)

    1.说明 Spring定义了CacheManager和Cache接口, 用来统一不同的缓存技术, 例如JCache,EhCache,Hazelcast,Guava,Redis等. 本文通过Spring ...

最新文章

  1. [Swift通天遁地]七、数据与安全-(19)使用Swift实现原生的SHA1加密
  2. LeetCode12- 整数转罗马数字
  3. 最轻量级的C协程库:Protothreads
  4. 条款七 为多态基类声明virtual析构函数
  5. 20150210--Smarty1-02
  6. rootfs 制作ubuntu_Ubuntu12笔记: 基于busybox的Linux小系统制作
  7. 一位工作了10年的C++程序员总结出这些忠告
  8. 【OpenCV】图像金字塔
  9. 【note】PAT甲级题目中的单词整理
  10. 【主机】计算机缓存机制
  11. manacher算法--最长回文子串
  12. JAVA中的继承和覆盖
  13. matlab画坐标系,Matlab如何绘制十字坐标系??
  14. IPv6Tools:IPv6安全审计框架
  15. TPM设备管理之设备采购方法及注意事项
  16. 实时网速怎么看快慢_如何知道网络的实时网速?4种方法轻松查询
  17. 2017年BEC剑桥商务英语考试指南
  18. python破解md5_python怎么使用md5加密解密
  19. 市场调研-邻苯二甲酰亚胺钾市场现状及未来发展趋势
  20. SONY ICX618AL/AQ 电路升级改造----第一章:初步方案确定

热门文章

  1. ASP.NET服务器端控件原理分析
  2. 全排列(含递归和非递归的解法)
  3. mysql创建表对经常要查询的列添加索引或者组合索引
  4. ARP欺骗原理与模拟
  5. mybaits四-2:模糊查询
  6. 【译】混沌工程与区块链
  7. 通过实例理解 RabbitMQ 的基本概念
  8. JavaScript正则表达式
  9. IOS开发笔记(Swift):UITableView表格视图的静态使用
  10. ACM POJ 2192 Zipper