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

2016-02-19 09:40 62 1 收藏

人生本是一个不断学习的过程,在这个过程中,图老师就是你们的好帮手,下面分享的SSH整合中 hibernate托管给Spring得到SessionFactory懂设计的网友们快点来了解吧!

【 tulaoshi.com - Web开发 】

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();

来源:https://www.tulaoshi.com/n/20160219/1591676.html

延伸阅读
如果曾经使用Java开发过Web应用程序,你一定会对Java applets比较熟悉,这是一种基本上在浏览器中运行的小型程序。当浏览器加载包含有Java applets标志的HTML代码时,Java applets就会执行,网页中的Windows Forms控件的运行方式与Java applets相似。我们可以使用由Windows Forms技术提供的丰富的类开发Windows Forms控件,然后在网页中部...
摘要:     最近在使用Liferay开发一个门户网站的过程中碰到默认的在线文章编辑器无法满足用户需求的问题。Liferay默认的文章编辑器功能比较简单,且可扩展性较差。经过再三权衡,决定采用tinyMCE作为Liferay的默认在线文章编辑器。但是,官方下载的tinyMCE的advimage插件不具有图片上传的功能,需要对该插件进行扩展。经过反...
Hibernate的文档中写道:集合filter是一种特殊的查询,用于一个持久化集合或者数组。查询字符串可以引用this,意为当前的数组元素 我觉得这样理解起来有些费劲.其实他的作用就是把你不需要的数据过滤掉,然后把结果集返回给你.现在举个例子说明一下: String hql = "select p from Picgroup p join p.images t where p.id="...
一、什么是Spring? Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架 二、如何在程序中获取Spring配置的bean呢? 方法一:在初始化时保存ApplicationContext对象 代码: 代码如下: ApplicationContext ac = new FileSystemXmlApplicationContex("applicationContext.xml");     ac.getBean("beanId"); ...
问题的提出: 假定我们的Html 页中有一些表单需要处理,并且我们需要初始化数据库中的字段,我们该怎么办?标准的解决办法就是使用CGI脚本或是使用Java Servlet等服务器端程序,但是你有没有想过,还可以编写一个脚本程序使你可以用javascript直接调用服务器端Java程序进行任何计算的结果,就像下面代码中列的那样: <html...

经验教程

698

收藏

16
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部