spring mvc + mybatis 的ehcache 的简单实现

由于这两天用的springmvc  和 mybatis 的搭建的web 框架  然后准备用缓存数据,就简单记录下

准备:

googlecode 的ehcache

这个可以在https://code.google.com/p/ehcache-spring-annotations/  下载,下载之后拿出来要用到的jar包

    

下载的压缩包中的注解包

ehcache-spring-annotations-1.2.0.jar  

下载的压缩包中lib 目录下的

ehcache-core-2.4.5.jar
guava-r09.jar

  由于其他的一些包都在spring mvc   中已经有了,只需要添加这几个进去。

配置ehcache.xml

在src  目录下新增配置 ehcache.xml  如下:

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 *
 * 缓存配置
 * @author yq
 * @date 2014.9.10
 *
 */ -->

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
 updateCheck="false">
 <diskStore path="java.io.tmpdir" />
 <defaultCache eternal="false"
   maxElementsInMemory="1000"
   overflowToDisk="false"
   diskPersistent="false"
   timeToIdleSeconds="0"
   timeToLiveSeconds="600"
   memoryStoreEvictionPolicy="LRU" />  

 <cache name="CustomerCache"
   eternal="false"
   maxElementsInMemory="100"
   overflowToDisk="false"
   diskPersistent="false"
   timeToIdleSeconds="0"
   timeToLiveSeconds="300"
   memoryStoreEvictionPolicy="LRU" />  

</ehcache>  

此配置中 可以有多个cache ,方便管理我们程序中的cache。

配置spring.xml

1  <ehcache:annotation-driven cache-manager="ehCacheManager" />
2  <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
3        <property name="configLocation" value="classpath:ehcache.xml" />
4    </bean>  

接下来就到我们的代码 Service 层 试用缓存了:

@Cacheable(cacheName = "CustomerCache")
    public Customer GetCustomerByCid(int cid){
        System.out.print("----------------------------------------------------------");
        return cd.GetCustomerByCid(cid);
    }
    //修改客户
    @TriggersRemove(cacheName ={"CustomerCache"},removeAll=true)
    public int UpdateCustomer(Customer cus){
        return cd.UpdateCustomer(cus);
    }

注意包的import  ,是这两个

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

以上就已经搭建好了。

当我们两次调用 service 中的 GetCustomerByCid 方法时候控制台只有一次sql打印表示成功了

