六、spring可以启用注解的方式来配置属性

  1. 重新这样配置bean,这个是原先的employeeService
<!-- 添加实现逻辑对象 -->
<bean id="employeeService" class="com.wang.service.imp.EmployeeService">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

这个是配置后的

<!-- 使用注解的方式配置逻辑对象 -->
<bean id="employeeService" class="com.wang.service.imp.EmployeeService" />

2.添加下面的话在ApplicationContext中启用注解

<!--  启用注解扫描:-->
<context:annotation-config />

3. 在EmployeeService中添加@Resource,javax.annotation.Resource

public class EmployeeService implements EmployeeServiceInter {

@Resource
private SessionFactory sessionFactory ;//这里的sessionFactory 必须和ApplicationContext中的bean对应上,名字相同

public void setSessionFactory(SessionFactory sessionFactory) {
System.out.println("使用注解的方式@Resource实现bean的调用");
this.sessionFactory = sessionFactory;
}

4. 同样的方法 action也可以用注解的方式来配置bean

========================================================================================


1. 解决懒加载问题,以下先介绍下hibernate中关系映射问题

ssh之雇员管理系统(7)-spring可以启用注解的方式来配置属性+解决懒加载问题_懒加载

ssh之雇员管理系统(7)-spring可以启用注解的方式来配置属性+解决懒加载问题_java_02ssh之雇员管理系统(7)-spring可以启用注解的方式来配置属性+解决懒加载问题_hibernate_03ssh之雇员管理系统(7)-spring可以启用注解的方式来配置属性+解决懒加载问题_懒加载_04ssh之雇员管理系统(7)-spring可以启用注解的方式来配置属性+解决懒加载问题_hibernate_05ssh之雇员管理系统(7)-spring可以启用注解的方式来配置属性+解决懒加载问题_懒加载_06ssh之雇员管理系统(7)-spring可以启用注解的方式来配置属性+解决懒加载问题_java_07ssh之雇员管理系统(7)-spring可以启用注解的方式来配置属性+解决懒加载问题_hibernate_08

2. 我们增加一个部门Department,那么雇员-》部门就是多对一,部门-》雇员就是一对多的问题

  • Department.java
package com.wang.domain;

import java.util.Set;

public class Department {

private Integer id;
private String name;

//由于一个部门下面有很多雇员,所以这里用set集合
private Set<Employee> emps;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Employee> getEmps() {
return emps;
}
public void setEmps(Set<Employee> emps) {
this.emps = emps;
}
}
  • View Code
  • Department.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.wang.domain">
<class name="Department" table="department">
<!-- 主键策略 -->
<id name="id" type="java.lang.Integer">
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="32"></column>
</property>

<!-- 一对多的配置 -->
<set name="emps">
<key column="department_id"></key>
<one-to-many class="com.wang.domain.Employee"/>
</set>
</class>
</hibernate-mapping>
  • View Code
  • Employee.hbm.xml中添加
<!-- 多对一雇员到部门的配置 -->
<many-to-one name="department" column="department_id" />
  • Employee.java中添加这个部门字段,并且实现他的set和get方法
//employee->department
private Department department;

3. 用spring提供opensessioninview的方法来解决懒加载

<!--提供了opensessioninview的方法来解决懒加载  -->
<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>

4. 在页面出调用通过用之前的setattribute的loginuser即${loginuser.department.name}可将部门调出来

 



作者:少帅

您的支持是对博主最大的鼓励,感谢您的认真阅读。

本文版权归作者所有,欢迎转载,但请保留该声明。