hibernatehelper.java
来自「struts的一些基本开发」· Java 代码 · 共 61 行
JAVA
61 行
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 + =
减小字号Ctrl + -
显示快捷键?