您好,登錄后才能下訂單哦!
目的:減少對數據庫的訪問次數!從而提升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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。