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