Simple version of Hibernate connection tool

package utils;

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

/**
 * Hibernate连接工具
 * 
 * @author WanAkiko
 *
 */
public class HibernateSplicer {

	private static SessionFactory sessionFactory;

	/**
	 * 获取Session
	 * 
	 * @return
	 */
	public static Session getSession() {
		return getSessionFactory().getCurrentSession();
	}

	/**
	 * 获取SessionFactory单例
	 * 
	 * @return
	 */
	public static SessionFactory getSessionFactory() {
		Configuration configure = new Configuration().configure("hibernate.cfg.xml");
		if (null == sessionFactory || sessionFactory.isClosed()) {
			sessionFactory = configure.buildSessionFactory();
		}
		return sessionFactory;
	}

}

Guess you like

Origin blog.csdn.net/qq_44965393/article/details/114371731