hibernate核心类和接口--openSession 和getCurrentSession的思想
[size=medium]
final public class HibernateUtil { //SqlHelper [size=large]private static SessionFactory sessionFactory=null;//使用线程局部模式private static ThreadLocal<Session> threadLocal=new ThreadLocal<Session>();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;}[/size]