spring openSessionInView 基础配置


WebRoot/WEB-INF/web.xml 中添加过滤器

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<filter>

<filter-name>openSessionInViewFilter</filter-name>

<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>

<init-param>

<param-name>sessionFactoryBeanName</param-name>

<param-value>sessionFactory</param-value>

</init-param>

<init-param>

<param-name>singleSession</param-name>

<param-value>true</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>openSessionInViewFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

 

 

注意放在struts前,如果项目有启用的话

DAO层调用session时使用 getCurrentSession() ,而不是openSession() 且搭配事务使用,手动开启和提交事务, 

如果之后需要使用到级联,则不应该关闭session;

注意除了查询操作以外,都应该调用session.flush()方法,以确保对数据库的写入.

 

主要遇到的问题就是session的关闭,之前以为使用了opensessioninview是关闭也没关系的.这点在找到的资料里也没有说明,所以才被误导了

目前只知道基本的使用而已,只是为了解决Lazy Load的级联问题, 对于这个功能还需要进一步了解.

 

posted @ 2016-05-02 18:38  岚罗青呱  阅读(1092)  评论(0编辑  收藏  举报