TA的每日心情 | 衰 2021-2-2 11:21 |
|---|
签到天数: 36 天 [LV.5]常住居民I
|
applicationContext.xml:" d3 O6 t) w, L! u( A
- <prop key="hibernate.cache.use_second_level_cache">true</prop> <!--设置缓存机制为二级缓存 -->! I3 x( p3 J" `
- <prop key="hibernate.cache.use_query_cache">true</prop> <!--启动查询缓存 -->
& q% \$ ~" L; ^& Z0 I - <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> <!--设置二级缓存的Provider类 -->% {. b9 W# L9 H. @2 |- e4 x
- <prop key="hibernate.cache.provider_configuration_file_resource_path">WEB-INF/classes/ehcache.xml</prop> <!--设置缓存的配置文件路径 -->
复制代码 + U7 J/ f* T. W# z! @% ]/ Z3 y$ d
将ehcache.xml文件放到src下面,并配置ehcache.xml文件如下:% X U9 F( S- L) q& S
- <ehcache>! D2 P" A6 h8 m4 |
- <diskStore path="D:\cache" />3 d' L; Q" o) U0 O; o+ v# f
- 5 u. } E9 Q* B! \ l# g
- <defaultCache maxElementsInMemory="10000" eternal="false"
$ U7 g! X8 ~0 n+ R+ J9 V# J4 E - timeToIdleSeconds="3600" timeToLiveSeconds="3600"$ ?' t7 D' G3 Y7 W* f8 n$ m2 P
- overflowToDisk="true" diskPersistent="false"
4 W- d- T, ]5 P8 E) ~2 B5 _1 V% o - diskExpiryThreadIntervalSeconds="120"
4 M% S! O+ ~5 @- o4 A - memoryStoreEvictionPolicy="LRU" />, ^ D/ F" Y! S. G9 ?) {' I
- <cache name="net.52itstyle.entity.DataAttr"
* G% `& R8 v' `. V p& D O - maxElementsInMemory="800" eternal="false" overflowToDisk="false"
2 U$ q8 Z' a; d, u: s - timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
6 }) G, w! B& w+ U+ k! o - </ehcache>
复制代码 在设置了缓存机制的类的xml文件中添加一段代码<cache usage="read-write" region="net.52itstyle.entity.SmProduct/>"代码如下:
+ S! {) Z% E; _& o' J% o- J: f3 c- <hibernate-mapping>- N" l$ N* s# }! x+ y6 `& H2 o8 U4 V
- <!--SM_PRODUCT表的hibernate映射描述文件 -->1 e/ I1 S7 l$ P+ F. _2 w. j5 }
- <class name="net.nk.entity.SmProduct" table="SM_PRODUCT" >
1 u5 W1 G+ l$ {/ O. F1 ~ - <cache usage="read-write" region="net.nk.entity.SmProduct"/>
5 J; k+ {1 E8 w1 r/ n - <!-- ID -->+ g% G( V5 K0 [ l [$ c
- <id name="id" type="string">1 L8 i2 j# Q4 R" @: B5 B
- <column name="ID" />
3 h4 s% P- J% r' V5 m! f - <generator class="assigned" />
6 g- n4 v. O, Q - </id>
复制代码 S5 R$ q5 K8 Y7 S# g
最后可以 通过放开hibernate的show_sql来查看是否缓存机制生效了1 X6 T' Y# P/ d/ p
% ^1 s+ m( N+ q: ~( y
) n' Q) C- ~* w" ~ |
|