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中,在不执行增删改操作提交事务的前提下,对同一条数据进行多次查询时,第一次查询从数据库中查询,完成后会存入缓 ...
随机推荐
- 如何理解OOP?
OOP (Object Oriented Programming)面向对象编程 1.它符合我们现在思考习惯 2.它让一些复杂的事情变得更加简单 3.它让操作者比那成了指挥者
- Android自定义action与permission!!! (转)
原文地址:http://blog.csdn.net/android_tutor/article/details/6310418#reply 大家好,今天给大家简单分享一下Android中自定义acti ...
- MS SQL 分类汇总参数 grouping(**)=1 rollup cubt
转:http://www.111cn.net/database/mssqlserver/43368.htm 本文章介绍了关于sql多级分类汇总实现方法及数据结构,有碰到问题的同学可参考一下. 据库结构 ...
- python之开篇---hello world!
(1)前沿 (2)python 简介 (3)python hello world 实现 (4) -------------qq:1327706646 ------------------------- ...
- Web存储使用详解(本地存储、会话存储)
Web存储使用详解(本地存储.会话存储)1,Web存储介绍HTML5的Web存储功能是让网页在用户计算机上保存一些信息.Web存储又分为两种:(1)本地存储,对应 localStorage 对象.用于 ...
- kafka的并行度与JStorm性能优化
kafka的并行度与JStorm性能优化 > Consumers Messaging traditionally has two models: queuing and publish-subs ...
- table表格用tbody新属性获取DOM元素
// alert(oTab.getElementsByTagName("tbody")[0] // .getElementsByTagName('tr')[1] // .getEl ...
- 使用asn1tools进行asn1编解码
最近在做3GPP的编解码,发现有两个第三方库比较好用.一个是ASN1C(c语言编译环境),一个是python第三方库asn1tools.这里介绍下asn1tools的使用方法: 1 第一步:生成asn ...
- iOS category 类别 和 extension 扩展
category 类别 又称为 分类 在ios项目开发中允许使用类别为现有的类添加新的方法,并不需要创建子类.通过类别我们可以动态地为现有的类添加新的方法,可以将类的定义模块化地布局到多个相关文件中 ...
- 让你快速上手Runtime(转)
前言 本篇主要介绍Runtime在开发中的一些使用场景,顺便讲解了下MJExtension的底层实现.如果喜欢我的文章,可以关注我微博:袁峥Seemygo,也可以来小码哥,了解下我们的iOS培训课程. ...