1:开启二级缓存sessionFactory需要安装jar包

2:在实体类配置文件添加(配置二级缓存)。我的配置文件是Account.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- name属性指定类名(全限定名) table指明表名,不指明table数据默认的表名和实体名一致 -->
<class name="com.entity.Account" table="acc_tab">
<!-- 配置二级缓存 重要点 -->
<cache usage="read-only" region="com.entity.Account"/>
<!-- 配置二级缓存  重要点 -->
<!-- type指明当前字段的类型 name对应实体中的属性名 -->
<id type="integer" name="id">
<!-- 提供ID自增的策略 native会根据数据库自行判断 -->
<generator class="native"/>
</id>
<property name="name" type="string"></property>
<property name="age" type="integer"></property>
</class>
</hibernate-mapping>

3:启用二级缓存 和启动二级缓存 ehcache插件。hibernate的核心配置文件hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!--配置mysql数据库连接参数-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property> <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">10</property>
<!-- 重点配制 -->
<!-- 启用二级缓存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- 启动二级缓存 ehcache插件 -->
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<!-- 打开查询缓存 -->
<property name="hibernate.cache.use_query_cache">true</property>
<!-- 重点配制 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping resource="com/entity/Account.hbm.xml"/>
</session-factory>
</hibernate-configuration>

4:再配置缓存文件。ehcache.xml

对应的ehcache.xml内容文件如下

<ehcache>
<!-- 它的作用是在指定位置建数据表,当超过"maxElementsInMemory"设置的值时开始写数据 ,"maxElementsInMemory"是下面默认配置-->
<diskStore path="/F:/"/> <defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/> <cache name="com.entity.Account"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/> <!-- 设置默认的查询缓存区域的属性 -->
<cache name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="50"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="7200"
overflowToDisk="true"/> <!-- 设置时间戳缓存区域的属性 -->
<cache name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="5000"
eternal="true"
overflowToDisk="true"/> <!-- 设置自定义命名查询缓存区域的属性,一般可不配置 -->
<cache name="myCacheRegion"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"/>
</ehcache>

以上hibernate可是使用缓存

5:启用查询缓存,用的比较少,适合在查询固定语句固定值时用

最新文章

  1. 浩瀚PDA无线POS盘点机(安装盘点程序):盘点结果实时上传到PC电脑端
  2. ubuntu 在mac 的 Parallels 的分辨率问题
  3. Dynamics AX 2012 R2 IIS WebSite Unauthorized 401
  4. NGUI之scroll view制作,以及踩的坑总结
  5. 淘宝:OceanBase分布式系统负载均衡案例分享
  6. c语言学习之基础知识点介绍(二十):预处理指令
  7. 在InteliJ IDEA中写Dart及配置IDEA - Dart Plugin
  8. sql server 2012提示评估期已过的解决办法 附序列号
  9. groovy学习(一)列表
  10. 18 UI美化transition 图片过渡
  11. Auto Layout - BNR
  12. [hadoop] hadoop native libraries 编译
  13. bzoj2301
  14. Mysql 和 Postgresql(PGSQL) 对比
  15. Linux内存管理大图(第三稿)
  16. Leetcode 647. Palindromic Substrings
  17. Oracle 与 SqlServer 的区别浅析总结
  18. linux 批量创建用户获取8位随机密码
  19. 从Objective-C到Swift,你必须会的(二)组合options
  20. LoadRunner的Capture Level说明

热门文章

  1. 3D赛瓦号——整装待发!
  2. 利用CMD 創建新文件的機種方法
  3. Re0:DP学习之路 数塔 HDU - 2084(基础递推)
  4. TestNG套件测试(一)
  5. Django ContentType内置组件
  6. 将网络图片转换为base64
  7. 【Codeforces 1038D】Slime
  8. HDU - 3407 - String-Matching Automata
  9. [bzoj1577][Usaco2009 Feb]庙会捷运Fair Shuttle_贪心_线段树
  10. Codeforces Round #391(div 1+2)