Chinaunix首页 | 论坛 | 博客
  • 博客访问: 897997
  • 博文数量: 215
  • 博客积分: 10062
  • 博客等级: 上将
  • 技术积分: 2235
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-01 13:21
文章分类

全部博文(215)

文章存档

2012年(1)

2011年(24)

2009年(16)

2008年(91)

2007年(83)

我的朋友

分类: Java

2009-03-19 18:49:27

第一步:在ehcache.xml中增加如下代码:
  
 
     maxElementsInMemory="1000"
    eternal="false"
    timeToIdleSeconds="10000"
    timeToLiveSeconds="10000"
    overflowToDisk="true"
 />
   
       maxElementsInMemory="1000"
    eternal="false"
    timeToIdleSeconds="3600"
    timeToLiveSeconds="7200"
    overflowToDisk="true"
 />
 
 
第二步:代码测试
  

public List queryHql(Object formatName){
  String hql="from TGameSerise c where c.formatName like ? ";
  List l=null;
  try{
  Query query=getSession().createQuery(hql);
  query.setCacheable(true);//激活查询缓存

  query.setCacheRegion("myCacheRegion");//指定要使用的cacheRegion,可选

  query.setString(0, formatName.toString());
  l=query.list();
  }catch(Exception ex){
   ex.printStackTrace();
  }finally{
   HibernateSessionFactory.closeSession();
  }
  return l;
 }

 public static void main(String[] args){
  
  List l0=this.queryHql("you");
  System.out.println(l0.size());
  List l=this.queryHql("jad");
  System.out.println(l.size());
  List l1=this.queryHql("zip");
  System.out.println(l1.size());
  List l2=this.queryHql("jad");
  System.out.println(l1.size());
  List l3=this.queryHql("jad");
  System.out.println(l3.size());
}

下面测试结果

 

Hibernate: select tgameseris0_.id as id0_, tgameseris0_.game_id as game2_0_, tgameseris0_.file_size as file3_0_, tgameseris0_.brand_id as brand4_0_, tgameseris0_.serise_id as serise5_0_, tgameseris0_.download_url as download6_0_, tgameseris0_.format_name as format7_0_, tgameseris0_.create_date as create8_0_, tgameseris0_.update_date as update9_0_, tgameseris0_.status as status0_ from cn_games_test.t_game_serise tgameseris0_ where tgameseris0_.format_name like ?
0
Hibernate: select tgameseris0_.id as id0_, tgameseris0_.game_id as game2_0_, tgameseris0_.file_size as file3_0_, tgameseris0_.brand_id as brand4_0_, tgameseris0_.serise_id as serise5_0_, tgameseris0_.download_url as download6_0_, tgameseris0_.format_name as format7_0_, tgameseris0_.create_date as create8_0_, tgameseris0_.update_date as update9_0_, tgameseris0_.status as status0_ from cn_games_test.t_game_serise tgameseris0_ where tgameseris0_.format_name like ?
7
Hibernate: select tgameseris0_.id as id0_, tgameseris0_.game_id as game2_0_, tgameseris0_.file_size as file3_0_, tgameseris0_.brand_id as brand4_0_, tgameseris0_.serise_id as serise5_0_, tgameseris0_.download_url as download6_0_, tgameseris0_.format_name as format7_0_, tgameseris0_.create_date as create8_0_, tgameseris0_.update_date as update9_0_, tgameseris0_.status as status0_ from cn_games_test.t_game_serise tgameseris0_ where tgameseris0_.format_name like ?
6
6
7

阅读(740) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~