国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

SSH整合中 hibernate托管給Spring得到SessionFactory

2020-07-27 21:41:13
字體:
來源:轉載
供稿:網友
<prop key="hibernate.current_session_context_class">thread</prop>
然后
Resource resource=new ClassPathResource("/WEB-INF/applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(resource);
SessionFactory sessionFactory = (SessionFactory)factory.getBean("sessionFactory");
就可以得到了
剩下的 不會就回爐吧,我 的 做法是 修改HibernateUtil文件的得到SessionFactory 的方法就 什么都解決了
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
//在hibernate托管給Spring時得到sessionFactory
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
Resource resource=new ClassPathResource("/WEB-INF/applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(resource);
sessionFactory = (SessionFactory)factory.getBean("sessionFactory");
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: "
+ ex.getMessage(), ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
//
當hibernate沒有托管給Spring使用這種和傳統方式都可以得到啊
sessionFactory = new Configuration().configure("/WEB-INF/hibernate.cfg.xml")
.buildSessionFactory();
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 张北县| 宜阳县| 彭泽县| 临武县| 贵州省| 固镇县| 铜川市| 拜泉县| 施甸县| 茌平县| 永德县| 新平| 江油市| 防城港市| 白山市| 辽中县| 桑植县| 清原| 永城市| 抚宁县| 揭西县| 砀山县| 恭城| 新野县| 九江市| 英吉沙县| 类乌齐县| 河北区| 鄄城县| 高要市| 冕宁县| 射阳县| 青河县| 霍林郭勒市| 千阳县| 绩溪县| 铁岭市| 阿城市| 石嘴山市| 海淀区| 徐水县|