一、查询缓存配置在hibernate.cfg.xml中加入查询缓存的策略, <propertyname="hibernate.cache.use_query_cache">true</propert
一、查询缓存配置
在hibernate.cfg.xml中加入查询缓存的策略, <propertyname="hibernate.cache.use_query_cache">true</property>
启用查询缓存的策略,默认是false。
二、关闭二级缓存,采用query.list()查询普通属性
代码如下所示。
public voidtestCache1() { Session session = null; try { session= HibernateUtils.getSession(); session.beginTransaction(); Listnames = session.createQuery("select s.name from Student s") .setCacheable(true) .list(); for (int i=0;i<names.size(); i++) { Stringname = (String)names.get(i); System.out.println(name); } System.out.println("-------------------------------------------------------"); //不会发出查询语句,因为启用查询缓存 names= session.createQuery("select s.name from Student s") .setCacheable(true) .list(); for (int i=0;i<names.size(); i++) { Stringname = (String)names.get(i); System.out.println(name); } session.getTransaction().commit(); }catch(Exceptione) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } }
--结束END--
本文标题: hibernate查询缓存详细分析
本文链接: https://www.lsjlt.com/news/223610.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-08
2024-05-08
2024-05-08
2024-05-08
2024-05-08
2024-05-08
2024-05-08
2024-05-08
2024-05-08
2024-05-08
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0