[color=brown][size=large] [size=xx-large]import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateTools {
static SessionFactory sf = null;
static Session session = null;
public SessionFactory getSF() {
if (sf == null) {
Configuration cfg = new Configuration()
.configure("hibernate-cfg.xml");
sf = cfg.buildSessionFactory();
}
return sf;
}
public Session getSession() {
if (session == null) {
session = getSF().openSession();
}
return session;
}
public void close() {
if (session != null) {
session.close();
}
if (sf != null) {
sf.close();
}
}[size=xx-large]
}[/size][/color]
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateTools {
static SessionFactory sf = null;
static Session session = null;
public SessionFactory getSF() {
if (sf == null) {
Configuration cfg = new Configuration()
.configure("hibernate-cfg.xml");
sf = cfg.buildSessionFactory();
}
return sf;
}
public Session getSession() {
if (session == null) {
session = getSF().openSession();
}
return session;
}
public void close() {
if (session != null) {
session.close();
}
if (sf != null) {
sf.close();
}
}[size=xx-large]
}[/size][/color]
本文介绍了一个简单的Hibernate工具类实现,该工具类用于管理SessionFactory和Session的创建与关闭。通过这个工具类,开发者可以方便地初始化SessionFactory并获取Session,从而简化Hibernate的使用流程。
501

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



