📄 hibernateutil.java
字号:
package com;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import java.util.Properties;
public class HibernateUtil
{
public static final SessionFactory sessionFactory;
static
{
try
{
//从hibernate.cfg.xml创建SessionFactory
Configuration cfg = new Configuration();
sessionFactory = cfg.configure().buildSessionFactory();
}
catch (Throwable ex)
{
System.err.println("开始创建SessionFactory失败." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException
{
Session s = (Session) session.get();
if (s == null)
{
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException
{
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -