📄 sessionutil.java
字号:
/*
* 创建日期 2005-12-4
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package com.zhurun.hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
/**
* @author 朱闰
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class SessionUtil {
private static final SessionFactory sessionFactory;
public static final ThreadLocal session = new ThreadLocal();
static
{
try
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch(HibernateException ex)
{
ex.printStackTrace();
throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(),ex);
}
}
public static Session currentSession() throws HibernateException
{
Session s = (Session)session.get();
// Open a new Session, if this Thread has none yet
if(s == null)
{
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException
{
Session s = (Session)session.get();
session.set(null);
if(s != null)
{
s.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -