HqlDemoApp.java
package cn.itcast.h3.query.hql;

import java.io.Serializable;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction; import cn.itcast.h3.query.hql.vo.StudentModel;
import cn.itcast.h3.query.hql.vo.TeacherModel;
import cn.itcast.util.HibernateUtil; public class HqlDemoApp {
//测试是否存在二级缓存
void testHasCache(){
Session s = HibernateUtil.getSession();
//读取数据,并且将数据加载入当前Session的一级缓存,
//同时加载到当前SessionFactory的二级缓存
TeacherModel tm1 = (TeacherModel) s.get(TeacherModel.class, 3L);
System.out.println(tm1);
//读取的是当前Session的一级缓存数据
TeacherModel tm2 = (TeacherModel) s.get(TeacherModel.class, 3L);
System.out.println(tm2);
s.close();
System.out.println("-----------------------------");
Session s2 = HibernateUtil.getSession();
//读取的是二级缓存的数据
TeacherModel tm3 = (TeacherModel) s2.get(TeacherModel.class, 3L);
System.out.println(tm3);
TeacherModel tm5 = (TeacherModel) s2.get(TeacherModel.class, 3L);
System.out.println(tm5);
s2.close();
System.out.println("--------------------------");
Session s3 = HibernateUtil.getSession();
//读取的是二级缓存的数据
TeacherModel tm4 = (TeacherModel) s3.get(TeacherModel.class, 3L);
System.out.println(tm4);
s3.close(); }
//测试SQL查询数据是否加载二级缓存
void testQueryToCache(){
Session s = HibernateUtil.getSession();
Query q = s.createQuery("from TeacherModel");
q.list();
Query q2 = s.createQuery("from TeacherModel");
q2.list();
s.close();
System.out.println("--------------------");
Session s3 = HibernateUtil.getSession();
TeacherModel tm4 = (TeacherModel) s3.get(TeacherModel.class, 3L);
System.out.println(tm4);
s3.close();
}
//测试SQL查询是否读取二级缓存数据
void testQueryToCache2(){
Session s3 = HibernateUtil.getSession();
TeacherModel tm4 = (TeacherModel) s3.get(TeacherModel.class, 3L);
System.out.println(tm4);
s3.close();
System.out.println("--------------------");
Session s = HibernateUtil.getSession();
Query q = s.createQuery("from TeacherModel where uuid = :uuid");
q.setLong("uuid", 3L);
TeacherModel tm = (TeacherModel) q.uniqueResult();
System.out.println(tm);
s.close(); }
//测试保存数据是否影响二级缓存数据
void testSaveToCache(){
//保存数据
Session s3 = HibernateUtil.getSession();
Transaction t = s3.beginTransaction();
TeacherModel tm = new TeacherModel();
tm.setTeacherName("aaa");
tm.setNick("bbb"); Serializable uuid = s3.save(tm); t.commit();
s3.close(); System.out.println("--------------------"); Session s = HibernateUtil.getSession();
TeacherModel tm4 = (TeacherModel) s.get(TeacherModel.class, uuid);
System.out.println(tm4);
s.close(); }
//测试删除数据是否影响二级缓存数据
void testDeleteToCache(){
//保存数据
Session s3 = HibernateUtil.getSession();
Transaction t = s3.beginTransaction();
TeacherModel tm = (TeacherModel) s3.get(TeacherModel.class, 6L); s3.delete(tm); t.commit();
s3.close(); System.out.println("--------------------"); Session s = HibernateUtil.getSession();
TeacherModel tm4 = (TeacherModel) s.get(TeacherModel.class, 6L);
System.out.println(tm4);
s.close(); }
//测试更新数据是否影响二级缓存数据
void testUpdateToCache(){
//保存数据
Session s3 = HibernateUtil.getSession();
Transaction t = s3.beginTransaction();
TeacherModel tm = (TeacherModel) s3.get(TeacherModel.class, 4L);
tm.setTeacherName("hahahh");
s3.update(tm);
t.commit();
s3.close(); System.out.println("--------------------"); Session s = HibernateUtil.getSession();
TeacherModel tm4 = (TeacherModel) s.get(TeacherModel.class, 4L);
System.out.println(tm4);
s.close(); }
//测试集合操作二级缓存
void testCollectionToCache(){
Session s = HibernateUtil.getSession();
TeacherModel tm = (TeacherModel) s.get(TeacherModel.class, 1L);
System.out.println(tm.getStudents());
s.close();
System.out.println("----------------");
Session s2 = HibernateUtil.getSession();
TeacherModel tm2 = (TeacherModel) s2.get(TeacherModel.class, 1L);
System.out.println(tm2.getStudents());
s2.close();
}
//DML影响二级缓存的操作
void testDMLToCache(){
Session s = HibernateUtil.getSession();
Transaction t = s.beginTransaction();
StudentModel tm = (StudentModel) s.get(StudentModel.class, 3L);
System.out.println(tm);
Query q = s.createQuery("update TeacherModel set teacherName=:teacherName where uuid = :uuid");
q.setString("teacherName", "测试更新1");
q.setLong("uuid", 4L);
q.executeUpdate();
t.commit();
s.close(); Session s2 = HibernateUtil.getSession();
StudentModel tm2 = (StudentModel) s2.get(StudentModel.class, 3L);
System.out.println(tm2);
s2.close();
}
//查询缓存
void testQueryToCache3(){
Session s2 = HibernateUtil.getSession();
Query q2 = s2.createQuery("from TeacherModel where uuid=:uuid");
//本次查询开启查询缓存
q2.setCacheable(true);
System.out.println(q2.list());
s2.close();
System.out.println("-------------");
Session s = HibernateUtil.getSession();
Query q = s.createQuery("from TeacherModel where uuid = 2");
q.setCacheable(true);
System.out.println(q.list());
s.close();
}
public static void main(String[] args) {
new HqlDemoApp().testQueryToCache3();
}
}

ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<diskStore path="d:\cache-data"/>
<cache
name="c1"
maxElementsInMemory="10"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="1200"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<defaultCache
maxElementsInMemory="10"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="1200"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>

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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/h3</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property> <!--方言的配置,用于区别使用何种数据库-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!--可选配置项中,开发过程常用设置-->
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<!-- 开启二级缓存功能 -->
<property name="cache.use_second_level_cache">true</property>
<!-- 设置二级缓存供应商 -->
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <!-- 开启查询缓存 -->
<property name="cache.use_query_cache">true</property> <!-- 开启线程绑定Session -->
<property name="current_session_context_class">thread</property>
<!--资源文件注册-->
<mapping resource="cn/itcast/h3/query/hql/vo/StudentModel.hbm.xml"/>
<mapping resource="cn/itcast/h3/query/hql/vo/TeacherModel.hbm.xml"/> <!-- 二级缓存的配置(使用) -->
<!-- 类级缓存的配置 -->
<class-cache
region="c1"
usage="read-write"
class="cn.itcast.h3.query.hql.vo.TeacherModel"
/>
<class-cache
region="c1"
usage="read-write"
class="cn.itcast.h3.query.hql.vo.StudentModel"
/>
<!-- 集合缓存的配置 -->
<collection-cache
region="c1"
usage="read-write"
collection="cn.itcast.h3.query.hql.vo.TeacherModel.students"
/> </session-factory>
</hibernate-configuration>

hibernate框架学习之二级缓存(测试用例)的更多相关文章

  1. hibernate框架学习之二级缓存

    缓存的意义 l应用程序中使用的数据均保存在永久性存储介质之上,当应用程序需要使用数据时,从永久介质上进行获取.缓存是介于应用程序与永久性存储介质之间的一块数据存储区域.利用缓存,应用程序可以将使用的数 ...

  2. hibernate框架学习之一级缓存

    l缓存是存储数据的临时空间,减少从数据库中查询数据的次数 lHibernate中提供有两种缓存机制 •一级缓存(Hibernate自身携带) •二级缓存(使用外部技术) lHibernate的一级缓存 ...

  3. Hibernate框架(四)缓存策略+lazy

    Hibernate作为和数据库数据打交道的框架,自然会设计到操作数据的效率问题,而对于一些频繁操作的数据,缓存策略就是提高其性能一种重要手段,而Hibernate框架是支持缓存的,而且支持一级和二级两 ...

  4. Hibernate的查询,二级缓存,连接池

    Hibernate的查询,二级缓存,连接池 1.Hibernate查询数据 Hibernate中的查询方法有5中: 1.1.Get/Load主键查询 使用get或者load方法来查询,两者之间的区别在 ...

  5. Hibernate框架学习笔记

      Hibernate 是一个 JDO( Java Data Objects)工具.它的工作原理是通过文件把值对象(Java对象)和 数据库表之间建立起一个映射关系,还提供数据查询和获取数据的方法. ...

  6. hibernate框架学习第六天:QBC、分页查询、投影、数据加载策略、二级缓存

    QBC查询 1.简单查询 Criteria c = s.createCriteria(TeacherModel.class); 2.获取查询结果 多条:list 单挑:uniqueResult 3.分 ...

  7. Hibernate学习之二级缓存

    © 版权声明:本文为博主原创文章,转载请注明出处 二级缓存 - 二级缓存又称“全局缓存”.“应用级缓存” - 二级缓存中的数据可适用范围是当前应用的所有会话 - 二级缓存是可插拔式缓存,默认是EHCa ...

  8. hibernate框架学习第三天:对象状态、一级缓存、快照等

    对象的状态 瞬时状态: 瞬时对象(TO) 应用程序创建出来的对象,不受H3控制 注意:TO对象不具有OID,一旦为TO赋值OID,那么此时就不是TO 持久化状态:持久化对象(PO) 受H3控制的对象, ...

  9. [ SSH框架 ] Hibernate框架学习之二

    一.Hibernate持久化类的编写规范 1.什么是持久化类 Hibernate是持久层的ORM影射框架,专注于数据的持久化工作.所谓持久化,就是将内存中的数据永久存储到关系型数据库中.那么知道了什么 ...

