发现现在自己的记性很差了,写了几遍的代码,依然记不住,老是想不起来,现在记下来,就当个知识库吧,用的时候到这里来取,呵呵,图个方便。
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static SessionFactory factory;
static {
try {
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
}catch(Exception e) {
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
}
本文提供了一个简单的Hibernate工具类实现,包括SessionFactory的初始化、Session的获取与关闭等方法,旨在帮助开发者快速进行Hibernate相关的数据库操作。

被折叠的 条评论
为什么被折叠?



