org.phoenix.basic.utils.HibernateUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of phoenix_db Show documentation
Show all versions of phoenix_db Show documentation
对hibernate4的封装。封装了Druid,通过Druid可以轻量级的对其他数据库进行操作
package org.phoenix.basic.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtil {
private final static SessionFactory FACTORY = buildSessionFactory();
//new File("c:\\phoenix\\hibernate.cfg.xml")
private static SessionFactory buildSessionFactory() {
Configuration cfg = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory factory = cfg.buildSessionFactory(serviceRegistry);
return factory;
}
public static SessionFactory getSessionFactory() {
return FACTORY;
}
public static Session openSession() {
return FACTORY.openSession();
}
public static void closeSession(Session session) {
if(session!=null)session.close();
}
}