Configuration configuration = new Configuration();
(1)到src下面去找名稱為hibernate.cfg.xml的配置文件绢要,創(chuàng)建對象瞎嬉,把配置文件放到對象里面(加載核心配置文件)
SessionFactory(重點)
(1)使用Congfiguration對象創(chuàng)建SessionFactory對象
根據(jù)核心配置文件中數(shù)據(jù)庫配置和映射文件部分,到數(shù)據(jù)庫中根據(jù)映射關(guān)系把表創(chuàng)建
<propterty name="hibernate.hbm2ddl.auto">update</property>
(2)創(chuàng)建SessionFactory過程中能曾,特別耗資源宵溅,所以在hibernate操作中 凌简,建議在根目錄創(chuàng)建SessionFactory對象
具體實現(xiàn)1.寫工具類,寫靜態(tài)代碼塊(靜態(tài)代碼塊在類加載時執(zhí)行恃逻,且執(zhí)行一次)
包.類? public class HibernateUtils{
private Configuration cfg = null;
private SessionFactory sessionFactory = null;
static{
cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory();
}
//提供方法返回sessionFactory
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
//使用:Session Factory? sessionFactory = HibernateUtils.getSessionFactory();