⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hibernatehelper.java

📁 struts的一些基本开发
💻 JAVA
字号:
package cn.com.csuinfosoft.hibernate;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

public class HibernateHelper {
	
	private static SessionFactory sessionFactory = null;
	
	
	private static ThreadLocal threadLocal = null;
	static {
		try {
			Configuration config = new Configuration().configure();
			sessionFactory = config.buildSessionFactory();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		threadLocal = new ThreadLocal();
	}
	
	private HibernateHelper() {
	}
	
	/**
	 * 获取Session对象
	 * @return
	 */
	public  static Session getSessionObject() {
		Session session = (Session) threadLocal.get();
		if(session == null) {
			try {
				session = sessionFactory.openSession();
			} catch (HibernateException e) {
				e.printStackTrace();
			}
			threadLocal.set(session);
		}
		return session;
	}
	
	/**
	 * 关闭Session
	 *
	 */
	public static void closeSession() {
		Session session = (Session) threadLocal.get();
		if(session != null) {
			try {
				session.close();
			} catch (HibernateException e) {
				e.printStackTrace();
			}
			threadLocal.set(null);
		}
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -