
从:
本文主要讨论Hibernate + EhCache配置辅助缓存的基本用法. 它主要分为以下两个方面:
(有关EhCache的基本介绍hibernate二级缓存注解,请参阅: )
[一]缓存的多种配置方法
Javabean缓存配置分为三种类型hibernate二级缓存注解,如下所述:
(1).Bean配置方法: @Cache(用法= CacheConcurrencyStrategy.READ_WRITE)
(2).hibernate.cfg.xml标记配置方法:
(3). 映射文件* .hb.xml中的标记配置方法: <缓存用法=“ />

1. classpath: ehcahce.xml配置文件如下:
Xml代码![]()
<? xmlversion =“ 1.0”编码=“ UTF-8”?> <ehcachexmlns: xsi =“ http://www.w3.org/2001/XMLSchema-instance” xsi: noNamespaceSchemaLocation =“ ehcache. xsd” updateCheck =“ true”监视=“自动检测” dynamicConfig =“ true”> 休眠. 缓存. StandardQueryCache“ maxElementsInMemory =” 5“ eternal =” false“ timeToLiveSeconds =” 120“ overflowToDisk =” true“ /> 2.hibernate配置文件: hibernate.cfg.xml Xml代码 <! DOCTYPEhibernate-configurationPUBLIC“-// Hibernate / HibernateConfigurationDTD // EN”“ http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”> 休眠. 方言. Oracle9Dialect 属性> <属性名称=“ connection.useUnicode”>正确属性> <属性名称=“连接.characterEncoding”> UTF-8 属性> <属性名称=“ connection.SetBigStringTryClob”>正确属性> < propertyname =“ connection.pool_size”> 10 property> 自动“>更新属性> <!-Hibernate3.3andhigher-> <!-<属性名=” hibernate. 缓存. 区域. factory_class“> net.sf.ehcache.hibernate.EhCacheRegionFactory property> 缓存. provider_class“> net.sf.ehcache.hibernate.SingletonEhCacheProvider 属性> <!-EnableSecond-LevelCacheandQueryCacheSettings-> EhBlogTopic2“ /> <!-映射文件-> 3. 相关的Javabean代码片段如下: (1).hibernate.cfg.xml已配置 Java代码 / *** @博客* @ authorMichael * / @实体@ Table(名称=“ MY_TB_EH_BLOG_TOPIC”)publicclassEhBlogTopicimplementsSerializable {/ *** serialVersionUID * / privatestaticfinallongserialVersionUID = -570936907944909799L; privateIntegerid; privateStringuserId ;;省略) (2).bean用注释配置缓存: EhBlogTopic2.java Java代码 / *** @博客* @ authorMichael * / @实体@ Table(名称=“ MY_TB_EH_BLOG_TOPIC2”)@缓存(用法= CacheConcurrencyStrategy.READ_WRITE)publicclassEhBlogTopic2implementsSerializable {//与EhBlogTopic //其他省略} (3). 在映射文件* .hb.xml中添加缓存标记: EhBlogTopic3.java Java代码 / *** @博客* @ authorMichael * / publicclassEhBlogTopic3implementsSerializable {//属性与EhBlogTopic相同//其他省略} tb_EhBlogTopic3.hb.xml Xml代码 <? xmlversion =“ 1.0”?> <! DOCTYPEhibernate-mappingPUBLIC“-// Hibernate / HibernateMappingDTD2.0 // ZH”“ http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”> (4). 未配置缓存的Bean: EhUserInfo.java Java代码 / *** @博客* @ authorMichael * / @实体@表(名称=“ MY_TB_EH_USER_INFO”)publicclassEhUserInfoimplementsSerializable {/ *** serialVersionUID * / privatestaticfinallongserialVersionUID = 930384253681681679239L; privateIntegerid; privateStringuserInfo; privateStringuserName; privateStringuserName; @returntheid * / @ ID @ GeneratedValue @ Column(名称=“ ID”)publicIntegergetId(){returnid;} //其他省略. . . } 4. 测试运行代码如下: Java代码 包裹迈克尔. 缓存. ehcache. 冬眠importjava. 实用程序. 清单; importmichael. 冬眠. 大串甲骨文. BigStrBlob; importnet. SF ehcache. 缓存; importnet. SF ehcache. CacheManager; importorg. 冬眠. 查询; importorg. 冬眠. 会议; importorg. 冬眠. SessionFactory; importorg. 冬眠. 交易; importorg. 冬眠. cfg. AnnotationConfiguration; importorg. 冬眠. cfg. 组态; / **** @博客* @ authorMichael * / publicclassTestEhcacheHibernate {/ *** @ paramargs * / @ SuppressWarnings(“ unchecked”)publicstaticvoidmain(String [] args){testMulitConfigMethod();} / ***测试更多方式配置缓存* / publicstaticvoidtestMulitConfigMethod(){SessionFactorysessionFactory = null;尝试{系统. 退出. println(“ ehcache-hibernateTest ...”); Configurationconfig = newAnnotationConfiguration(). 配置(“ michael / cache / ehcache / hibernate / hibernate.cfg.xml”);系统. 出来. println(“ hibernateconfigsuccessful: ” + config); sessionFactory =配置. buildSessionFactory(); Transactionta = null;尝试{Sessionsession = sessionFactory. getCurrentSession(); ta =会话. beginTransaction();} catch(Exceptione){e. printStackTrace(); ta. rollback();}字符串[] cacheNames = CacheManager. getInstance(). getCacheNames();系统. 出来. println(“ cached keycacheNameslength: =” + cacheNames. length +“详细列表如下: ”); for(Stringname: cacheNames){系统. 出来. println(“ name: =” + name);}} catch(Exceptione){e. printStackTrace();}系统. 出来. println(“ ehcache-hibernateTestend. ”);}} 结果如下: ehcache-休眠测试... hibernate配置成功: org.hibernate.cfg.AnnotationConfiguration@193c0cf![]()

![]()

![]()
![]()
![]()

![]()
![]()
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-159473-1.html
2楼的就知道瞎叫
没有股民来的水源
自己活着