如何通过shiro注解配置ehcachee

spring3.2+ehcache 注解使用 - Hello World - ITeye技术网站
博客分类:
我一直不喜欢hibernate ,但是框架是spring mvc + hibernate 搭建,而且我喜欢自己写SQL,数据层 是自己封装的也写东西,没用hql 语句,也就没用他那些缓存,自己也想缓存一部分数据,所以就想自己写个缓存,或者用现成的缓存,通过spring 拦截,实现颗粒度比较细,容易控制的缓存。了解了下,spring 3.0 以后,应该从3.1 以后吧,注解方式的缓存就已经实现,下面是我自己做的例子,分享给大家:
例子内容介绍:
1.没用数据库,用的集合里面的数据,也就没事务之类的,完成的一个CRUD操作
2.主要测试内容,包括第一次查询,和反复查询,缓存是否生效,更新之后数据同步的问题
3.同时含有一些常用参数绑定等东西
4.为了内容简单,我没有使用接口,就是User,UserControl,UserServer,UserDao 几个类,以及xml 配置文件
直接看类吧,源码文件,以及jar 都上传,方便大家下载:
package com.
public class User {
// 这个需要,不然在实体绑定的时候出错
public User(){}
public User(Integer id, String name, String password) {
this.name =
this.password =
public Integer getId() {
public void setId(Integer id) {
public String getName() {
public void setName(String name) {
this.name =
public String getPassword() {
public void setPassword(String password) {
this.password =
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password
package com.
import java.util.ArrayL
import java.util.L
import org.springframework.stereotype.R
* 静态数据,模拟数据库操作
@Repository("userDao")
public class UserDao {
List&User& users = initUsers();
public User findById(Integer id){
User user =
for(User u : users){
if(u.getId().equals(id)){
public void removeById(Integer id){
User user =
for(User u : users){
if(u.getId().equals(id)){
users.remove(user);
public void addUser(User u){
users.add(u);
public void updateUser(User u){
addUser(u);
// 模拟数据库
private List&User& initUsers(){
List&User& users = new ArrayList&User&();
User u1 = new User(1,"张三","123");
User u2 = new User(2,"李四","124");
User u3 = new User(3,"王五","125");
users.add(u1);
users.add(u2);
users.add(u3);
package com.
import java.util.L
import org.springframework.beans.factory.annotation.A
import org.springframework.cache.annotation.CacheE
import org.springframework.cache.annotation.C
import org.springframework.stereotype.S
* 业务操作,
@Service("userService")
public class UserService {
@Autowired
private UserDao userD
// 查询所有,不要key,默认以方法名+参数值+内容 作为key
@Cacheable(value = "serviceCache")
public List&User& getAll(){
printInfo("getAll");
return userDao.
// 根据ID查询,ID 我们默认是唯一的
@Cacheable(value = "serviceCache", key="#id")
public User findById(Integer id){
printInfo(id);
return userDao.findById(id);
// 通过ID删除
@CacheEvict(value = "serviceCache", key="#id")
public void removeById(Integer id){
userDao.removeById(id);
public void addUser(User u){
if(u != null && u.getId() != null){
userDao.addUser(u);
// key 支持条件,包括 属性condition ,可以 id & 10 等等类似操作
// 更多介绍,请看参考的spring 地址
@CacheEvict(value="serviceCache", key="#u.id")
public void updateUser(User u){
removeById(u.getId());
userDao.updateUser(u);
// allEntries 表示调用之后,清空缓存,默认false,
// 还有个beforeInvocation 属性,表示先清空缓存,再进行查询
@CacheEvict(value="serviceCache",allEntries=true)
public void removeAll(){
System.out.println("清除所有缓存");
private void printInfo(Object str){
System.out.println("非缓存查询----------findById"+str);
package com.
import java.util.L
import org.springframework.beans.factory.annotation.A
import org.springframework.stereotype.C
import org.springframework.ui.M
import org.springframework.web.bind.annotation.ModelA
import org.springframework.web.bind.annotation.PathV
import org.springframework.web.bind.annotation.RequestM
@Controller
public class UserControl {
@Autowired
private UserService userS
// 提前绑定视图,提前绑定数据,方便查看数据变化
@ModelAttribute("users")
public List&User& cityList() {
return userService.getAll();
// 根据ID查询
@RequestMapping("/get/{id}")
public String getUserById(Model model,@PathVariable Integer id){
User u = userService.findById(id);
System.out.println("查询结果:"+u);
model.addAttribute("user", u);
return "forward:/jsp/edit";
@RequestMapping("/del/{id}")
public String deleteById(Model model,@PathVariable Integer id){
printInfo("删除-----------");
userService.removeById(id);
return "redirect:/jsp/view";
@RequestMapping("/add")
public String addUser(Model model,@ModelAttribute("user") User user){
printInfo("添加----------");
userService.addUser(user);
return "redirect:/jsp/view";
@RequestMapping("/update")
public String update(Model model,@ModelAttribute User u){
printInfo("开始更新---------");
userService.updateUser(u);
model.addAttribute("user", u);
return "redirect:/jsp/view";
// 清空所有
@RequestMapping("/remove-all")
public String removeAll(){
printInfo("清空-------------");
userService.removeAll();
return "forward:/jsp/view";
// JSP 跳转
@RequestMapping("/jsp/{jspName}")
public String toJsp(@PathVariable String jspName){
System.out.println("JSP TO --&&" +jspName);
return jspN
private void printInfo(String str){
System.out.println(str);
XML 配置:
&?xml version="1.0" encoding="UTF-8"?&
&web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="/xml/ns/javaee" xmlns:web="/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"&
&display-name&springEhcache&/display-name&
&servlet-name&spring&/servlet-name&
&servlet-class&org.springframework.web.servlet.DispatcherServlet&/servlet-class&
&init-param&
&param-name&contextConfigLocation&/param-name&
&param-value&classpath:com/config/springmvc-context.xml&/param-value&
&/init-param&
&load-on-startup&1&/load-on-startup&
&/servlet&
&servlet-mapping&
&servlet-name&spring&/servlet-name&
&url-pattern&/&/url-pattern&
&/servlet-mapping&
&/web-app&
&?xml version="1.0" encoding="UTF-8"?&
&ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"&
&!-- service 缓存配置 --&
&cache name="serviceCache"
eternal="false"
maxElementsInMemory="100"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LRU" /&
&/ehcache&
&?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:context="http://www.springframework.org/schema/context"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd"&
&!-- 默认扫描 @Component @Repository
@Service @Controller --&
&context:component-scan base-package="com.se" /&
&!-- 一些@RequestMapping 请求和一些转换 --&
&mvc:annotation-driven /&
&!-- 前后缀 --&
&bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&
&property name="prefix" value="/"/&
&property name="suffix" value=".jsp"/&
静态资源访问 的两种方式
&!-- &mvc:default-servlet-handler/&
&mvc:resources location="/*" mapping="/**" /&
&bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"&
&property name="configLocation"
value="classpath:com/config/ehcache.xml"/&
&!-- 支持缓存注解 --&
&cache:annotation-driven cache-manager="cacheManager" /&
&!-- 默认是cacheManager --&
&bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"&
&property name="cacheManager"
ref="cacheManagerFactory"/&
JSP 配置:
&%@ page language="java" contentType="text/ charset=Utf-8" pageEncoding="Utf-8"%&
&meta http-equiv="Content-Type" content="text/ charset=UTF-8"&
&title&Insert title here&/title&
&script type="text/javascript" src="&%=request.getContextPath()%&/js/jquery-1.4.3.js"&&/script&
&strong&用户信息&/strong&&br&
输入编号:&input id="userId"&
&a href="#" id="edit"&编辑&/a&
&a href="#" id="del" &删除&/a&
&a href="&%=request.getContextPath()%&/jsp/add"&添加&/a&
&a href="&%=request.getContextPath()%&/remove-all"&清空缓存&/a&&br/&
&p&所有数据展示:&p/&
&p&静态图片访问测试:&/p&
&img style="width: 110height: 110px" src="&%=request.getContextPath()%&/img/404cx.png"&&br&
&script type="text/javascript"&
$(document).ready(function(){
$('#userId').change(function(){
var userId = $(this).val();
var urlEdit = '&%=request.getContextPath()%&/get/'+userId;
var urlDel = '&%=request.getContextPath()%&/del/'+userId;
$('#edit').attr('href',urlEdit);
$('#del').attr('href',urlDel);
&%@ page language="java" contentType="text/ charset=UTF-8"
pageEncoding="UTF-8"%&
&!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&
&script type="text/javascript" src="../js/jquery-1.4.3.js"&&/script&
&strong&编辑&/strong&&br&
&form id="edit" action="&%=request.getContextPath()%&/update" method="get"&
用户ID:&input id="id" name="id" value="${user.id}"&&br&
用户名:&input id="name" name="name" value="${user.name}"&&br&
用户密码:&input id="password" name="password" value="${user.password}"&&br&
&input value="更新" id="update" type="submit"&
&%@ page language="java" contentType="text/ charset=UTF-8"
pageEncoding="UTF-8"%&
&!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&
&meta http-equiv="Content-Type" content="text/ charset=UTF-8"&
&title&Insert title here&/title&
&script type="text/javascript" src="../js/jquery-1.4.3.js"&&/script&&
&strong&添加&/strong&
&form action="&%=request.getContextPath()%&/add" method="post"&
用户ID:&input
name="id" &&br&
用户名:&input
name="name" &&br&
用户密码:&input
name="password"&&br&
&input value="添加" id="add" type="submit"&
关于测试方式:
输入地址:
然后根据ID 进行查询编辑, 还有添加,以及删除等操作,这里仅仅做简单实例,更多的需要大家自己做写。测试用例的我也导入了,业务可以自己写。
里面用到了很多参考资料:
这个是google
弄的例子,实例项目类似,就不上传了
这个是涛ge,对spring cache 的一些解释
还有就是spring 官网的一些资料,我是自己下的src ,里面文档都有,会上传
自己路径的文件地址/spring-3.2.0.M2/docs/reference/htmlsingle/index.html
关于Ehcache 的介绍,可以参考
还有我缓存分类里面转的文章,关于几个主流缓存分类 以及区别的。
1.现在aop 和DI 用得很多了,也特别的方便,但是框架太多了,但是要核心源码搞清楚了才行
2.上面的缓存机制还可以运用到类上,和事务差不多,反正里面东西挺多了, 我就不一一介绍了,可以自己进行扩展研究。
3.感觉spring 的缓存,颗粒度还可以再细,精确到元素,或者可以提供对指定方法,指定内容的时间控制,而不是只能通过xml,也可以在方法上直接加参数,细化元素的缓存时间,关于这点的作用来说,我项目中,比如A 结果集,我想缓存10秒,B结果集想缓存50秒,就不用再次去配置缓存了。而且ehcache 是支持元素级的颗粒控制,各有想法吧。当然看自己喜欢用xml 方式拦截呢,还是注解使用,这里仅提供了注解方式,我比较喜欢~。~
4.有问题的地方欢迎大家,多指正!
不能超过10M,就分开传的,包括项目,以及文档介绍
(113.4 KB)
下载次数: 887
下载次数: 654
下载次数: 728
浏览 33072
Java编码的时候直接在new Element()的时候就可以设置Element级的超时如何用注解设置Element级别的超时?其实我再这里也没发现这个东西,或者我不够仔细,反正颗粒度是不够的。我是配置了多个,然后指定使用的。比如5分总 10分钟 1小时& 天。。等等要么自己写一个注解。我现在用memcache 了,他的颗粒度是支持到这个的。
greemranqq 写道greemranqq 写道mmmmmmmmj 写道你好,我结合了数据库试了下,添加完之后数据不能刷新,只有点一下清空缓存数据才能更新出来,或者把添加后的 redirect:/jsp/view 改成 forward:/jsp/view 。请问这是为什么?添加的时候,你的放到缓存里面。不然缓存里面的值 还是以前的,包括 更新也是一样的。你可以看看 @CachePut& 和 @CacheEvict至于JSP 那里问题,你参考 转发和重定向用了 cacheput数据也刷新不了,用forward页面数据能更新,redirect就不行,我的理解是 业务层的缓存 和 forward 这些没关的。你可以debug 进缓存 比如cacheput 或者 其他缓存逐 里面去看看执行的时候,放进去没有。或者其他地方配置是否有问题。
greemranqq 写道mmmmmmmmj 写道你好,我结合了数据库试了下,添加完之后数据不能刷新,只有点一下清空缓存数据才能更新出来,或者把添加后的 redirect:/jsp/view 改成 forward:/jsp/view 。请问这是为什么?添加的时候,你的放到缓存里面。不然缓存里面的值 还是以前的,包括 更新也是一样的。你可以看看 @CachePut& 和 @CacheEvict至于JSP 那里问题,你参考 转发和重定向用了 cacheput数据也刷新不了,用forward页面数据能更新,redirect就不行,
mmmmmmmmj 写道你好,我结合了数据库试了下,添加完之后数据不能刷新,只有点一下清空缓存数据才能更新出来,或者把添加后的 redirect:/jsp/view 改成 forward:/jsp/view 。请问这是为什么?添加的时候,你的放到缓存里面。不然缓存里面的值 还是以前的,包括 更新也是一样的。你可以看看 @CachePut& 和 @CacheEvict至于JSP 那里问题,你参考 转发和重定向
你好,我结合了数据库试了下,添加完之后数据不能刷新,只有点一下清空缓存数据才能更新出来,或者把添加后的 redirect:/jsp/view 改成 forward:/jsp/view 。请问这是为什么?添加的时候,你的放到缓存里面。不然缓存里面的值 还是以前的,包括 更新也是一样的。你可以看看 @CachePut& 和 @CacheEvict
shmily2038 写道看了下,是可以实现缓存了,不过配置这块,还有挺多细节的要注意的。这仅仅是这个入门的资料~。~,我道行不高,没那么多深奥的东西,关于实战方面,我们做线下数据分析的,1个小时分析一次 类似的数据,缓存这块用得比较多,不用长时间访问数据库,其实所谓的实战 还得看 你们缓存在 业务上的应用是什么,首先要明白缓存是什么,能给你们带来什么好处,用哪一款,等等,这些才是需要 你实际考虑的,而技术本身太多,找资料都能学会的嗯,技术学习只是时间问题,具体看业务就是
看了下,是可以实现缓存了,不过配置这块,还有挺多细节的要注意的。这仅仅是这个入门的资料~。~,我道行不高,没那么多深奥的东西,关于实战方面,我们做线下数据分析的,1个小时分析一次 类似的数据,缓存这块用得比较多,不用长时间访问数据库,其实所谓的实战 还得看 你们缓存在 业务上的应用是什么,首先要明白缓存是什么,能给你们带来什么好处,用哪一款,等等,这些才是需要 你实际考虑的,而技术本身太多,找资料都能学会的
要是使用数据库,该怎么实现?额,数据库你直接加上就可以了,也就是把dao 层的数据 来源 从数据库获取
看了下,是可以实现缓存了,不过配置这块,还有挺多细节的要注意的。
谢谢,大哥,这块实战还不够,能介绍一些这方面的实际的例子吗?或者介绍一下深入的方向
greemranqq
浏览: 537967 次
来自: 重庆
16/11/18 16:21:17 ERROR SparkDe ...
什么情况下会有 p!=t ?
给个我实现的代码,需要自己写一个类继承之~
public ab ...
100%发生死锁!
if (isValid(conn))Hibernate+spring配置ehcache缓存的方式 -
- ITeye技术网站
博客分类:
本文主要讲一讲Hibernate+EhCache配置二级缓存的基本使用方法,主要分以下两个方面介绍:
(有关EhCache的基础介绍可参见:/blog/1288257 )
&&& * Cache的多种配置方法
&&& * Hibernate+EhCache集成demo
[一]、Cache的多种配置方法
Javabean cache的配置有三种,下面将一一介绍,具体如下::
(1).bean中注解配置的方式: @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
(2).hibernate.cfg.xml中标签配置方式: &class-cache class="" usage="" /&
(3).映射文件*.hb.xml中标签配置方式: &cache usage=" /&
1. classpath:ehcahce.xml配置文件如下:
&&& &?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"/&
&&&&&&& &transactionManagerLookup class="net.sf.ehcache.transaction.manager.DefaultTransactionManagerLookup"
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& properties="jndiName=java:/TransactionManager" propertySeparator=";"/&
&&&&&&& &cacheManagerEventListenerFactory class="" properties=""/&
&&&&&&& &defaultCache
&&&&&&&&&&&&&&& maxElementsInMemory="100"
&&&&&&&&&&&&&&& eternal="false"
&&&&&&&&&&&&&&& timeToIdleSeconds="120"
&&&&&&&&&&&&&&& timeToLiveSeconds="120"
&&&&&&&&&&&&&&& overflowToDisk="true"
&&&&&&&&&&&&&&& diskSpoolBufferSizeMB="30"
&&&&&&&&&&&&&&& maxElementsOnDisk="100"
&&&&&&&&&&&&&&& diskPersistent="false"
&&&&&&&&&&&&&&& diskExpiryThreadIntervalSeconds="120"
&&&&&&&&&&&&&&& memoryStoreEvictionPolicy="LRU"
&&&&&&&&&&&&&&& statistics="false"
&&&&&&&&&&&&&&& /&
&&&&&&& &cache name="org.hibernate.cache.StandardQueryCache"
&&&&&&&
maxElementsInMemory="5"
eternal="false"
timeToLiveSeconds="120"
&&&&&&&
overflowToDisk="true" /&
&&&&&&& &cache name="org.hibernate.cache.UpdateTimestampsCache"
&&&&&&&
maxElementsInMemory="5000"
eternal="true"
overflowToDisk="true" /&
&&&&&&& &cache name="sampleCache1"
&&&&&&&&&&&&&& maxElementsInMemory="10000"
&&&&&&&&&&&&&& maxElementsOnDisk="1000"
&&&&&&&&&&&&&& eternal="false"
&&&&&&&&&&&&&& overflowToDisk="true"
&&&&&&&&&&&&&& diskSpoolBufferSizeMB="20"
&&&&&&&&&&&&&& timeToIdleSeconds="300"
&&&&&&&&&&&&&& timeToLiveSeconds="600"
&&&&&&&&&&&&&& memoryStoreEvictionPolicy="LFU"
&&&&&&&&&&&&&& transactionalMode="off"
&&&&&&&&&&&&&&& /&
&&&&&&& &cache name="sampleCache2"
&&&&&&&&&&&&&& maxElementsInMemory="1000"
&&&&&&&&&&&&&& eternal="true"
&&&&&&&&&&&&&& overflowToDisk="false"
&&&&&&&&&&&&&& memoryStoreEvictionPolicy="FIFO"
&&&&&&&&&&&&&&& /&
&&& &/ehcache&
2.hibernate配置文件:hibernate.cfg.xml
&&& &!DOCTYPE hibernate-configuration
&&&&&&& PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
&&&&&&& "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&
&&& &hibernate-configuration&
&session-factory&
&&&
&property name="connection.driver_class"&
&&&
oracle.jdbc.driver.OracleDriver
&&&
&/property&
&&&
&property name="connection.url"&
&&&
jdbc:oracle:thin:@localhost:1521:ora11g
&&&
&/property&
&&&
&property name="connection.username"&mytest&/property&
&&&
&property name="connection.password"&111111&/property&
&property name="dialect"&
&&&
org.hibernate.dialect.Oracle9Dialect
&&&
&/property&
&property name="connection.useUnicode"&true&/property&
&&&
&property name="connection.characterEncoding"&UTF-8&/property&
&&&
&property name="connection.SetBigStringTryClob"&true&/property&
&&&
&property name="connection.pool_size"&10&/property&
&&&
&property name="hibernate.jdbc.batch_size"&10&/property&
&property name="show_sql"&true&/property&
&&&
&property name="format_sql"&false&/property&
&&&
&property name="current_session_context_class"&thread&/property&
&&&
&property name="hbm2ddl.auto"&update&/property&
&&&
&!--& Hibernate 3.3 and higher --&
&&&
&property name="hibernate.cache.region.factory_class"&
&&&
net.sf.ehcache.hibernate.EhCacheRegionFactory
&&&
&/property&
&&&
&property name="hibernate.cache.region.factory_class"&
&&&
net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
&&&
&/property&
&&&
&!-- hibernate3.0-3.2 cache config--&
&&&
&property name="hibernate.cache.region.factory_class"&
&&&
net.sf.ehcache.hibernate.EhCacheProvider
&&&
&/property&
&&&&
&property name="hibernate.cache.provider_class"&
&&&
net.sf.ehcache.hibernate.SingletonEhCacheProvider
&&&
&/property&
&&&
&!-- Enable Second-Level Cache and Query Cache Settings --&
&&&
&property name="hibernate.cache.use_second_level_cache"&
&&&
&/property&
&&&
&property name="hibernate.cache.use_query_cache"&
&&&
&/property&
&!-- 注解配置& --&
&&&
&mapping class="michael.cache.ehcache.hibernate.EhUserInfo" /&
&&&
&mapping class="michael.cache.ehcache.hibernate.EhBlogTopic" /&
&&&
&mapping class="michael.cache.ehcache.hibernate.EhBlogTopic2" /&
&!-- 映射文件 --&
&&&
&mapping
&&&
resource="michael/cache/ehcache/hibernate/tb_EhBlogTopic3.hb.xml" /&
&!-- class-cache config --&
&&&
&class-cache class="michael.cache.ehcache.hibernate.EhBlogTopic"
&&&
usage="read-write" /&
&/session-factory&
&&& &/hibernate-configuration&
3.相关javabean代码片段如下:
(1).hibernate.cfg.xml中&calss-cache&标签配置的:EhBlogTopic.java:
&&& /**
&&&& * @blog
&&&& * @author Michael
&&&& */
&&& @Entity
&&& @Table(name = "MY_TB_EH_BLOG_TOPIC")
&&& public class EhBlogTopic implements Serializable {
&&&&&&& /**
&&&&&&&& * serialVersionUID
&&&&&&&& */
&&&&&&& private static final long serialVersionUID = -909799L;
&&&&&&& private I
&&&&&&& private String userId;
&&&&&&& private S
&&&&&&& private S
&&&&&&& //其他省略
&&& }
(2). bean中注解的方式配置cache的:EhBlogTopic2.java
&&& /**
&&&& * @blog
&&&& * @author Michael
&&&& */
&&& @Entity
&&& @Table(name = "MY_TB_EH_BLOG_TOPIC2")
&&& @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
&&& public class EhBlogTopic2 implements Serializable {
&&&&&&&&& //属性和EhBlogTopic一样
&&&&&&&& //其他省略
&&& }
(3). 映射文件*.hb.xml中添加cache标签的: EhBlogTopic3.java
&&& /**
&&&& * @blog
&&&& * @author Michael
&&&& */
&&& public class EhBlogTopic3 implements Serializable {
&&&&&& //属性和EhBlogTopic一样
&&&&&& //其他省略
&&& }
tb_EhBlogTopic3.hb.xml
&&& &?xml version="1.0"?&
&&& &!DOCTYPE hibernate-mapping PUBLIC
&&&
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
&&&
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&
&&& &hibernate-mapping package="michael.cache.ehcache.hibernate"&
&class name="EhBlogTopic3" table="MY_TB_EH_BLOG_TOPIC3"&
&&&
&cache usage="read-write" /&
&&&
&id name="id" type="int" unsaved-value="null"&
&&&
&generator class="increment" /&
&&&
&property name="userId" column="USER_ID" type="string"
&&&
not-null="false" length="20" /&
&property name="topic" column="TOPIC" type="string"
&&&
not-null="false" length="100" /&
&&&
&property name="site" column="SITE" type="string"
&&&
not-null="false" length="100" /&
&/class&
&&& &/hibernate-mapping&
(4). 没有配置cache的bean:EhUserInfo.java
&&& /**
&&&& * @blog
&&&& * @author Michael
&&&& */
&&& @Entity
&&& @Table(name = "MY_TB_EH_USER_INFO")
&&& public class EhUserInfo implements Serializable {
&&&&&&& /**
&&&&&&&& * serialVersionUID
&&&&&&&& */
&&&&&&& private static final long serialVersionUID = 679239L;
&&&&&&& private I
&&&&&&& private String userId;
&&&&&&& private String userN
&&&&&&& private String otherI
&&&&&&& /**
&&&&&&&& * @return the id
&&&&&&&& */
&&&&&&& @Id
&&&&&&& @GeneratedValue
&&&&&&& @Column(name = "ID")
&&&&&&& public Integer getId() {
&&&&&&&&&&&
&&&&&&& }
&&&&& //其他省略。。。
4.测试运行代码如下:
&&& package michael.cache.ehcache.
&&& import java.util.L
&&& import michael.hibernate.bigstring.oracle.BigStrB
&&& import net.sf.ehcache.C
&&& import net.sf.ehcache.CacheM
&&& import org.hibernate.Q
&&& import org.hibernate.S
&&& import org.hibernate.SessionF
&&& import org.hibernate.T
&&& import org.hibernate.cfg.AnnotationC
&&& import org.hibernate.cfg.C
&&& /**
&&&& *
&&&& * @blog
&&&& * @author Michael
&&&& */
&&& public class TestEhcacheHibernate {
&&&&&&& /**
&&&&&&&& * @param args
&&&&&&&& */
&&&&&&& @SuppressWarnings("unchecked")
&&&&&&& public static void main(String[] args) {
&&&&&&&&&&& testMulitConfigMethod();
&&&&&&& }
&&&&&&& /**
&&&&&&&& * 测试多种配置缓存的方法
&&&&&&&& */
&&&&&&& public static void testMulitConfigMethod() {
&&&&&&&&&&& SessionFactory sessionFactory =
&&&&&&&&&&& try {
&&&&&&&&&&&&&&& System.out.println("ehcache - hibernate Test ...");
&&&&&&&&&&&&&&& Configuration config = new AnnotationConfiguration()
&&&&&&&&&&&&&&&&&&&&&&& .configure("michael/cache/ehcache/hibernate/hibernate.cfg.xml");
&&&&&&&&&&&&&&& System.out.println("hibernate config successful :" + config);
&&&&&&&&&&&&&&& sessionFactory = config.buildSessionFactory();
&&&&&&&&&&&&&&& Transaction ta =
&&&&&&&&&&&&&&& try {
&&&&&&&&&&&&&&&&&&& Session session = sessionFactory.getCurrentSession();
&&&&&&&&&&&&&&&&&&& ta = session.beginTransaction();
&&&&&&&&&&&&&&& } catch (Exception e) {
&&&&&&&&&&&&&&&&&&& e.printStackTrace();
&&&&&&&&&&&&&&&&&&& ta.rollback();
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& String[] cacheNames = CacheManager.getInstance().getCacheNames();
&&&&&&&&&&&&&&& System.out.println("缓存的key cacheNames length := "
&&&&&&&&&&&&&&&&&&&&&&& + cacheNames.length + " 具体详细列表如下:");
&&&&&&&&&&&&&&& for (String name : cacheNames) {
&&&&&&&&&&&&&&&&&&& System.out.println("name := " + name);
&&&&&&&&&&&&&&& }
&&&&&&&&&&& } catch (Exception e) {
&&&&&&&&&&&&&&& e.printStackTrace();
&&&&&&&&&&& }
&&&&&&&&&&& System.out.println("ehcache - hibernate Test end.");
&&&&&&& }
&&& }
运行结果如下:
ehcache - hibernate Test ...
hibernate config successfulrg.hibernate.cfg.AnnotationConfiguration@193c0cf
11:32:36 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic]; using defaults.
11:32:36 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic2]; using defaults.
11:32:36 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic3]; using defaults.
11:32:37 net.sf.ehcache.util.UpdateChecker doCheck
信息: New update(s) found: 2.4.6 [http://www.terracotta.org/confluence/display/release/Release+Notes+Ehcache+Core+2.4]. Please check http://ehcache.org for the latest version.
缓存的key cacheNames length := 7 具体详细列表如下:
name := sampleCache2
name := michael.cache.ehcache.hibernate.EhBlogTopic2
name := org.hibernate.cache.UpdateTimestampsCache
name := sampleCache1
name := michael.cache.ehcache.hibernate.EhBlogTopic
name := org.hibernate.cache.StandardQueryCache
name := michael.cache.ehcache.hibernate.EhBlogTopic3
ehcache - hibernate Test end.
从运行结果可见:三种方式的缓存配置都已经成功。
[二]、Hibernate+EhCache集成demo
1.分别初始化EhUserInfo(没有配置cache的)和EhBlogTopic(配置过cache的)数据如下:
EhUserInfo:
EhBlogTopic:
2.演示代码:
&&& package michael.cache.ehcache.
&&& import java.util.L
&&& import michael.hibernate.bigstring.oracle.BigStrB
&&& import net.sf.ehcache.C
&&& import net.sf.ehcache.CacheM
&&& import org.hibernate.Q
&&& import org.hibernate.S
&&& import org.hibernate.SessionF
&&& import org.hibernate.T
&&& import org.hibernate.cfg.AnnotationC
&&& import org.hibernate.cfg.C
&&& /**
&&&& *
&&&& * @blog
&&&& * @author Michael
&&&& */
&&& public class TestEhcacheHibernate {
&&&&&&& /**
&&&&&&&& * @param args
&&&&&&&& */
&&&&&&& @SuppressWarnings("unchecked")
&&&&&&& public static void main(String[] args) {
&&&&&&&&&&& SessionFactory sessionFactory =
&&&&&&&&&&& try {
&&&&&&&&&&&&&&& System.out.println("ehcache - hibernate Test ...");
&&&&&&&&&&&&&&& Configuration config = new AnnotationConfiguration()
&&&&&&&&&&&&&&&&&&&&&&& .configure("michael/cache/ehcache/hibernate/hibernate.cfg.xml");
&&&&&&&&&&&&&&& sessionFactory = config.buildSessionFactory();
&&&&&&&&&&&&&&& System.out.println("buildSessionFactory===============");
&&&&&&&&&&&&&&& System.out.println("====================================");
&&&&&&&&&&&&&&& System.out.println("session open ....");
&&&&&&&&&&&&&&& System.out.println("同一个session(一级缓存默认的)中,没有配置cache的Bean");
&&&&&&&&&&&&&&& Transaction ta =
&&&&&&&&&&&&&&& try {
&&&&&&&&&&&&&&&&&&& Session session = sessionFactory.getCurrentSession();
&&&&&&&&&&&&&&&&&&& String hsql = "select t from EhUserInfo t ";
&&&&&&&&&&&&&&&&&&& ta = session.beginTransaction();
&&&&&&&&&&&&&&&&&&& Query query = session.createQuery(hsql);
&&&&&&&&&&&&&&&&&&& System.out.println("查询全部query.list().size:"
&&&&&&&&&&&&&&&&&&&&&&&&&&& + query.list().size());
&&&&&&&&&&&&&&&&&&& System.out.println("再根据ID=1查询某记录时不会再去查数据库,故不会打印hsql");
&&&&&&&&&&&&&&&&&&& EhUserInfo vo1 = (EhUserInfo) session.get(EhUserInfo.class, 1);
&&&&&&&&&&&&&&&&&&& System.out.println("根据ID=1找到的记录:" + vo1);
&&&&&&&&&&&&&&&&&&& ta.commit();
&&&&&&&&&&&&&&& } catch (Exception e) {
&&&&&&&&&&&&&&&&&&& e.printStackTrace();
&&&&&&&&&&&&&&&&&&& ta.rollback();
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& System.out.println("session closed.");
&&&&&&&&&&&&&&& System.out.println("session open ....");
&&&&&&&&&&&&&&& try {
&&&&&&&&&&&&&&&&&&& Session session = sessionFactory.getCurrentSession();
&&&&&&&&&&&&&&&&&&& ta = session.beginTransaction();
&&&&&&&&&&&&&&&&&&& System.out.println("第一次根据ID=1查询某记录时,会打印hsql");
&&&&&&&&&&&&&&&&&&& EhUserInfo vo1 = (EhUserInfo) session.get(EhUserInfo.class, 1);
&&&&&&&&&&&&&&&&&&& System.out.println("根据ID=1找到的记录:" + vo1);
&&&&&&&&&&&&&&&&&&& ta.commit();
&&&&&&&&&&&&&&& } catch (Exception e) {
&&&&&&&&&&&&&&&&&&& e.printStackTrace();
&&&&&&&&&&&&&&&&&&& ta.rollback();
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& System.out.println("session closed.");
&&&&&&&&&&&&&&& System.out.println("====================================");
&&&&&&&&&&&&&&& System.out.println("当前同一个sessionFactory,没有配置cache的Bean,");
&&&&&&&&&&&&&&& System.out.println("session open ....");
&&&&&&&&&&&&&&& try {
&&&&&&&&&&&&&&&&&&& Session session = sessionFactory.getCurrentSession();
&&&&&&&&&&&&&&&&&&& ta = session.beginTransaction();
&&&&&&&&&&&&&&&&&&& System.out.println("第一次根据ID=1查询记录时会再去查数据库,故打印hsql如下:");
&&&&&&&&&&&&&&&&&&& EhUserInfo vo1 = (EhUserInfo) session.get(EhUserInfo.class, 1);
&&&&&&&&&&&&&&&&&&& System.out.println("根据ID=1找到的记录:" + vo1);
&&&&&&&&&&&&&&&&&&& ta.commit();
&&&&&&&&&&&&&&& } catch (Exception e) {
&&&&&&&&&&&&&&&&&&& e.printStackTrace();
&&&&&&&&&&&&&&&&&&& ta.rollback();
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& System.out.println("session closed.");
&&&&&&&&&&&&&&& System.out.println("====================================");
&&&&&&&&&&&&&&& String[] cacheNames = CacheManager.getInstance().getCacheNames();
&&&&&&&&&&&&&&& System.out.println("缓存的key cacheNames length := "
&&&&&&&&&&&&&&&&&&&&&&& + cacheNames.length);
&&&&&&&&&&&&&&& for (String name : cacheNames) {
&&&&&&&&&&&&&&&&&&& System.out.println("name := " + name);
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& System.out.println("====================================");
&&&&&&&&&&&&&&& System.out.println("同一个session(一级缓存默认的)中,配置cache的Bean");
&&&&&&&&&&&&&&& System.out.println("session open ....");
&&&&&&&&&&&&&&& try {
&&&&&&&&&&&&&&&&&&& Session session = sessionFactory.getCurrentSession();
&&&&&&&&&&&&&&&&&&& String hsql = "select t from EhBlogTopic t ";
&&&&&&&&&&&&&&&&&&& ta = session.beginTransaction();
&&&&&&&&&&&&&&&&&&& Query query = session.createQuery(hsql);
&&&&&&&&&&&&&&&&&&& query.setCacheable(true);
&&&&&&&&&&&&&&&&&&& System.out.println("查询全部query.list().size:"
&&&&&&&&&&&&&&&&&&&&&&&&&&& + query.list().size());
&&&&&&&&&&&&&&&&&&& Cache myCache1 = CacheManager.getInstance().getCache(
&&&&&&&&&&&&&&&&&&&&&&&&&&& "michael.cache.ehcache.hibernate.EhBlogTopic");
&&&&&&&&&&&&&&&&&&& System.out.println("查询到EhBlogTopic cache size:"
&&&&&&&&&&&&&&&&&&&&&&&&&&& + myCache1.getKeys().size());
&&&&&&&&&&&&&&&&&&& myCache1 = CacheManager.getInstance().getCache(
&&&&&&&&&&&&&&&&&&&&&&&&&&& "michael.cache.ehcache.hibernate.EhBlogTopic");
&&&&&&&&&&&&&&&&&&& System.out.println("EhBlogTopic cache size:"
&&&&&&&&&&&&&&&&&&&&&&&&&&& + myCache1.getKeys().size() + ",详细记录如下:");
&&&&&&&&&&&&&&&&&&& for (Object str : myCache1.getKeys()) {
&&&&&&&&&&&&&&&&&&&&&&& System.out.println(str);
&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&& System.out
&&&&&&&&&&&&&&&&&&&&&&&&&&& .println("在同一个session,再根据ID=1查询记录时不会再去查数据库,故不会打印hsql");
&&&&&&&&&&&&&&&&&&& EhBlogTopic vo1 = (EhBlogTopic) session.get(EhBlogTopic.class,
&&&&&&&&&&&&&&&&&&&&&&&&&&& 1);
&&&&&&&&&&&&&&&&&&& System.out.println("根据ID=1找到的记录:" + vo1);
&&&&&&&&&&&&&&&&&&& EhBlogTopic vo2 = new EhBlogTopic();
&&&&&&&&&&&&&&&&&&& vo2.setId(10);
&&&&&&&&&&&&&&&&&&& vo2.setUserId("michael");
&&&&&&&&&&&&&&&&&&& vo2.setTopic("Myblog:11");
&&&&&&&&&&&&&&&&&&& vo2.setSite("");
&&&&&&&&&&&&&&&&&&& session.save(vo2);
&&&&&&&&&&&&&&&&&&& ta.commit();
&&&&&&&&&&&&&&&&&&& System.out.println("新增加一条记录ID=10");
&&&&&&&&&&&&&&& } catch (Exception e) {
&&&&&&&&&&&&&&&&&&& e.printStackTrace();
&&&&&&&&&&&&&&&&&&& ta.rollback();
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& System.out.println("session closed.");
&&&&&&&&&&&&&&& System.out.println("====================================");
&&&&&&&&&&&&&&& Cache myCache1 = CacheManager.getInstance().getCache(
&&&&&&&&&&&&&&&&&&&&&&& "michael.cache.ehcache.hibernate.EhBlogTopic");
&&&&&&&&&&&&&&& System.out.println("EhBlogTopic cache size:"
&&&&&&&&&&&&&&&&&&&&&&& + myCache1.getKeys().size() + ",详细记录如下:");
&&&&&&&&&&&&&&& for (Object str : myCache1.getKeys()) {
&&&&&&&&&&&&&&&&&&& System.out.println(str);
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& System.out.println("====================================");
&&&&&&&&&&&&&&& System.out.println("在当前同一个sessionFactory,配置cache的bean");
&&&&&&&&&&&&&&& System.out.println("session open ....");
&&&&&&&&&&&&&&& try {
&&&&&&&&&&&&&&&&&&& Session session = sessionFactory.getCurrentSession();
&&&&&&&&&&&&&&&&&&& ta = session.beginTransaction();
&&&&&&&&&&&&&&&&&&& System.out
&&&&&&&&&&&&&&&&&&&&&&&&&&& .println("不管是否第一次查询ID=1的记录,如果cache中存在的,则不会再查数据库,故不打印hsql");
&&&&&&&&&&&&&&&&&&& EhBlogTopic vo1 = (EhBlogTopic) session.get(EhBlogTopic.class,
&&&&&&&&&&&&&&&&&&&&&&&&&&& 1);
&&&&&&&&&&&&&&&&&&& System.out.println("根据ID=1找到的记录:" + vo1);
&&&&&&&&&&&&&&&&&&& System.out.println("查询之前session保存的数据ID=10,也不会再查数据库,故不打印hsql");
&&&&&&&&&&&&&&&&&&& EhBlogTopic vo2 = (EhBlogTopic) session.get(EhBlogTopic.class,
&&&&&&&&&&&&&&&&&&&&&&&&&&& 10);
&&&&&&&&&&&&&&&&&&& System.out.println("根据之前save的ID=10找到的记录:" + vo2);
&&&&&&&&&&&&&&&&&&& ta.commit();
&&&&&&&&&&&&&&& } catch (Exception e) {
&&&&&&&&&&&&&&&&&&& e.printStackTrace();
&&&&&&&&&&&&&&&&&&& ta.rollback();
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& System.out.println("session closed.");
&&&&&&&&&&& } catch (Exception e) {
&&&&&&&&&&&&&&& e.printStackTrace();
&&&&&&&&&&& } finally {
&&&&&&&&&&&&&&& if (null != sessionFactory) {
&&&&&&&&&&&&&&&&&&& sessionFactory.close();
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&&&&&&&&&& System.out.println("sessionFactory& closed.");
&&&&&&& }
&&& }
运行结果如下:
ehcache - hibernate Test ...
13:27:15 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic]; using defaults.
13:27:15 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic2]; using defaults.
13:27:15 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic3]; using defaults.
buildSessionFactory===============
====================================
session open ....
同一个session(一级缓存默认的)中,没有配置cache的Bean
Hibernate: select ehuserinfo0_.ID as ID1_, ehuserinfo0_.USER_ID as USER2_1_, ehuserinfo0_.USER_NAME as USER3_1_, ehuserinfo0_.OHTER_INFO as OHTER4_1_ from MY_TB_EH_USER_INFO ehuserinfo0_
查询全部query.list().size:2
再根据ID=1查询某记录时不会再去查数据库,故不会打印hsql
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhUserInfo@63b2e6
session closed.
session open ....
第一次根据ID=1查询某记录时,会打印hsql
Hibernate: select ehuserinfo0_.ID as ID1_0_, ehuserinfo0_.USER_ID as USER2_1_0_, ehuserinfo0_.USER_NAME as USER3_1_0_, ehuserinfo0_.OHTER_INFO as OHTER4_1_0_ from MY_TB_EH_USER_INFO ehuserinfo0_ where ehuserinfo0_.ID=?
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhUserInfo@123a389
session closed.
====================================
当前同一个sessionFactory,没有配置cache的Bean,
session open ....
第一次根据ID=1查询记录时会再去查数据库,故打印hsql如下:
Hibernate: select ehuserinfo0_.ID as ID1_0_, ehuserinfo0_.USER_ID as USER2_1_0_, ehuserinfo0_.USER_NAME as USER3_1_0_, ehuserinfo0_.OHTER_INFO as OHTER4_1_0_ from MY_TB_EH_USER_INFO ehuserinfo0_ where ehuserinfo0_.ID=?
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhUserInfo@429c19
session closed.
====================================
缓存的key cacheNames length := 7
name := sampleCache2
name := michael.cache.ehcache.hibernate.EhBlogTopic2
name := org.hibernate.cache.UpdateTimestampsCache
name := sampleCache1
name := michael.cache.ehcache.hibernate.EhBlogTopic
name := org.hibernate.cache.StandardQueryCache
name := michael.cache.ehcache.hibernate.EhBlogTopic3
====================================
同一个session(一级缓存默认的)中,配置cache的Bean
session open ....
Hibernate: select ehblogtopi0_.ID as ID2_, ehblogtopi0_.USER_ID as USER2_2_, ehblogtopi0_.TOPIC as TOPIC2_, ehblogtopi0_.SITE as SITE2_ from MY_TB_EH_BLOG_TOPIC ehblogtopi0_
查询全部query.list().size:9
查询到EhBlogTopic cache size:9
EhBlogTopic cache size:9,详细记录如下:
michael.cache.ehcache.hibernate.EhBlogTopic#6
michael.cache.ehcache.hibernate.EhBlogTopic#5
michael.cache.ehcache.hibernate.EhBlogTopic#7
michael.cache.ehcache.hibernate.EhBlogTopic#8
michael.cache.ehcache.hibernate.EhBlogTopic#2
michael.cache.ehcache.hibernate.EhBlogTopic#9
michael.cache.ehcache.hibernate.EhBlogTopic#1
michael.cache.ehcache.hibernate.EhBlogTopic#4
michael.cache.ehcache.hibernate.EhBlogTopic#3
在同一个session,再根据ID=1查询记录时不会再去查数据库,故不会打印hsql
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhBlogTopic@11a0d35
Hibernate: insert into MY_TB_EH_BLOG_TOPIC (USER_ID, TOPIC, SITE, ID) values (?, ?, ?, ?)
新增加一条记录ID=10
session closed.
====================================
EhBlogTopic cache size:10,详细记录如下:
michael.cache.ehcache.hibernate.EhBlogTopic#6
michael.cache.ehcache.hibernate.EhBlogTopic#5
michael.cache.ehcache.hibernate.EhBlogTopic#7
michael.cache.ehcache.hibernate.EhBlogTopic#8
michael.cache.ehcache.hibernate.EhBlogTopic#2
michael.cache.ehcache.hibernate.EhBlogTopic#9
michael.cache.ehcache.hibernate.EhBlogTopic#10
michael.cache.ehcache.hibernate.EhBlogTopic#1
michael.cache.ehcache.hibernate.EhBlogTopic#4
michael.cache.ehcache.hibernate.EhBlogTopic#3
====================================
在当前同一个sessionFactory,配置cache的bean
session open ....
不管是否第一次查询ID=1的记录,如果cache中存在的,则不会再查数据库,故不打印hsql
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhBlogTopic@1321f5
查询之前session保存的数据ID=10,也不会再查数据库,故不打印hsql
根据之前save的ID=10找到的记录:michael.cache.ehcache.hibernate.EhBlogTopic@1a6518
session closed.
sessionFactory closed.
我们从上面的详细运行日志中就可以看出cache的效果。
xingqinstar
浏览: 47263 次
来自: 深圳
你的人生就是精彩的,一段十二年过去了,后面更多的十二年一样会精 ...
努力,加油哦!
要继续研究哦,亲

我要回帖

更多关于 ehcache 注解使用 的文章

 

随机推荐