Hibernate二级缓存完整配置思路

来源:互联网 发布:北大青鸟java培训费用 编辑:程序博客网 时间:2024/06/15 01:25

HIbernate包含一级缓存与二级缓存,一级缓存这里不做介绍,着重讲解二级缓存的配置:

思路:

什么是 hibernate二级缓存?
怎么配置hibernate的二级缓存?
如何使用hibernate二级缓存结合自己的项目?

hibernate配置二级缓存的具体思路:

1,  导入hibernate及其缓存的相关依赖;2,  修改spring-hibernate.xml的配置文件(hibernate.properties),包含缓存产品的驱动类;3, 创建 enache.xml文件,添加需要缓存的model;4,   方式一:  创建Person.hbm.xml文件; 方式二:annotation注解实现;5,   测试程序;

hibernate 结合项目实现的具体思路:

1,导入jar包或者添加依赖;2,修改spring-hibernate.xml;3,配置hibernate.properties;4,Src下添加ehcache.xml;5,Hibernate.hbm.xml或者entity的实体类添加注解(4个策略);6,hibernate的load(),iterate(),list()添加 setCacheAble(true);7,功能测试,可以实现;

以下是结合本项目实现的具体思路:

第一步: pom文件添加依赖,为方便管理,version处一般会集中处理,最后诸如这种形式展示:${hibernate.version}

<dependency>     <groupId>org.hibernate</groupId>     <artifactId>hibernate-ehcache</artifactId>     <version>5.2.10.Final</version></dependency>
第二步: 修改spring-hibernate.xml配置文件,

    <!-- 配置Hibernate Session工厂 -->    <b:bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><b:property name="dataSource" ref="dataSource" />        <b:property name="hibernateProperties">            <b:props>                <!-- 系统使用的数据库方言 -->                <b:prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</b:prop>                <!-- 是否打印Hibernate生成的SQL到控制台 -->                <b:prop key="hibernate.show_sql">${pom.hibernate.show_sql}</b:prop>                <!-- 是否格式化打印出来的SQL -->                <b:prop key="hibernate.format_sql">${pom.hibernate.format_sql}</b:prop>                <!-- 是否二级缓存最小化写操作   新增-->                 <b:prop key="hibernate.cache.use_minimal_puts">true</b:prop>                <!-- 是否使用hibernate的二级缓存 新增 -->                <b:prop key="hibernate.cache.use_second_level_cache">true</b:prop>                <!-- 是否使用查询缓存   新增 -->                <b:prop key="hibernate.cache.use_query_cache">true</b:prop>                <!-- 指定使用缓存产品的驱动类  新增-->                <b:prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</b:prop>                <!-- ehcache区域工厂  新增  -->                <b:prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</b:prop>            </b:props>        </b:property>

第三步: 配置hibernate.properties配置文件,本项目已经移除,主要是配合spring-hibernate.xml 新增的props使用;

第四步:添加encache.xml配置文件

<!-- defaultCache节点为缺省的缓存策略    maxElementsInMemory 内存中最大允许存在的对象数量    eternal 设置缓存中的对象是否永远不过期    overflowToDisk:当缓存对象达到1000个的时候,是否把溢出的对象存放到硬盘上    timeToIdleSeconds:指定缓存对象空闲多长时间就过期,过期的对象会被清除掉    timeToLiveSeconds:指定缓存对象总的存活时间,超过这个值就会被清除掉    diskPersistent:当你的缓存应用关闭的时候,是否需要把缓存的对象持久化到硬盘上。当jvm结束是是否持久化对象    diskExpiryThreadIntervalSeconds:指定专门用于清除过期对象的监听线程的轮询时间,也就是说后面有一个线程,它会不断扫描,扫描是否有对象过期,有对象过期,就会将其清除掉 --><!--  <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> --><ehcache><diskStore path="user.home/ehcache/" /><defaultCache maxElementsInMemory="1000" eternal="false" overflowToDisk="true" timeToIdleSeconds="120" timeToLiveSeconds="180"diskPersistent="false" diskExpiryThreadIntervalSeconds="120"MemoryStoreEvictionPolicy="LRU" /><cache name="com.cccollector.user.model.SensitiveWord" maxElementsInMemory="100000" eternal="false"      overflowToDisk="true"       timeToIdleSeconds="86400"       timeToLiveSeconds="86400"       diskPersistent="false"      MemoryStoreEvictionPolicy="LRU"/></ehcache>

第五步:Hibernate.hbm.xml或者entity的实体类添加注解(4个策略),本项目使用如下策略

//表示开启二级缓存,并使用read-only策略@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)

第五步新增 实体Bean的配置方式有三种:

 (1).bean中注解配置的方式: @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) (2).hibernate.cfg.xml中标签配置方式: <class-cache class="" usage="" /> (3).映射文件*.hb.xml中标签配置方式: <cache usage=" />

第六步:hibernate的load(),iterate(),list()添加 setCacheAble(true)

此方法使用的前提: hibernate,且使用的是JPA注解方式;

第七步: 测试


至此花费三天的成果进行展示,实现的具体的步骤你可以不用过细揣摩,最重要的,最重要的,最重要的,是整体的思路,思路,思路;

感谢我们老大给我时间让我去研究,希望你们也能够做事,思路先行!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!大笑大笑大笑生气



原创粉丝点击