HibernateUtil 获取session工具类

package com.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
/*用于创建SessionFactory的工具类,官方写法*/
public class HibernateUtil {
	
	private static final SessionFactory sessionFactory=bulidSessionFactory();
	
	private static SessionFactory bulidSessionFactory(){
		try {
			Configuration cfg=new Configuration();
			cfg.configure();
			return  cfg.buildSessionFactory(
					new StandardServiceRegistryBuilder().
					applySettings(cfg.getProperties()).build() );
			
		} catch (Throwable ex) {
			throw new ExceptionInInitializerError(ex);
		}
	}
	
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}

}

猜你喜欢

转载自947218223.iteye.com/blog/2187907