二级缓存的配置及实例详解
1. 拷贝oscache-2.1.jar到当前工程的lib目录下
2. 开启二级缓存
<property name="hibernate.cache.use_second_level_cache">true</property>
<property
name="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</property>
3. 指定使用二级缓存的类
方法一:在映射文件中编写代码:<class name="Customers" table="Customers">
<cache usage="read-only"/>
方法二:在配置文件中编写代码:<class-cache usage="read-write" class="cn.csdn.producets.domain.Customers"/>
<class-cache usage="read-write" class="cn.csdn.producets.domain.Orders" />
5.在src下添加一个文件oscache.xml
案例讲解:
一,从二级缓存中读取数据
@Test
public void testCache() {
// 丛二级缓存中读取数据
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Customers customer = (Customers) session.load(Customers.class, 3);
System.out.println(customer.getPass());
tx.commit();
session.close();
session = HibernateUtil.getSession();
tx = session.beginTransaction();
customer = (Customers) session.load(Customers.class, 3);
System.out.println(customer.getPass());
tx.commit();
session.close();
}
运行结果:Hibernate: select customers0_.id as id0_0_,customers0_.realName as realName0_0_, customers0_.pass as pass0_0_,customers0_.sex as sex0_0_, customers0_.petName as petName0_0_,customers0_.email as email0_0_, customers0_.rdate as rdate0_0_ from Customerscustomers0_ where customers0_.id=?
111
111
二.Iterator检索
public void testIterator(){
// iterator先到数据库中检索符合条件的id,然后根据id分别到一级缓存和二级缓存中查找对象,
// 因为第一次关闭了session,所以一级缓存里的数据已经不存在,而二级缓存中的3数据并没有消失。
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Orders o where o.id<11");
query.list().size();
tx.commit();
session.close();
session = HibernateUtil.getSession();
tx = session.beginTransaction();
query = session.createQuery("from Orders o");
Iterator<Orders> it = query.iterate();
while (it.hasNext()) {
System.out.println(it.next().getNumber());
}
tx.commit();
session.close();
}
运行结果:Hibernate: select orders0_.id as id1_, orders0_.number as number1_,orders0_.address as address1_, orders0_.phone as phone1_, orders0_.odate asodate1_, orders0_.sum as sum1_, orders0_.status as status1_, orders0_.cid ascid1_ from orders orders0_ where orders0_.id<11
Hibernate: select orders0_.idas col_0_0_ from orders orders0_
2
123
Hibernate: select orders0_.idas id1_0_, orders0_.number as number1_0_, orders0_.address as address1_0_,orders0_.phone as phone1_0_, orders0_.odate as odate1_0_, orders0_.sum assum1_0_, orders0_.status as status1_0_, orders0_.cid as cid1_0_ from ordersorders0_ where orders0_.id=?
123
三.查询缓存
public void testowerFlow(){
// 测试缓存溢出存放到临时目录
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Orders o");
query.list().size();
tx.commit();
session.close();
}
运行结果:Hibernate: select orders0_.id as id1_, orders0_.number as number1_,orders0_.address as address1_, orders0_.phone as phone1_, orders0_.odate asodate1_, orders0_.sum as sum1_, orders0_.status as status1_, orders0_.cid ascid1_ from orders orders0_
四.查询缓存
public void testQueryCache(){
// 查询缓存:查询缓存依赖二级缓存,所以必须开启二级缓存,查询缓存才起作用
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Query query = session
.createQuery("selectc.id from Customers c where c.pass='444'");
// 设置缓存,如果查询缓存存在,直接返回,如果查询缓存不存在,查询数据库,将查询结果设置到查询缓存中
query.setCacheable(true);
query.list();
System.out.println("--------");
tx.commit();
session.close();
session = HibernateUtil.getSession();
tx = session.beginTransaction();
query = session
.createQuery("selectc.id from Customers c where c.pass='444'");
query.setCacheable(true);
query.list();
System.out.println("--------");
tx.commit();
session.close();
}
运行结果:Hibernate: select customers0_.id as col_0_0_ from Customers customers0_where customers0_.pass='444'
--------
Hibernate: select customers0_.id as col_0_0_ from Customers customers0_where customers0_.pass='444'
五.
public void testUpdate(){
// 测试对对象的操作,会同步到二级缓存中。
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Customers customer = (Customers) session.load(Customers.class, 4);
System.out.println(customer.getPass());
customer.setPass("222");
tx.commit();
session.close();
session = HibernateUtil.getSession();
tx = session.beginTransaction();
customer = (Customers) session.load(Customers.class, 4);
System.out.println(customer.getPass());
tx.commit();
session.close();
}
Hibernate: selectcustomers0_.id as id0_0_, customers0_.realName as realName0_0_,customers0_.pass as pass0_0_, customers0_.sex as sex0_0_, customers0_.petNameas petName0_0_, customers0_.email as email0_0_, customers0_.rdate as rdate0_0_from Customers customers0_ where customers0_.id=?
444
Hibernate: update Customers setrealName=?, pass=?, sex=?, petName=?, email=?, rdate=? where id=?
222
六.
public void testUpdeatTim(){
// 改变值的时候同步到二级缓存中
// 当改变值得时候二级缓存会重新加载数据,以保证数据的一致性
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Customers customer = (Customers) session.load(Customers.class, 4);
System.out.println(customer.getPass());
Query query = session
.createQuery("updateCustomers c set c.pass='444' where c.id=4");
query.executeUpdate();
tx.commit();
session.close();
session = HibernateUtil.getSession();
tx = session.beginTransaction();
customer = (Customers) session.load(Customers.class, 4);
System.out.println(customer.getPass());
tx.commit();
session.close();
}
运行结果:Hibernate: select customers0_.id as id0_0_, customers0_.realName asrealName0_0_, customers0_.pass as pass0_0_, customers0_.sex as sex0_0_,customers0_.petName as petName0_0_, customers0_.email as email0_0_,customers0_.rdate as rdate0_0_ from Customers customers0_ wherecustomers0_.id=?
222
Hibernate: update Customers setpass='444' where id=4
Hibernate: selectcustomers0_.id as id0_0_, customers0_.realName as realName0_0_, customers0_.passas pass0_0_, customers0_.sex as sex0_0_, customers0_.petName as petName0_0_,customers0_.email as email0_0_, customers0_.rdate as rdate0_0_ from Customerscustomers0_ where customers0_.id=?
444