您的位置:首页 > 编程语言 > Java开发

雇员管理系统-SSH版(六)

2017-12-23 18:52 323 查看
一 spring可以启用注解的方式来配置属性
1 重新这样配置bean
<bean id="employeeService" class="com.hsp.service.imp.EmployeeService"/>
2 在EmployeeService的属性sessionFactory中添加一个注解@Resource
@Resource
private SessionFactory sessionFactory;
3 在applicationContext.xml中启用注解
<!-- 启用注解扫描 -->
<context:annotation-config/>

二 解决懒加载问题
思考:如果我们在mainFrame.jsp中要求显示该雇员所在部门的时候,怎样处理?
当访问${loginuser.department.name}时,懒加载问题会出现。
解决思路有3个:
1 明确初始化
在session还没有关闭时,访问一次 xxx.getXxx(),强制访问数据库。或者 Hibernate.initialize(xxx)。
2    在对象映射文件中 取消懒加载  <lazy=”false”/>
上面方法问题是: 不管你在jsp中使不使用部门的名称,它都有向数据库发出select 请求。
3 spring专门提供了opensessioninview的方法来解决懒加载。
需要在web.xml文件中添加如下配置:
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

该方法可以有效的减少对数据库的查询,缺点是和数据保持的session,时间延长了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring