package com.sun.java.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* @author Dell
* hibernate使用xml配置文件时的工具类
*/
public class HibernateCfgUtils {
private static SessionFactory factory;
// 静态代码块
static {
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
}
// 获取SessionFactory的方法
public static SessionFactory getSessionFactory() {
return factory;
}
// 获取session的方法
public static Session getSession() {
return factory.openSession();
}
// 私有化构造方法
private HibernateCfgUtils() {
}
}
本文介绍了一个使用XML配置的Hibernate工具类实现,该工具类提供SessionFactory和Session的获取方法,适用于需要通过XML进行配置的Hibernate项目。
1116

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



