基于注解形式的hibernate二级缓存的使用(ehcache)

来源:互联网 发布:客户地图制作软件 编辑:程序博客网 时间:2024/04/29 19:38

第三方插件为ehcache,spring+hibernate

基于注解形式;

我使用的是maven管理项目,需要添加的maven依赖:

  <dependency>          <groupId>com.googlecode.ehcache-spring-annotations</groupId>          <artifactId>ehcache-spring-annotations</artifactId>          <version>1.1.2</version>          <type>jar</type>          <scope>compile</scope>      </dependency>  

如果不使用依赖,直接导入包的话除了ehcache的jar包之外应该加上:common-logging,backport-util-concurrent这两个(我使用的是依赖,直接导入jar包方式没有尝试过,是看书上这么说的)

第一步:

package com.easitech.domain;@Entity@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)@Table(name="tb_permission"    ,catalog="crebas")public class Permission  extends BaseDomain {
第二步:

<bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="packagesToScan"><list><value>com.easitech.domain</value></list></property><property name="hibernateProperties"><props><!-- 1.使用EHCache缓存实现方案 --><prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><!-- 显示sql语句 --><prop key="hibernate.show_sql">true</prop></props></property><!-- 2.配置缓存策略 --><property name="entityCacheStrategies"><props><prop key="com.easitech.domain.Permission">nonstrict-read-write,fixedRegion</prop></props></property></bean>
第三步:
在src下建立一个文件ehcache.xml;

<ehcache>
    <diskStore path="java.io.tmpdir"/>
    <defaultCache maxElementsInMemory="10000" eternal="false"
        overflowToDisk="false" timeToIdleSeconds="0" timeToLiveSeconds="0"
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120" />
    <cache name="fixedRegion" maxElementsInMemory="100"
    eternal="true" overflowToDisk="false"/>
</ehcache>

这里注意timeToIdleSeconds中间一个是I一个是l,不一样的;一个是大写的i,一个是小写的L




原创粉丝点击