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的级联问题, 对于这个功能还需要进一步了解.

 

转载于:https://www.cnblogs.com/rufusvisaber/p/5452829.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值