DEBUG - DispatcherServlet with name ‘myproject-dispatcher‘ processing GET request for [/jxc/customer/GetCustomerByCid]
DEBUG - Looking up handler method for path /customer/GetCustomerByCid
DEBUG - 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: -1
DEBUG - 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 SqlSession
DEBUG - SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
DEBUG - Fetching JDBC Connection from DataSource
DEBUG - JDBC Connection [jdbc:mysql://192.168.1.6:3306/lyjxc?useUnicode=true&characterEncoding=utf-8, [email protected], MySQL-AB JDBC Driver] will not be managed by Spring
DEBUG - 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 Customer where cid=?
DEBUG - ==> Parameters: 18(Integer)
DEBUG - Closing non transactional SqlSession [[email protected]]
DEBUG - Returning JDBC Connection to DataSource
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 ‘Customer‘ of type [com.ly.jxc.entity.Customer] to request in view with name ‘Settings/addCustomer‘
DEBUG - Added model object ‘springMacroRequestContext‘ 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 ‘Settings/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 request
DEBUG - Returning cached instance of singleton bean ‘sqlSessionFactory‘
DEBUG - DispatcherServlet with name ‘myproject-dispatcher‘ processing GET request for [/jxc/customer/GetCustomerByCid]
DEBUG - Looking up handler method for path /customer/GetCustomerByCid
DEBUG - 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: -1
DEBUG - 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 ‘Customer‘ of type [com.ly.jxc.entity.Customer] to request in view with name ‘Settings/addCustomer‘
DEBUG - Added model object ‘springMacroRequestContext‘ 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 ‘Settings/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 request
DEBUG - Returning cached instance of singleton bean ‘sqlSessionFactory‘

  

以上就是简单记录下。

时间: 2024-07-29 12:48:44

spring mvc + mybatis 的ehcache 的简单实现的相关文章

Spring MVC +MyBatis +MySQL 简单的登录查询 Demo 解决了mybatis异常

忙活了大半天,饭也没顾得上吃,哎许久不动手,一动手就出事,下面请看今天的重头戏,额吃个饭回来再发了! 1.整体结构 2.准备工作 数据库: --Mysql 5.6 创建数据库 wolf CREATE DATABASE wolf; 创建用户表 user create table user( id int  AUTO_INCREMENT  primary key, name varchar(25) not null, pwd varchar(20) not null, create_time dat

日常开发系列——Maven+Spring+Spring MVC+MyBatis+MySQL整合SSM框架

进入公司开发已经3个多月了,项目用的是Maven+Spring+Spring MVC+MyBatis+MySQL,趁这个周末有空,仔细研读一下公司项目的基本框架,学习一下这个环境是怎么搭建起来的,经过自己的研究终于是成功地试验出来.自己亲手做的才算是自己学到的,决定将其记录下来,以便日后查询,源码同时也欢迎大家拍砖. 一.数据库的准备 这次整合试验想着做个简单的,就决定做一个普通的用户登陆,就一张表吧 我新建的数据库名字是test,然后新建了一张表 DROP TABLE IF EXISTS `u

基于Spring + Spring MVC + Mybatis + shiro 高性能web构建

一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJS,做了大量的研究,对前后端交互有了更深层次的认识. 今天抽个时间写这篇文章,我有预感,这将是一篇很详细的文章,详细的配置,详细的注释,看起来应该很容易懂. 用最合适的技术去实现,并不断追求最佳实践.这就是架构之道. 希望这篇文章能给你们带来一些帮助,同时希望你们可以为这个项目贡献你的想法. 源码地址:https://github.com/starzou/quick4j 点击打开 看我们的项目结构: 是一个典型

[转]基于Spring + Spring MVC + Mybatis 高性能web构建

http://blog.csdn.net/zoutongyuan/article/details/41379851/ 一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJs,做了大量的研究,对前后端交互有了更深层次的认识. 今天抽个时间写这篇文章,我有预感,这将是一篇很详细的文章,详细的配置,详细的注释,看起来应该很容易懂. 用最合适的技术去实现,并不断追求最佳实践.这就是架构之道. 希望这篇文章能给你们带来一些帮助,同时希望你们可以为这个项目贡献你的想法. 源

基于Spring + Spring MVC + Mybatis 高性能web构建

一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJs,做了大量的研究,对前后端交互有了更深层次的认识. 今天抽个时间写这篇文章,我有预感,这将是一篇很详细的文章,详细的配置,详细的注释,看起来应该很容易懂. 用最合适的技术去实现,并不断追求最佳实践.这就是架构之道. 希望这篇文章能给你们带来一些帮助,同时希望你们可以为这个项目贡献你的想法. 源码地址:https://github.com/starzou/quick4j 点击打开 看我们的项目结构: 是一个典型

spring mvc+mybatis整合 (二)

转:http://lifeneveralone.diandian.com/post/2012-11-02/40042292065 本文介绍使用spring mvc结合Mybatis搭建一个应用程序框架. demo源码下载:springMVC-Mybatis 1.准备工作: spring的jar包: spring-beans-3.1.0.RELEASE.jar spring-core-3.1.0.RELEASE.jar spring-web-3.1.0.RELEASE.jar spring-web

spring Mvc + Mybatis 中使用junit

在Spring Mvc + Mybatis的项目中我们有时候需要在测试代码中注入Dao操作数据库,对表进行增删改查,实现如下: 这是一般的maven项目项目结构 测试代码一般写在src/test/java包下. 这是一个普通的测试类,通过mybatis查询某个表的数据. 1 public class SpringMybatisTest { 2 3 @Resource 4 private static TestDao testDao; 5 6 @BeforeClass 7 public stati

maven/eclipse搭建ssm(spring+spring mvc+mybatis)

maven/eclipse搭建ssm(spring+spring mvc+mybatis) 前言 本文旨在利用maven搭建ssm环境,而关于maven的具体内容,大家可以去阅读<Maven 实战>.其实园内这方面文章已有不少,那么为什么我还要重复造轮子呢?我只是想记录自己的实践过程罢了,附带着给别人以参考.对于别人的博文,看了固然好,要是能付之实践,那就更好了! maven安装 安装过程挺简单的,去apache下载apache-maven的zip包,解压到一个目录下,如下图 接下来配置win

Spring MVC+Mybatis+Maven+Velocity+Mysql整合实例

本篇文章将通过一个简单显示用户信息的实例整合Spring mvc+mybatis+Maven+velocity+mysql. 对于实现整合的重点在于以下几个配置文件的实现 1.Maven依赖包 2.spring配置文件(springContext-user.xml) 3.mybatis配置文件(MyBatis-User-Configuration.xml) 4.spring-mvc配置文件(spring-mvc.xml) 5.web.xml配置文件 源码下载地址:http://download.