您好,登录后才能下订单哦!
目的:减少对数据库的访问次数!从而提升hibernate的执行效率!
一级缓存、二级缓存
1)Hibenate中一级缓存,也叫做session的缓存,它可以在session范围内减少数据库的访问次数! 只在session范围有效! Session关闭,一级缓存失效!
2)当调用session的save/saveOrUpdate/get/load/list/iterator方法的时候,都会把对象放入session的缓存中。
3)Session的缓存由hibernate维护, 用户不能操作缓存内容; 如果想操作缓存内容,必须通过hibernate提供的evit/clear方法操作。
只在(当前)session范围有效,作用时间短,效果不是特别明显!
在短时间内多次操作数据库,效果比较明显!
session.flush(); 让一级缓存与数据库同步
session.evict(object); 清空一级缓存中指定的对象
session.clear(); 清空一级缓存中缓存的所有对象
在什么情况用上面方法?
批量操作使用使用:
Session.flush(); // 先与数据库同步
Session.clear(); // 再清空一级缓存内容
面试题1: 不同的session是否会共享缓存数据?
不会。
面试题2: list与iterator查询的区别?
list():一次把所有的记录都查询出来,会放入缓存,但不会从缓存中获取数据。
Iterator():进行N+1次查询; N表示所有的记录总数。即会先发送一条语句查询所有记录的主键(1次),再根据每一个主键再去数据库查询(N次)!会放入缓存,也会从缓存中取数据!
User.java
package com.rk.hibernate.i_status; public class User { private int userId; private String username; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public String toString() { return "User [userId=" + userId + ", username=" + username + "]"; } }
User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.rk.hibernate.i_status" auto-import="true"> <class name="User" table="T_Users"> <id name="userId" column="id"> <generator class="native"></generator> </id> <property name="username" column="name" type="string"></property> </class> </hibernate-mapping>
App2_Cache.java
package com.rk.hibernate.i_status; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.Test; public class App2_Cache { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(User.class) .buildSessionFactory(); } @Test public void testGet() { Session session = sf.openSession(); session.beginTransaction(); // 查询 User u1 = (User) session.get(User.class, 2);// 先检查缓存中是否有数据,如果有,则不查询数据库,直接从缓存中获取 User u2 = (User) session.get(User.class, 2); System.out.println(u1); System.out.println(u2); System.out.println(u1 == u2); // true session.getTransaction().commit(); session.close(); } @Test public void testFlush() { Session session = sf.openSession(); session.beginTransaction(); User u = (User)session.get(User.class, 2); u.setUsername("ABC"); // 缓存数据与数据库同步 session.flush(); //如果没有这一语句,则执行一次update;加上这条语句后,执行两次update。 u.setUsername("DEF"); session.getTransaction().commit();// session.flush(); session.close(); } @Test public void testClear() { Session session = sf.openSession(); session.beginTransaction(); // 查询 User u1 = (User)session.get(User.class, 2); // 清空缓存内容 // session.clear();// 清空所有 session.evict(u1);// 清除指定 User u2 = (User)session.get(User.class, 2); session.getTransaction().commit(); session.close(); } @Test public void testSessionScope() { Session session1 = sf.openSession(); session1.beginTransaction(); Session session2 = sf.openSession(); session2.beginTransaction(); // u1放入session1的缓存区 User u1 = (User) session1.get(User.class, 2); // u2放入session2的缓存区 User u2 = (User) session2.get(User.class, 2); session1.getTransaction().commit(); session1.close(); session2.getTransaction().commit(); session2.close(); } }
App3_List_Iterator.java
package com.rk.hibernate.i_status; import java.util.Iterator; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.Test; public class App3_List_Iterator { private static SessionFactory sf; static { sf = new Configuration() .configure() .addClass(User.class) .buildSessionFactory(); } /** * list与iterator区别 * 1. list 方法 * 2. iterator 方法 * 3. 缓存 * @throws Exception */ //1. list 方法 @Test public void list() { Session session = sf.openSession(); session.beginTransaction(); // HQL查询 Query q = session.createQuery("from User "); // list()方法 List<User> list = q.list(); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } session.getTransaction().commit(); session.close(); } //2. iterator 方法 @Test public void iterator() throws Exception { Session session = sf.openSession(); session.beginTransaction(); // HQL查询 Query q = session.createQuery("from User "); // iterator()方法 Iterator<User> it = q.iterate(); while (it.hasNext()) { // 得到当前迭代的每一个对象 User user = it.next(); System.out.println(user); } session.getTransaction().commit(); session.close(); } //3. 测试List缓存 @Test public void testList() { Session session = sf.openSession(); session.beginTransaction(); Query q = session.createQuery("from User"); List<User> list1 = q.list(); if(list1 != null && list1.size()>0) { for(User u : list1) { System.out.println(u); } } System.out.println("------------------------------------"); List<User> list2 = q.list(); if(list2 != null && list2.size()>0) { for(User u : list2) { System.out.println(u); } } System.out.println("list1 == list2 ? " + (list1 == list2)); // false for(int i=0;i<list1.size();i++) { User u1 = list1.get(i); User u2 = list2.get(i); System.out.println(u1 == u2); //true } session.getTransaction().commit(); session.close(); } @Test public void testIterator() { Session session = sf.openSession(); session.beginTransaction(); Query q = session.createQuery("from User"); Iterator<User> it = q.iterate(); while(it.hasNext()) { User u = it.next(); System.out.println(u); } System.out.println("------------------------------------"); it = q.iterate(); while(it.hasNext()) { User u = it.next(); System.out.println(u); } session.getTransaction().commit(); session.close(); } @Test public void testListIterator() { Session session = sf.openSession(); session.beginTransaction(); Query q = session.createQuery("from User"); List<User> list = q.list(); for(User u : list) { System.out.println(u); } System.out.println("--------------------------------------------"); Iterator<User> it = q.iterate(); while(it.hasNext()) { User u = it.next(); System.out.println(u); } session.getTransaction().commit(); session.close(); } @Test public void testGet() { Session session = sf.openSession(); session.beginTransaction(); Query q = session.createQuery("from User "); List<User> list = q.list(); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } Query q2 = session.createQuery("from User where userId<5"); Iterator<User> it = q2.iterate(); while (it.hasNext()) { // 得到当前迭代的每一个对象 User user = it.next(); System.out.println(user); } session.getTransaction().commit(); session.close(); } public void temp() { Session session = sf.openSession(); session.beginTransaction(); session.getTransaction().commit(); session.close(); } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。