?? 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 + -