HibernateUtil
package com.test.bean.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static SessionFactory factory;
static {
try {
//创建Configuration对象,读取hibernate.cfg.xml文件,完成初始化
Configuration cfg = new Configuration().configure();
ServiceRegistry serviceRegistry = (ServiceRegistry) new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build();
factory = cfg.buildSessionFactory(serviceRegistry);
}catch(Exception e) {
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
}