hibernateutil.java
来自「网上购物系统用SSH实现的」· Java 代码 · 共 57 行
JAVA
57 行
package cn.com.tarena.ecport.common.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* <pre>
* Hibernate的工具类
* 通过此类可以方便地使用Hibernate来管理持久层
* </pre>
*
* @author zhouyu 2008-1-16
*/
public class HibernateUtil {
/**
* Hibernate的sessionFactory
* 通过sessionFactory可以创建Hibernate的session
*/
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
/**
* 返回Hibernate的sessionFactory
* @return sessionFactory
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* <pre>
* 获得“当前”Hibernate的Session
* “当前”的含义可根据Hibernate配置文件中的
* hibernate.current_session_context_class来
* 配置,详细请参考Hibernate文档
* </pre>
* @return “当前”Hibernate的Session
*/
public static Session getCurrentSession(){
return HibernateUtil.getSessionFactory().getCurrentSession();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?