今天打算把原来做的一个项目中的代码做一下优化和重构。
首先,从hibernate的查询做起。原来有一个查询在页面是多个页面都调用这个查询。而且查询条件是基本一样的。所以个人感觉应当应用hibernate的二级缓存和查询缓存。
步骤如下:
1:配置spring文件
在sessionfactiory的bean中加上如下配置
<prop key="hibernate.show_sql">true</prop> 打开二级缓存 <prop key="hibernate.cache.use_second_level_cache">true</prop> 指定二级缓存的外部程序我用的是ECACHE <propkey="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> 打开hibernate的查询缓存 <prop key="hibernate.cache.use_query_cache">true</prop>
2配置要使用缓存的实体
<hibernate-mapping> <class name="com.tjsinfo.tjsoa.mail.vo.TjsEmailFolder" table="TjsEmailFolder" schema="tjsoadba" catalog="TJSOA"> 关键是这里指定缓存的策略一般用读写就可以了,如果数据从不变化可以用只读 <cache usage="read-write"></cache> <id name="folderId" type="java.lang.Integer"> <column name="Folder_id" /> <generator class="native" /> </id> <many-to-one name="tjsUser" class="com.tjsinfo.tjsoa.system.TjsUser.TjsUser" fetch="select"> <column name="Folder_userid" not-null="true" /> </many-to-one> <property name="folderName" type="java.lang.String"> <column name="Folder_name" length="100" not-null="true" /> </property> <set name="tjsFoldMails" inverse="true"> <key> <column name="tjsmail_flod_id" not-null="true" /> </key> <one-to-many class="com.tjsinfo.tjsoa.mail.vo.TjsFoldMail" /> </set> </class> </hibernate-mapping>
3定义ehcache.xml文件
<ehcache> <!-- Sets the path to the directory where cache .data files are created. If the path is a Java System Property it is replaced by its value in the running VM. The following properties are translated: user.home - User's home directory user.dir - User's current working directory java.io.tmpdir - Default temp file path --> <diskStore path="java.io.tmpdir"/> <!--Default Cache configuration. These will applied to caches programmatically created through the CacheManager. The following attributes are required for defaultCache: maxInMemory - Sets the maximum number of objects that will be created in memory eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used if the element is not eternal. Idle time is now - last accessed time timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used if the element is not eternal. TTL is now - creation time overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit. Predefined caches. Add your cache configuration settings here. If you do not have a configuration for your cache a WARNING will be issued when the CacheManager starts The following attributes are required for defaultCache: name - Sets the name of the cache. This is used to identify the cache. It must be unique. maxInMemory - Sets the maximum number of objects that will be created in memory eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used if the element is not eternal. Idle time is now - last accessed time timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used if the element is not eternal. TTL is now - creation time overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit. Sample cache named sampleCache1 This cache contains a maximum in memory of 10000 elements, and will expire an element if it is idle for more than 5 minutes and lives for more than 10 minutes. If there are more than 10000 elements it will overflow to the disk cache, which in this configuration will go to wherever java.io.tmp is defined on your system. On a standard Linux system this will be /tmp" <cache name="sampleCache1" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" /> <cache name="sampleCache2" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" /> Sample cache named sampleCache2 This cache contains 1000 elements. Elements will always be held in memory. They are not expired. --> <!-- Place configuration for your caches following --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" /> </ehcache>
4执行相应的HQL语句,应当是第一次执行有SQL而第二次没有可是我试了一下还是有SQL说明查询缓存没用。我晕。
后来发现应当在调用查询的方法中的spring 的hibernate模板设置打开查询缓存。
public List selectHql(String hql) {
// TODO Auto-generated method stub
getHibernateTemplate().setCacheQueries(true);
return getHibernateTemplate().find(hql);
}
这样就OK啦。
实践是检验一切真理的唯一标准。