hibernate 一级缓存、二级缓存
一级缓存:——session一旦关掉就没有了。
使用 load和get加载对象的时候,会自动加载到缓存,读取的也会读缓存。
public void huancun(){
Session session=null;
try{
session=HibernateUtil.getSession();
Info data1=session.get(Info.class, "p003");
Info data2 =session.get(Info.class, "p003");
System.out.println(data1 == data2);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
HibernateUtil.closeSession();
}
}

生成了一条查询语句,返回的结果为true
第一次get()生成了语句,在数据库中生成了查询,第二次,hibernate会检索缓存中是否有该条数据,如果有,直接从缓存中取出该条数据,不再去数据库中查询
使用hql查询多条数据库,如果使用getResultList()默认是无法放到缓存中的。使用iterator()可以用在缓存中。
public void huancun(){
Session session=null;
try{
session=HibernateUtil.getSession();
//默认是无法放到缓存中的
List<Info> list1 = session.createQuery("from Info").getResultList();
List<Info> list2= session.createQuery("from Info").getResultList();
System.out.println(list1 == list2);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
HibernateUtil.closeSession();
}
}
生成结果如下

说明并没有进行缓存,两次查询出的对象也并不是同一个对象
使用iterator迭代器,第一次查询会生成缓存,第二次查询时会先检索缓存中是否存在需要的数据
public void huancun(){
Session session=null;
try{
session=HibernateUtil.getSession();
//iterator()迭代器
Iterator<Info> list1=session.createQuery("from Info").iterate();
while(list1.hasNext()){
System.out.println(list1.next().getName());
}
Iterator<Info> list2 = session.createQuery("from Info").iterate();
while(list2.hasNext()){
System.out.println(list2.next().getName());
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
HibernateUtil.closeSession();
}
}
生成结果如下
很明显的能看出,第一次查询时生成了sql语句,第二次没有,直接从缓存中将数据取出
Hibernate二级缓存:需要扩展外部插件。SessionFactory内的缓存。Session关了后,只要SessionFactory没有close,还可以使用缓存。
插件需要的3个jar包:

2.在hibernate.cfg.xml中配置,启动二级缓存
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/mydb?characterEncoding=GBK</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property> <!-- 配置缓存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheMessageLogger</property>
<property name="hibernate.cache.use_query_cache">true</property>
<!-- 结束 --> <mapping resource="com/maya/model/Family.hbm.xml"/>
<mapping resource="com/maya/model/Info.hbm.xml"/>
<mapping resource="com/maya/model/Nation.hbm.xml"/>
<mapping resource="com/maya/model/Title.hbm.xml"/>
<mapping resource="com/maya/model/Work.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3.把ehcache.xml配置文件复制过来,放到hibernate框架生成的实体类映射文件同一文件夹下

4.在实体类的映射文件中,配置缓存
<?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">
<!-- Generated 2017-3-11 9:29:47 by Hibernate Tools 5.2.0.CR1 -->
<hibernate-mapping>
<class name="com.maya.model.Info" table="info" catalog="mydb" optimistic-lock="version">
<!-- <cache usage="read-write"/> 这句话一定要放在class下面的最前面 -->
<cache usage="read-write"/> <id name="code" type="string">
<column name="Code" length="50" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="Name" length="50" />
</property>
<property name="sex" type="java.lang.Boolean">
<column name="Sex" />
</property>
<property name="nation" type="string">
<column name="Nation" length="50" />
</property>
<property name="birthday" type="timestamp">
<column name="Birthday" length="19" />
</property>
</class>
</hibernate-mapping>
配置完成后,如果使用load或get的时候,不需要其它操作,直接使用的二缓存,中间session关闭也没关系
hibernate 一级缓存、二级缓存的更多相关文章
- Hibernate 再接触 一级缓存 二级缓存 查询缓存
缓存 就是把本来应该放在硬盘里的东西放在内存里 将来存内存里读 一级缓存: session缓存 二级缓存: sessionFactory级别的 (适合经常访问,数据量有限,改动不大) 很多的se ...
- 说说自己对hibernate一级、二级、查询、缓存的理解。
说说自己对hibernate一级.二级.查询.缓存的理解. 2016-03-14 21:36 421人阅读 评论(0) 收藏 举报 分类: web开发(19) 版权声明:本文为博主原创文章,未经博 ...
- hibernate的获取session的两方法比较,和通过id获取对象的比较,一级缓存二级缓存
opensession与currentsession的联系与区别 在同一个线程中opensession的session是不一样的,而currentsession获取的session是一样的,这就保证了 ...
- hibernate 5的二级缓存案例讲解
hibernate 5的二级缓存案例讲解 本帖最后由 鱼丸儿 于 2018-1-20 11:44 编辑 大家好,今天来记录讲解一下磕磕绊绊的hibernate5 的二级缓存配置,一条路摸到黑 那么在这 ...
- Hibernate中 一 二级缓存及查询缓存(1)
最近趁有空学习了一下Hibernate的缓存,其包括一级缓存,二级缓存和查询缓存(有些是参照网络资源的): 一.一级缓存 一级缓存的生命周期和session的生命周期一致,当前sessioin ...
- Hibernate-ORM:16.Hibernate中的二级缓存Ehcache的配置
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客讲述Hibernate中的二级缓存的配置,作者将使用的是ehcache缓存 一,目录 1.二级缓存的具 ...
- 具体解释Hibernate中的二级缓存
1.前言 这篇博客再前几篇博客的基础上来解说一下.Hibernate中的二级缓存.二级缓存是属于SessionFactory级别的缓存机制. 第一级别的缓存是Session级别的缓存,是属于事务范围的 ...
- Mybatis一级、二级缓存
Mybatis一级.二级缓存 一级缓存 首先做一个测试,创建一个mapper配置文件和mapper接口,我这里用了最简单的查询来演示. <mapper namespace="c ...
- hibernate 查询、二级缓存、连接池
hibernate 查询.二级缓存.连接池 查询: 1) 主键查询 Dept dept = (Dept) session.get(Dept.class, 12); Dept dept = (Dep ...
- MyBatis(七):MyBatis缓存详解(一级缓存/二级缓存)
一级缓存 MyBatis一级缓存上SqlSession缓存,即在统一SqlSession中,在不执行增删改操作提交事务的前提下,对同一条数据进行多次查询时,第一次查询从数据库中查询,完成后会存入缓 ...
随机推荐
- Jenkins+GitHub+Xcode+fir搭了一个持续集成环境
enkins+GitHub+Xcode+fir搭了一个持续集成环境 字数826 阅读5699 评论44 喜欢49 原文链接 Coding Duck 今天用Jenkins+GitHub+Xcode+fi ...
- 搭建apphouse(docker镜像私服)
1.下载apphouse,地址为http://www.youruncloud.com/soft.html2.上传AppHouse_v1.0.2.tar到服务器并解压3../apphouse_insta ...
- bzoj3992【SDOI2015】序列统计
3992: [SDOI2015]序列统计 Time Limit: 30 Sec Memory Limit: 128 MB Submit: 673 Solved: 327 [Submit][Stat ...
- Lumen开发:结合Redis实现消息队列(3)
4.运行队列监听器 开启任务监听器 Lumen包含了一个Artisan命令用来运行推送到队列的新任务.你可以使用queue:listen命令运行监听器: php artisan queue:liste ...
- PopupWindowFromBottom 从底部弹出popupwindow
自定义PopupWindowFromBottom public class PopupWindowFromBottom extends PopupWindow { public PopupWindow ...
- TP框架的增删改
TP添加数据有三种方式 1. //1.使用数组添加 $n = M("nation"); $arr = array("Code"=>"n007&q ...
- 【原创】Hibernate自动生成(2)
本实战是博主初次学习Java,分析WCP源码时,学习HibernateTools部分的实战,由于初次接触,难免错误,仅供参考,希望批评指正. 开发环境: Eclipse Version: Photon ...
- nodejs windows下安装运行
node 官网下载地址http://nodejs.org/ 下载自己对应的版本 ,我下的是windows版本 node-v4.1.1-x64.msi 然后 下一步 下一步 就完成安装了,非常简单, ...
- 【python】-- 列表
Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推 列表 1.定义列表,取出列表中的值 names = [] #定义空列表 ...
- Python菜鸟之路:前端HTML基础
前面的章节中,Python的基本知识已经差不多介绍完了.本节介绍HTML相关的知识.需要着重声明的是,前端知识是非常非常重要的知识,以我实际项目经验来看,一个项目的瓶颈在设计和前端.设计就先不说了,前 ...