Hibernate工具類

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
/**
 * 工具類:能獲得 SessionFactory對象 ,Session對象
 *
 * */
public class HiberUtil {
    private static SessionFactory factory;
      
    private HiberUtil(){
          
    }
    //靜態塊
    static
    {   //配置數據庫信息
        Configuration conf=new Configuration();
        conf.configure();
        //初始化factory對象
        factory=conf.buildSessionFactory();
    }
      
    /**
     * 獲得SessionFactory對象
     * @return factory
     * @author Administrator
     *
     */
    public static SessionFactory getSessionFactory(){
          
        return factory;
    }
      
    /**
     * 獲得Session對象
     * @return Session
     * @author Administrator
     * */
    public static Session getSession(){
          
        return factory.openSession();
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章