创建hibernateUtil工具类

2018-09-09  本文已影响0人  Mango_lxh
package com.hibernate;

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

public class HibernateUtils {

    private static SessionFactory factory;
    
    static {
        try {
                      //创建Configuration对象,读取hibernate.cfg.xml文件,完成初始化
            Configuration cfg = new Configuration().configure();
            factory = cfg.buildSessionFactory();
        }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();
            }
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读