【封装】HibernateUtils.java

package com.exp.hibernate.utils;

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

public class HibernateUtils {

    private static SessionFactory factory;
    static{
        //1.获取核心 配置文件对象
        Configuration cfg = new Configuration().configure();

        //2.创建会话工厂
        factory = cfg.buildSessionFactory();

        //监听程序关闭
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run() {
                System.out.println("程序关闭...");
                //关闭会话工厂
                factory.close();
            }
        });

    }

    public static Session openSession(){
        return factory.openSession();
    }

    public static Session getCurrentSession(){
        return factory.getCurrentSession();
    }

}

 

posted @ 2019-04-14 13:50  expworld  阅读(152)  评论(0)    收藏  举报