实体类 :

  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;
}
}

工具类:

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 }

测试类:

 package cn.happy.test;

 import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import cn.happy.entity.Emp;
import cn.happy.util.HibernateUtil; public class STest {
Transaction tx;
Session session;
Transaction tx2;
Session session2;
@Test
public void testBulk(){
session = HibernateUtil.getSession();
tx=session.beginTransaction();
Emp emp = (Emp)session.get(Emp.class, 1);
System.out.println(emp);
tx.commit();
HibernateUtil.closeSession();
System.out.println("===================");
session2 = HibernateUtil.getSession();
tx2=session2.beginTransaction();
Emp emp2 = (Emp)session2.get(Emp.class, 1);
System.out.println(emp2);
tx2.commit();
HibernateUtil.closeSession();
}
}

小配置:

 <?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="cn.happy.entity">
<class name="Emp" table="Emp">
<cache usage="read-write"/>
<id name="empNo" type="int" column="EMPNO">
<generator class="native">
</generator>
</id>
<property name="empName" type="string" column="EMPNAME"/>
</class>
</hibernate-mapping>

大配置:

 <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">zc</property>
<property name="connection.password">zc</property>
<!-- 输出所有 SQL 语句到控制台。 -->
<property name="hibernate.show_sql">true</property>
<!-- 配置Hibernate.cfg.xml开启二级缓存。 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- 配置二级缓存的供应商 -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <!-- 在 log 和 console 中打印出更漂亮的 SQL。 -->
<property name="hibernate.format_sql">true</property>
<!-- 自动生成数据表,update/create -->
<property name="hbm2ddl.auto">update</property>
<!-- 方言 -->
<property name="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect</property>
<!-- 关联小配置 --> <mapping resource="cn/happy/entity/Emp.hbm.xml"/>
<class-cache usage="read-write" class="cn.happy.entity.Emp"/>
</session-factory>
</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;}
  }

Hibernate中"二级缓存"配置的更多相关文章

  1. Hibernate中二级缓存指的是什么?

    一.一级缓存.二级缓存的概念解释 (1)一级缓存就是Session级别的缓存,一个Session做了一个查询操作,它会把这个操作的结果放在一级缓存中,如果短时间内这个 session(一定要同一个se ...

  2. 【Hibernate】解析hibernate中的缓存

    Hibernate中的缓存一共有三种,一级缓存.二级缓存.查询缓存.缓存除了使用Hibernate自带的缓存,还可以使用redis进行缓存,或是MongoDB进行缓存. 所使用的Demo: User. ...

  3. 配置Hibernate的二级缓存

    1.在applicationContex.xml文件里面添加二级缓存配置: <!-- 配置hibernate的sessionFactory --> <bean id="se ...

  4. Hibernate二级缓存简述及基于Spring4,Hibernate5,Ehcache3的二级缓存配置

    Hibernate L2缓存 缓存的分类 L2缓存工作原理 放入二级缓存的数据 Ehcache 依赖 ehcache.xml 常用的memoryStoreEvictionPolicy(缓存算法) eh ...

  5. SSM-MyBatis-18:Mybatis中二级缓存和第三方Ehcache配置

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 二级缓存 Mybatis中,默认二级缓存是开启的.可以关闭. 一级缓存开启的.可以被卸载吗?不可以的.一级缓存 ...

  6. Hibernate 二级缓存配置

    详见:https://www.cnblogs.com/Junsept/p/7324981.html Hibernate的cache管理: Cache就是缓存,它往往是提高系统性能的最重要手段,对数据起 ...

  7. Hibernate中一级缓存和二级缓存

    缓存是介于应用程序和物理数据源之间,其作用是为了降低应用程序对物理数据源访问的频次,从而提高了应用的运行性能.缓存内的数据是对物理数据源中的数据的复制,应用程序在运行时从缓存读写数据,在特定的时刻或事 ...

  8. Hibernate之二级缓存

                                                            Hibernate之二级缓存 一.简介 Gaving King曾经对别人说,hibern ...

  9. hibernate(九) 二级缓存和事务级别详讲

    序言 这算是hibernate的最后一篇文章了,下一系列会讲解Struts2的东西,然后说完Struts2,在到Spring,然后在写一个SSH如何整合的案例.之后就会在去讲SSM,在之后我自己的个人 ...

随机推荐

  1. day6-面向对象

    Python 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前没有接触过 ...

  2. Creating an AVI in memory with C++

    Creating an AVI in memory with C++        The following example demonstrates how an avi file is comp ...

  3. 2017浙江省赛 A - Cooking Competition ZOJ - 3958

    地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3958 题目: "Miss Kobayashi's Drag ...

  4. Codeforces Round #408 (Div. 2) D - Police Stations

    地址:http://codeforces.com/contest/796/problem/D 题目: D. Police Stations time limit per test 2 seconds ...

  5. 利用URLConnection来发送POST和GET请求

    URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接.程序可以通过URLConnection实例向该URL发送请求.读取U ...

  6. JVM基本配置与调优

    JVM基本配置与调优 JVM调优,一般都是针对堆内存配置调优. 如图:堆内存分新生代和老年代,新生代又划分为eden区.from区.to区. 一.区域释义 JVM内存模型,堆内存代划分为新生代和老年代 ...

  7. git删除本地分支和删除远程分支

    引言: 切换分支的时候命令打错了,git checkout 后面没有跟分支名,结果git status,很多delete的文件,直接冒冷汗,git add ,commit 之后发现本地与远程确实是删除 ...

  8. maven-surefire-plugin

    本文参考自:https://www.cnblogs.com/qyf404/p/5013694.html surefire是maven里执行测试用例(包括testNG,Junit,pojo)的插件,他能 ...

  9. 20145329 《JAVA程序设计》实验三总结

    实验日期:2016.4.12 实验时间:15:30~17:30 实验序号:实验三 实验名称: 敏捷开发与XP实践 实验目的与要求: XP基础 XP核心实践 相关工具 实验内容 1.使用git托管代码 ...

  10. HTML中的figure和gigcaption标签

    参考自:anti-time的博客http://www.cnblogs.com/morning0529/p/4198494.html 在写xhtml.html中常常用到一种图片列表,图片+标题或者图片+ ...