Hibernate 二级缓存 使用

来源:互联网 发布:淘宝客用手机可以做吗 编辑:程序博客网 时间:2024/05/17 21:47

 

// 使用  缓存

 

User 使用缓存

 

// hib.cfg.xml 缓存

 

启动查询缓存

//  缓存配置文件

 

 ehcache.xml

 

 

 

 

 

// 再查询时候,  虽然第一次 Session 关闭了 ,但是 开启了 二级缓存: SessionFactory 级别的。

 

c.setCacheable(true);

再 第二次执行

l=c.list();

的时候  将不会执行hql

其执行结果如下

----------

 

Hibernate: select this_.id as id0_0_, this_.username as username0_0_, this_.password as password0_0_ from sys_user this_ where this_.username=?
-1-NyiiMzA7O5mT
-2-NyiiMzA7O5mT

 

=========================================

 

如果 不开启 二次缓存

c.setCacheable(false);  // 默认

执行结果则如下:

--------------------------------------------

Hibernate: select this_.id as id0_0_, this_.username as username0_0_, this_.password as password0_0_ from sys_user this_ where this_.username=?
-1-NyiiMzA7O5mT

Hibernate: select this_.id as id0_0_, this_.username as username0_0_, this_.password as password0_0_ from sys_user this_ where this_.username=?
-2-NyiiMzA7O5mT