sessionFaction工具类

工具类
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {

private static SessionFactory sessionFactory = null;

static {

try {
Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
} catch (HibernateException e) {
e.printStackTrace();
}

}

public static SessionFactory getSessionFactory(){
return sessionFactory;
}

public static Session getSession() {
return sessionFactory.openSession();
}

public static void closeSession(Session session){
if(session.isOpen()){
session.close();
}
}
}

你可能感兴趣的:(java)