雇员管理系统-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文件中添加如下配置:
该方法可以有效的减少对数据库的查询,缺点是和数据保持的session,时间延长了。
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,时间延长了。
相关文章推荐
- 雇员管理系统-SSH版(二)
- ssh之雇员管理系统(8)-增加其他功能
- ssh之雇员管理系统(10)-分页实现
- ssh之雇员管理系统(1)-spring测试
- ssh之雇员管理系统(2)-hibernate测试
- 雇员管理系统ssh之准备1
- 雇员管理系统-SSH版(三)
- 雇员管理系统-SSH版(九)
- 雇员管理系统-SSH版(四)
- 雇员管理系统-SSH版(八)
- Spring 雇员管理系统SSH 1
- 雇员管理系统-SSH版(十)
- 雇员管理系统-SSH版(五)
- Spring 雇员管理系统SSH 3
- 雇员管理系统-SSH版(七)
- Spring 雇员管理系统SSH 4
- Spring 雇员管理系统SSH 6
- Spring 雇员管理系统SSH 5