SpringMVC4集成ehcache
前言
使用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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/cache
http://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" /> <defaultCache
maxElementsInMemory="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 { @Autowired
UserService userService; @RequestMapping("/index")
public ModelAndView index(){
System.out.println("size:"+userService.listAllUser().size()); ModelAndView mav =new ModelAndView("index"); return mav;
}
}
SpringMVC4集成ehcache的更多相关文章
- spring boot集成ehcache 2.x 用于hibernate二级缓存
https://www.jianshu.com/p/87b2c309b776 本文将介绍如何在spring boot中集成ehcache作为hibernate的二级缓存.各个框架版本如下 spring ...
- 集成Ehcache用来缓存表以后,怎么设置缓存刷新时间
问答 集成Ehcache用来缓存表以后,怎么设置缓存刷新时间 发布于 217天前 作者 老司机 93 次浏览 复制 上一个帖子 下一个帖子 标签: 无 集成Ehcache用来缓存表以后, ...
- (37)Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】
[本文章是否对你有用以及是否有好的建议,请留言] 写后感:博主写这么一系列文章也不容易啊,请评论支持下. 如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了. 那么我们先说说这一篇文 ...
- 如果有人问你 JFinal 如何集成 EhCache,把这篇文章甩给他
废话不多说,就说一句:在 JFinal 中集成 EhCache,可以提高系统的并发访问速度. 可能有人会问 JFinal 是什么,EhCache 是什么,简单解释一下. JFinal 是一个基于Jav ...
- Mybatis集成ehcache
Mybatis集成ehcache 1.为什么需要缓存 拉高程序的性能 2. 什么样的数据需要缓存 很少被修改或根本不改的数据 业务场景比如:耗时较高的统计分析sql.电话账单查询sql等 3. ehc ...
- spring集成ehcache本地缓存
1.maven依赖 <!-- ehcache 相关依赖 --> <dependency> <groupId>net.sf.ehcache</groupId&g ...
- SpringBoot 集成ehcache
1, 项目实在springboot 集成mybatis 的基础上的: https://www.cnblogs.com/pickKnow/p/11189729.html 2,pom 如下,有的不需要加, ...
- spring-data-redis集成ehcache实现缓存
1.结构 2.pom.xml <?xml version="1.0" encoding="UTF-8"?><project xmlns=&qu ...
- Spring Caching集成Ehcache
Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.在应用中用于常常需要读取的数据交换,而不是通过DB DAO数据交换(cache不占用DB宝贵的NIO,直接交换堆内存). 整合S ...
随机推荐
- python 用文本来提供输入信息的模板,不用每次都手动粘贴了
#下面这一段用一个txt来保存input的信息来模拟input.最后提交代码时候删除这一段即可. a9999=open('1.txt','r') def input(): return a9999.r ...
- You are using pip version 9.0.1, however version 9.0.3 is available.
1,pip不能用了,提示:You are using pip version 8.1.1, however version 9.0.1 is available.网上搜索了一箩筐的安装程序,各种安装, ...
- Maven依赖及范围
一.依赖范围(scope): 共5种,compile (编译).test (测试).runtime (运行时).provided.system compile:编译依赖范围,在编译,测试,运行时都需要 ...
- #10072. 「一本通 3.2 例 1」Sightseeing Trip(floyd求最小环+路径)
https://loj.ac/problem/10072 针对无向图 因为Floyd是按照结点的顺序更新最短路的,所以我们在更新最短路之前先找到一个连接点k,当前的点k肯定不存在于已存在的最短路f[i ...
- slecte下拉框的多选操作及获取值的 变化
对select增加一个 multiple属性,再获取多选的值的时候,对数据进行遍历,如果单纯的获取select的value值,指挥获取一个值, 遍历方法 可以先获取到select的dom元素到,然后对 ...
- Chrome浏览器用AdBlockPlus拦截百度广告
一:安装AdBlockPlus插件,这个貌似要FQ安装,不知道可不可以本地安装: 二:在右侧的扩展那里找到ABP扩展,然后设置-高级-我的过滤列表栏-开始创建我的过滤列表: 三:在列表栏里添加 bai ...
- Hive记录-Beeline常用操作命令
Beeline和其他工具有一些不同,执行查询都是正常的SQL输入,但是如果是一些管理的命令, 比如进行连接,中断,退出,执行Beeline命令需要带上"!",不需要终止符.常用命令 ...
- Dynamic Programming | Set 1 (Overlapping Subproblems Property)
动态规划是这样一种算法范式:将复杂问题划分为子问题来求解,并且将子问题的结果保存下来以避免重复计算.如果一个问题拥有以下两种性质,则建议使用动态规划来求解. 1 重叠子问题(Overlapping S ...
- jenkins+donet core持续集成环境搭建
一.Jenins+GitHub 参考 另外需要配置Global Tool Configuration 如果没有安装git,需下载安装,下载地址 二.jenkins发布donet core应用 1.配置 ...
- Java 中 & | ^ 运算符的简单使用
背景 今天碰到了代码中的按位与运算,复习一下,先列一个各个进制数据表. 顺便复习一下十进制转二进制的计算方式: 接下来解释下这三个运算符: & 按位与,都转为二进制的情况下,同为1则为1,否则 ...