spring+hibernate2.0二级缓存配置文件:
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</prop>
<prop key="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORDBA</prop>
<prop key="hibernate.connection.username">scott</prop>
<prop key="hibernate.connection.password">tiger</prop>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.Oracle9Dialect</prop>
<!--使用hibernate支持的ehcache缓存配置-->
<prop key="hibernate.cache.provider_class">net.sf.hibernate.cache.EhCacheProvider</prop>
<!--使用sql查询缓存配置-->
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>cn/com/zhongxin/entity/ElecAdvancevalueinfo.hbm.xml</value>
</list>
</property>
</bean>
ehcache.xml配置文档,默认位于src目录下,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="400"
timeToLiveSeconds="12000"
overflowToDisk="true"/>
</ehcache>
在代码中使用sql缓存
q = session.createQuery(hql);
q.setCacheable(true);//激活缓存
在每个映射文档.xml里配置
<cache usage="read-write"/>
在collection里配置
如在set里配置
<cache usage="read-only"/>