hibernate的工具类的封装

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {
	private static SessionFactory sf;
	
	static{
		//1.加载配置
		Configuration cfg = new Configuration().configure();
		
		//2.创建一个sessionFactory
		sf = cfg.buildSessionFactory();
		
		//3.在虚拟机关闭时,释放SessionFactory
		Runtime.getRuntime().addShutdownHook(new Thread(){
			@Override
			public void run() {
				sf.close();
				System.out.println("释放资源");
			}
		});
		
	}
	
	public static Session openSession(){
		return sf.openSession();
	}
	
	public static Session getCurrentSession(){
		return sf.getCurrentSession();
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_41298572/article/details/88185179