随机推荐

  1. C#设计模式(9)——代理模式

    1.代理模式介绍 在软件开发中有时会遇到不能直接使用对象的问题,如我们要使用的对象在进程外,甚至在远程的机器上,但是我们要使用这个对象的功能怎么办呢?代理模式就可以用来解决这个问题.举一个生活中的例子 ...

  2. Sqlserver中的触发器

    一 什么是触发器 1.1  触发器的概念   触发器(trigger)是SQL server来保证数据完整性的一种方法,它是与表事件相关的特殊的存储过程,它的执行是由事件来触发,当对一个表进行操作(  ...

  3. ASP.NET MVC+EF框架+EasyUI实现权限管理(附源码)

    前言:时间很快,已经快到春节的时间了,这段时间由于生病,博客基本没更新,所以今天写一下我们做的一个项目吧,是对权限的基本操作的操作,代码也就不怎么说了,直接上传源码和图片展示,下面我们直接进入主题介绍 ...

  4. ssm+maven+pageHelper搭建maven项目实现快速分页

    ssm+maven+pageHelper搭建maven项目实现快速分页 PageHelper分页使用: 插件的环境引入: 1.pom文件中引入分页插件的资源位置: <dependency> ...

  5. centos下问题:connect:network is unreachable

    问题描述 弄了三台机器准备搭建一个集群,按照centos7系统,一台主节点安装桌面环境,两台计算节点.配置计算节点的时候,发现ping不通,出现connect:network is unreachab ...

  6. 自学python 6.

    内容:id() is == 编码 解码1.好声音选秀比赛评委在打分的时候可以进行输入. 假设有10个评委.让10个评委进行打分, 要求, 分数必须大于5分, 小于10分.count = 1while ...

  7. C# 使用 log4net 记录日志

    Ø  前言 在一般的开发应用中,都会涉及到日志记录,用于排查错误 或 记录程序运行时的日志信息.log4net 库是 Apache log4j 框架在 Microsoft .NET 平台的实现,是一个 ...

  8. [译]使用NuGet管理共享代码

    原文 可以在内网部署自己的私人NuGet仓储服务. Setting it up 本例中我们创建一个发邮件的类,将其作为我们自己的NuGet包: using System; using System.N ...

  9. [C++]PAT乙级1001.害死人不偿命的(3n+1)猜想(15/15)

    /* 1001.害死人不偿命的(3n+1)猜想 (15) 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下 ...

  10. lua与C交互 具体

    什么样类型的函数可以被Lua调用 typedef int (*lua_CFunction) (lua_State *L); 符合类型的函数怎样处理后才可以被Lua调用 使用lua_register或者 ...