Hibernate學習筆記之EHCache的配置

2021-06-20 05:47:36 字數 3073 閱讀 1043

hibernate預設二級快取是不啟動的,啟動二級快取(以ehcache為例)需要以下步驟:

ehcache.jar和commons-logging.jar,如果hibernate.jar中含有ehcache就不用新增ehcache.jar,commons-logging.jar是用來實現ehcache寫日誌的。本示例使用hibernate3.2

2、配置hibernate.cfg.xml檔案

view plain

copy to clipboard

print?

true

net.sf.ehcache.hibernate.ehcacheprovider  

true

true

3、新增配置檔案--ehcache.xml,一般放在classpath或src下,也可以自定義檔名和路徑,並在hibernate.cfg.xml中通過 hibernate.cache.provider_configuration_file_resource_path引數指定。

view plain

copy to clipboard

print?

<?xml version="1.0" encoding="utf-8"?>  

eternal="false"   

timetoidleseconds="1000"   

timetoliveseconds="1000"  

overflowtodisk="false"   

memorystoreevictionpolicy="lru"/>  

其中:maxelementsinmemory=「10000」 //cache中最多允許儲存的資料物件的數量

external=「false」 //快取中物件是否為永久的,如果是,超時設定將被忽略,物件從不過期

timetoidleseconds=「1000」  //快取資料鈍化時間(設定物件在它過期之前的空閒時間)  

timetoliveseconds=「1000」  //快取資料的生存時間(設定物件在它過期之前的生存時間)

overflowtodisk=「false」 />    //記憶體不足時,是否啟用磁碟快取  

memorystoreevictionpolicy="lru" //記憶體不足時資料物件的清除策略

ehcache中快取的3種清空策略:

fifo(first in first out):先進先出

lfu( less frequently used):一直以來最少被使用的。如上面所講,快取的元素有乙個hit屬性,hit值最小的將會被清出快取。

lru(least recently used):最近最少使用的,快取的元素有乙個時間戳,當快取容量滿了,而又需要騰出地方來快取新的元素的時候,那麼現有快取元素中時間戳離當前時間最遠的元素將被清出快取。

4、配置相關實體的快取策略

view plain

copy to clipboard

print?

@entity  

@table(name="cui_user")  

@cache(usage=cacheconcurrencystrategy.read_write)//可讀可寫   

public class tuser   

public void setid(integer id)   

public string getname()   

public void setname(string name)   

}  

最後還是需要測試的。通過id快取的例子如下:

view plain

copy to clipboard

print?

public static void main(string args)  catch (interruptedexception e)   

session=hibernatesessionfactory.getsession();  

tuser user2=(tuser)session.load(tuser.class, 200);  

system.out.println("2---"+user2.getname());  

session.close();          

}  

hibernate生成的sql語句:

hibernate: select tuser0_.id as id0_0_, tuser0_.name as name0_0_ from cui_user tuser0_ where tuser0_.id=?

1---cuisea

2---cuisea

可見第二次讀取tuser物件並沒有去資料庫查詢,說明是從快取裡讀取的,ehcache配置成功。

查詢快取(必須在hibernate.cfg.xml中配置hibernate.cache.use_query_cache為true)的例子如下:

view plain

copy to clipboard

print?

public static void main(string args)   

//另外開啟乙個事務     

session=hibernatesessionfactory.getsession();  

query=session.createquery("from tuser");  

query.setcacheable(true);//必須設定   

list=query.list();  

for (tuser user : list)   

}  

hibernate生成的sql語句:

hibernate: select tuser0_.id as id0_, tuser0_.name as name0_ from cui_user tuser0_

1---tester

1---cuisea

2---tester

2---cuisea

可見,第二次查詢並沒有從資料庫查詢,而是從快取中取資料。查詢快取使用hibernate生成的sql語句和引數作為key快取起來,當執行相同的sql並使用相同引數時從緩訪問資料。

Hibernate學習筆記一之註解

1 entiy 實體類註解 2 table 對映表 name 表名 3 coulmn column name columnname 1 boolean unique default false 2 boolean nullable default true 3 boolean insertable ...

學習hibernate筆記

1.首先要配置hibernate.xml 配置檔案,然後建立每個實體類的對映檔案 x.hbm.xml。2.準備工作做好之後,在寫測試類時先要拿到configuration cfg org.cfg.configuration 用來讀配置檔案的 然後通過cfg.configur返回乙個 找到配置檔案自身...

Hibernate學習筆記

什麼是orm呢?orm是一種思想 orm關注是物件與資料庫中的列的關係 什麼是hibernate?hibernate是乙個物件關係對映框架,它將pojo與資料庫表建立對映關係,自動生成sql語句,自動執行。hibernate的核心api 一共有6個,分別為 session sessionfactor...