码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate二级缓存配置

时间:2016-09-05 13:45:43      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

实体类

 1 package cn.happy.entity;
 2 
 3 public class Emp {
 4     private Integer empNo;     
 5     private String empName;
 6     public Integer getEmpNo() {
 7         return empNo;
 8     }
 9     public void setEmpNo(Integer empNo) {
10         this.empNo = empNo;
11     }
12     public String getEmpName() {
13         return empName;
14     }
15     public void setEmpName(String empName) {
16         this.empName = empName;
17     }
18     
19     
20 
21 }

工具类

 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二级缓存配置

标签:

原文地址:http://www.cnblogs.com/1And0/p/5841829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!