package com.it.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Hibernate工具类
*/
public class HibernateUtil {
private static final Configuration config = new Configuration().configure();
private static final SessionFactory factory = config.buildSessionFactory();
//本地线程对象
private static ThreadLocal<Session> local = new ThreadLocal<Session>();
/*
* 获取session
*/
public static Session getSession(){
Session session = local.get();
if(session == null){
session = factory.openSession();
local.set(session);
}
return session;
}
/*
* 关闭
*/
public static void closeSession(){
Session session = local.get();
if(session != null && session.isOpen()){
session.close();
session = null;
}
local.set(null);
}
}