需要兩個包:
commons-logging-1.0.4.jar
ehcache-1.2.3.jar

在src根目錄下新建 ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
 
<diskStore path="java.io.tmpdir" />
 
<defaultCache maxElementsInMemory="10000" eternal="false"
  timeToIdleSeconds
="120" timeToLiveSeconds="120" overflowToDisk="true"
  diskExpiryThreadIntervalSeconds
="120" memoryStoreEvictionPolicy="LRU" />
 
<cache name="cn.yu.vo.Guestbook" maxElementsInMemory="1000" eternal="false"
  timeToIdleSeconds
="3000" timeToLiveSeconds="600" overflowToDisk="true"/>
</ehcache>
  <!-- 
          diskStore:保存在硬盤上的臨時目錄
          
          name:Cache的唯一標識

          maxElementsInMemory:內存中最大緩存對象數。

          maxElementsOnDisk:磁盤中最大緩存對象數,若是0表示無窮大。

          eternal:Element是否永久有效,一但設置了,timeout將不起作用。

          overflowToDisk:配置此屬性,當內存中Element數量達到maxElementsInMemory時,Ehcache將會Element寫到磁盤中。

          timeToIdleSeconds:設置Element在失效前的允許閑置時間。僅當element不是永久有效時使用,可選屬性,默認值是0, 
                                              也就是可閑置時間無窮大。

          timeToLiveSeconds:設置Element在失效前允許存活時間。最大時間介于創建時間和失效時間之間。僅當element不是永久
                                              有效時使用,默認是0.,  也就是element存活時間無窮大。

          diskPersistent:是否緩存虛擬機重啟期數據。(這個虛擬機是指什么虛擬機一直沒看明白是什么,有高人還希望能指點
                                       一二)。

          diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒。

          diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每個Cache都應該有自己的一
                                                     個緩沖區。

          memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存。默認策略是
                                                           LRU(最近最少使用)。你可以設置為FIFO(先進先出)或是LFU(較少使用)。這里比較
                                                           遺憾,Ehcache并沒有提供一個用戶定制策略的接口,僅僅支持三種指定策略,感覺做的不夠
                                                           理想。
 
-->

在hibernate.cfg.xml里面新增屬性
    <property name="cache.provider_class">
        org.hibernate.cache.EhCacheProvider
    
</property>
在guestbook.hbm.xml里面新增屬性
    <class name="cn.yu.vo.Guestbook" table="GUESTBOOK" schema="OLIVER">
    
<cache usage="read-write"/>
        
<id name="id" type="java.lang.Long">
            
<column name="ID" precision="22" scale="0" />
            
<generator class="sequence">
             
<param name="sequence">gb_seq</param>
             
</generator>
        
</id>