package com.hsp.util;import org.hibernate.Session; import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;final public class HibernateUtil { //SqlHelper private static SessionFactory sessionFactory=null;//使用线程局部模式private static ThreadLocalthreadLocal=new ThreadLocal ();private HibernateUtil(){};static {sessionFactory=new Configuration().configure("com/hsp/config/hsp.cfg.xml").buildSessionFactory();}//获取全新的全新的sesessionpublic static Session openSession(){return sessionFactory.openSession();}//获取和线程关联的sessionpublic static Session getCurrentSession(){Session session=threadLocal.get();//判断是否得到if(session==null){session=sessionFactory.openSession();//把session对象设置到 threadLocal,相当于该session已经和线程绑定threadLocal.set(session);}return session;}}