OpenSessionInView
来源:互联网 发布:成都java培训机构学费 编辑:程序博客网 时间:2024/05/15 14:08
Spring 为我们提供了一个叫做 OpenSessionInViewFilter 的过滤器,他是标准的 Servlet Filter 所以我们把它按照规范配置到 web.xml 中方可使用。使用中我们必须配合使用 Spring 的 HibernateDaoSupport 来进行开发,也就是说,我们的dao层的类都要继承于 HibernateDaoSupport,从中由 Spring 来控制 Hibernate 的 Session 在请求来的时候开启,走的时候关闭,保证了我们访问数据对象时的稳定性。
OpenSessionInViewFilter的主要功能是用来把一个Hibernate Session和一次完整的请求过程对应的线程相绑定。Open Session In View在request把session绑定到当前thread期间一直保持hibernate session在open状态,使session在request的整个期间都可以使用,如在View层里PO也可以lazy loading数据,如 ${ company.employees }。当View 层逻辑完成后,才会通过Filter的doFilter方法或Interceptor的postHandle方法自动关闭session。
1. 在 web.xml 中加入对应过滤器配置文件
- <!-- Spring的OpenSessionInView实现 -->
- <filter>
- <filter-name>openSessionInViewFilter</filter-name>
- <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
- </filter-class>
- <!-- singleSession默认为true,若设为false则等于没用OpenSessionInView 。所以默认可以不写-->
- <init-param>
- <param-name>singleSession</param-name>
- <param-value>true</param-value>
- </init-param>
- <!--
- 指定org.springframework.orm.hibernate3.LocalSessionFactoryBean在spring配置文件中的名称,默认值为sessionFactory。 如果LocalSessionFactoryBean在spring中的名称不是sessionFactory,该参数一定要指定,否则会出现找不到sessionFactory的例外。所以默认可以不写
- -->
- <init-param>
- <param-name>sessionFactoryBean</param-name>
- <param-value>sessionFactory</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>openSessionInViewFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
0 0
- OpenSessionInView
- OpenSessionInView
- OpenSessionInView
- OpenSessionInView
- openSessionInView
- OpenSessionInView
- OpenSessionInView
- OpenSessionInView
- OpenSessionInView
- OpenSessionInView
- opensessioninview
- OpenSessionInView
- OpenSessionInView
- OpenSessionInView感想
- OpenSessionInView模式
- OpenSessionInView详解
- openSessionInview详解
- OpenSessionInView理解
- 如何高效产生m个n范围内的不重复随机数(m<=n)
- 设计模式基本概念总结(一)
- c++ 什么情况使用深拷贝什么情况使用浅拷贝
- [leetcode-139]Word Break(java)
- Generate Parentheses(****)
- OpenSessionInView
- 问题解决:java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date
- AndroidFrameWork
- hibernate级联删除操作
- Treap 模板
- C++基础---char型字符串库函数
- Android获得当前系统时间、星期几、周几
- 二叉树基本操作
- $ionicActionSheet的学习