Hibernate中二级缓存配置
实体类 :
1 package cn.happy.entity; 2 public class Emp { 3 private Integer empNo; 4 private String empName; 5 public Integer getEmpNo() { 6 return empNo; 7 } 8 public void setEmpNo(Integer empNo) { 9 this.empNo = empNo; 10 } 11 public String getEmpName() { 12 return empName; 13 } 14 public void setEmpName(String empName) { 15 this.empName = empName; 16 } 17 }
工具类:
1 package cn.happy.util;2 3 import org.hibernate.Session;4 import org.hibernate.SessionFactory;5 import org.hibernate.cfg.Configuration;6 7 public class HibernateUtil {8 private static final ThreadLocal sessionTL=new ThreadLocal();9 private static Configuration cf; 10 private static final SessionFactory factory; 11 static{ 12 try { 13 cf=new Configuration().configure(); 14 factory=cf.buildSessionFactory(); 15 } catch (Exception e) { 16 throw new ExceptionInInitializerError(e); 17 } 18 } 19 public static Session getSession() 20 { 21 //sessionTL的get()方法根据当前线程返回其对应的线程内部变量, 22 //也就是我们需要的Session,多线程情况下共享数据库连接是不安全的。 23 //ThreadLocal保证了每个线程都有自己的Session。 24 Session session = (Session)sessionTL.get(); 25 //如果session为null,则打开一个新的session 26 if (session==null) { 27 //创建一个数据库连接对象session 28 session=factory.openSession(); 29 //保存该数据库连接session到ThreadLocal中。 30 sessionTL.set(session); 31 32 } 33 //如果当前线程已经访问过数据库了, 34 //则从sessionTL中get()就可以获取该线程上次获取过的数据库连接对象。 35 return session; 36 } 37 /** 38 * 关闭Session 39 */ 40 public static void closeSession() 41 { 42 Session session =(Session)sessionTL.get(); 43 sessionTL.set(null); 44 session.close(); 45 } 46 47 }
测试类:
1 package cn.happy.test; 2 3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5 import org.junit.Test; 6 7 import cn.happy.entity.Emp; 8 import cn.happy.util.HibernateUtil; 9 10 public class STest { 11 Transaction tx; 12 Session session; 13 Transaction tx2; 14 Session session2; 15 @Test 16 public void testBulk(){ 17 session = HibernateUtil.getSession(); 18 tx=session.beginTransaction(); 19 Emp emp = (Emp)session.get(Emp.class, 1); 20 System.out.println(emp); 21 tx.commit(); 22 HibernateUtil.closeSession(); 23 System.out.println("==================="); 24 session2 = HibernateUtil.getSession(); 25 tx2=session2.beginTransaction(); 26 Emp emp2 = (Emp)session2.get(Emp.class, 1); 27 System.out.println(emp2); 28 tx2.commit(); 29 HibernateUtil.closeSession(); 30 } 31 }
小配置:
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="cn.happy.entity"> 6 <class name="Emp" table="Emp"> 7 <cache usage="read-write"/> 8 <id name="empNo" type="int" column="EMPNO"> 9 <generator class="native"> 10 </generator> 11 </id> 12 <property name="empName" type="string" column="EMPNAME"/> 13 </class> 14 </hibernate-mapping>
大配置:
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> 8 <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> 9 <property name="connection.username">zc</property> 10 <property name="connection.password">zc</property> 11 <!-- 输出所有 SQL 语句到控制台。 --> 12 <property name="hibernate.show_sql">true</property> 13 <!-- 配置Hibernate.cfg.xml开启二级缓存。 --> 14 <property name="hibernate.cache.use_second_level_cache">true</property> 15 <!-- 配置二级缓存的供应商 --> 16 <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> 17 18 <!-- 在 log 和 console 中打印出更漂亮的 SQL。 --> 19 <property name="hibernate.format_sql">true</property> 20 <!-- 自动生成数据表,update/create --> 21 <property name="hbm2ddl.auto">update</property> 22 <!-- 方言 --> 23 <property name="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect</property> 24 <!-- 关联小配置 --> 25 26 <mapping resource="cn/happy/entity/Emp.hbm.xml"/> 27 <class-cache usage="read-write" class="cn.happy.entity.Emp"/> 28 </session-factory> 29 </hibernate-configuration>
Jar包导入:
package cn.happy.entity;
public class Emp {
private Integer empNo;
private String empName;
public Integer getEmpNo() {return empNo;}
public void setEmpNo(Integer empNo) {this.empNo = empNo;}
public String getEmpName() {return empName;}
public void setEmpName(String empName) {this.empName = empName;}
}
转载于:https://www.cnblogs.com/Zhangmin123/p/5842945.html
Hibernate中二级缓存配置相关推荐
- hibernate Search 继续研究 增加 hibernate memcache 二级缓存 配置成功 附件maven代码(2)...
首先安装 memecached 服务端: 之前写过的 文章,centos 安装memcached服务 : http://toeo.iteye.com/blog/1240607 然后 在 前几天的 弄的 ...
- Java Hibernate 二级缓存配置及缓存的统计策略
1.首先要打开二级缓存,在hibernate.cfg.xml中添加如下配置: <propertyname="hibernate.cache.use_second_level_cache ...
- 【MyBatis笔记12】MyBatis中二级缓存相关配置内容
这篇文章,主要介绍MyBatis中二级缓存相关配置信息. 目录 一.MyBatis二级缓存 1.1.cache标签相关属性 (1)eviction属性 (2)size属性 (3)flushIntern ...
- 【SSH网上商城项目实战16】Hibernate的二级缓存处理首页的热门显示
转自:https://blog.csdn.net/eson_15/article/details/51405911 网上商城首页都有热门商品,那么这些商品的点击率是很高的,当用户点击某个热门商品后需要 ...
- hibernate教程--二级缓存详解
Hibernate的二级缓存 一.缓存概述 缓存(Cache): 计算机领域非常通用的概念.它介于应用程序和永久性数据存储源(如硬盘上的文件或者数据库)之间,其作用是降低应用程序直接读写永久性数据存储 ...
- redis作为hibernate的二级缓存
hibernate的二级缓存有好多,像ehcache.不过项目的缓存使用的是redis,而redis官方没有实现hibernate的二级缓存接口,只得自己实现.看看公司的高手如何做的吧. 先看配置: ...
- hibernate的二级缓存
缓存(Cache): 计算机领域非常通用的概念.它介于应用程序和永久性数据存储源(如硬盘上的文件或者数据库)之间,其作用是降低应用程序直接读写永久性数据存储源的频率,从而提高应用的运行性能.缓存中的数 ...
- 不要依赖hibernate的二级缓存
一.hibernate的二级缓存 如果开启了二级缓存,hibernate在执行任何一次查询的之后,都会把得到的结果集放到缓存中,缓存结构可以看作是一个hash table,key是数据库记录的 ...
- 使用Redis在Hibernate中进行缓存
Hibernate是Java编程语言的开放源代码,对象/关系映射框架.Hibernate的目标是帮助开发人员摆脱许多繁琐的手动数据处理任务.Hibernate能够在Java类和数据库表之间以及Java ...
最新文章
- 单目摄像头检测6D姿态
- TCL with SNPS sizeof_collectionget_object_namefindget_libslist_attributes
- 对NumPy中dot()函数的理解(亲测,矩阵算法)
- MYSQL之sql优化——慢查询日志
- 漏磁用MATLAB,管道漏磁内检测数据可视化技术研究
- 利用协议代理实现导航控制器UINavigationController视图之间的正向传值和反向传值...
- SpringMvc项目中使用GoogleKaptcha 生成验证码
- ftp 553 Could not create file
- csp认证多少分通过_一级结构工程师考试难不难?多少分通过?
- 零值比较--BOOL,int,float,指针变量与零值比
- keras padding_GAN整体思路以及使用Keras搭建DCGAN
- 路由器信号总是无法与手机连接服务器,手机无法搜到路由器信号怎么办? | 192路由网...
- eclipse 下安装插件
- Nginx的keeplive
- 二分图最大匹配(最大流)
- 如何用计算机装手机系统,如何使用手机给电脑安装Windows10系统?
- RGB格式转换的实现
- word文档怎么批量解除锁定_word文档被锁定,怎么解开?
- FPGA学习日记(八)SDRAM的读写测试
- root下备份mysql_如何用指令行备份mysql下所有数据库