Hibernate框架之查詢快取
阿新 • • 發佈:2019-02-02
一級快取跟二級快取都只能快取實體物件, 而查詢快取可以快取基本屬性, 同時查詢快取也可以快取實體物件, 但是隻能快取其id
查詢快取的生命週期是: 一旦資料庫表的資料發生修改, 則查詢快取則被清掉了
直接使用Hibernate框架提供的查詢快取即可
第一步: 開啟查詢快取總開關
"hibernate.cfg.xml"配置檔案:
<!-- 手動開啟查詢快取
-->
<property name="hibernate.cache.use_query_cache">true</property>
第二步: 在測試類中具體開啟查詢快取的開關:
測試程式碼:
package com.rl.hiber.test; import java.util.Iterator; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.junit.Test; import com.rl.hiber.model.User; import com.rl.hiber.utils.HibernateUtils; public class TestHibernate6 { @Test public void query1(){ Session session = HibernateUtils.getSession(); try { Query query = session.createQuery("select u.uname from User u where u.userId = 1"); //開啟查詢快取 query.setCacheable(true); //發出sql String name = (String)query.uniqueResult(); System.out.println(name); } finally{ HibernateUtils.closeSession(session); } Session session2 = HibernateUtils.getSession(); try { Query query = session2.createQuery("select u.uname from User u where u.userId = 1"); //開啟查詢快取 query.setCacheable(true); //不發出sql String name = (String)query.uniqueResult(); System.out.println(name); } finally{ HibernateUtils.closeResource(session2); } } /** * 查詢快取支援list方法 */ @Test public void query2(){ Session session = HibernateUtils.getSession(); try { Query query = session.createQuery("select u.uname from User u"); //開啟查詢快取 query.setCacheable(true); //發出sql List<String> nameList = query.list(); for(String name: nameList){ System.out.println(name); } } finally{ HibernateUtils.closeSession(session); } Session session2 = HibernateUtils.getSession(); try { Query query = session2.createQuery("select u.uname from User u"); //開啟查詢快取 query.setCacheable(true); //不發出sql List<String> nameList = query.list(); for(String name: nameList){ System.out.println(name); } } finally{ HibernateUtils.closeResource(session2); } } /** * 查詢快取不支援iterate方法 */ @Test public void query3(){ Session session = HibernateUtils.getSession(); try { Query query = session.createQuery("select u.uname from User u"); //開啟查詢快取 query.setCacheable(true); //發出sql Iterator<String> iterator = query.iterate(); while(iterator.hasNext()){ String name = iterator.next(); System.out.println(name); } } finally{ HibernateUtils.closeSession(session); } Session session2 = HibernateUtils.getSession(); try { Query query = session2.createQuery("select u.uname from User u"); //開啟查詢快取 query.setCacheable(true); //發出sql Iterator<String> iterator = query.iterate(); while(iterator.hasNext()){ String name = iterator.next(); System.out.println(name); } } finally{ HibernateUtils.closeResource(session2); } } /** * 嚴格來講查詢快取並不支援快取實體物件 * 能夠快取物件的id */ @Test public void query4(){ Session session = HibernateUtils.getSession(); try { Query query = session.createQuery("from User"); //開啟查詢快取 query.setCacheable(true); //發出一條sql查詢全部 List<User> userList = query.list(); for(User user: userList){ System.out.println(user); } } finally{ HibernateUtils.closeSession(session); } Session session2 = HibernateUtils.getSession(); try { Query query = session2.createQuery("from User"); //開啟查詢快取 query.setCacheable(true); //根據快取進來的id查詢具體每一條資料 List<User> userList = query.list(); for(User user: userList){ System.out.println(user); } } finally{ HibernateUtils.closeResource(session2); } } /** * 結合二級快取的iterate方法 * 利用iterate在二級快取中查詢物件的全部id的特點 * 此時結合查詢快取則能起到從快取中讀取資料的功能 */ @Test public void query5(){ Session session = HibernateUtils.getSession(); try { Query query = session.createQuery("select u from User u"); //開啟查詢快取 query.setCacheable(true); //發出sql Iterator<User> iterator = query.iterate(); while(iterator.hasNext()){ //發出根據id來查詢的sql User user = iterator.next(); System.out.println(user); } } finally{ HibernateUtils.closeSession(session); } Session session2 = HibernateUtils.getSession(); try { Query query = session2.createQuery("select u from User u"); //開啟查詢快取 query.setCacheable(true); //發出sql查詢出所有的id Iterator<User> iterator = query.iterate(); while(iterator.hasNext()){ //不發出sql User user = iterator.next(); System.out.println(user); } } finally{ HibernateUtils.closeResource(session2); } } }