十二、增加Department

1.增加Department

  1.1在com.myz.domain包下增加Department类

package com.myz.domain;

import java.util.Set;

public class Department {
    
    private Integer id;//部门编号
    private String name;//部门名称
    
    private Set<Employee> emps;//员工
    
    
    public Department() {
        
    }
    public Department(Integer id, String name) {
        this.id = id;
        this.name = name;
    }
    public Set<Employee> getEmps() {
        return emps;
    }
    public void setEmps(Set<Employee> emps) {
        this.emps = 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;
    }
}

  1.2增加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.myz.domain">
    <class name="Department" table="department">
        <cache usage="read-write"/>
        <id name="id" type="java.lang.Integer">
            <generator class="assigned"></generator>
        </id>
        <property name="name" type="java.lang.String">
            <column name="name"></column>
        </property>
        
        <!-- 一个部门可以有多个雇员-->
        <set name="emps" cascade="save-update">
            <key column="department_id"></key>
            <one-to-many class="Employee"/>
        </set>
    </class>
</hibernate-mapping>

 

  1.3修改Employee类,修改Employee.hbm.xml

package com.myz.domain;

import java.util.Date;

public class Employee {
    private Integer id;//编号
    private String name;//名字
    private String email;//邮件
    private Date hiredate;//雇用日期
    private Float salary;//工资
    private String password;//密码
    private Integer grade;//等级
    
    private Department department;//所属部门
    
    
    public Department getDepartment() {
        return department;
    }


    public void setDepartment(Department department) {
        this.department = department;
    }


public Employee() {
        
    }
    

    public Employee(Integer id, String name, String email, Date hiredate,
            Float salary, String password, Integer grade) {
        super();
        this.id = id;
        this.name = name;
        this.email = email;
        this.hiredate = hiredate;
        this.salary = salary;
        this.setPassword(password);
        this.setGrade(grade);
    }



    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 String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Date getHiredate() {
        return hiredate;
    }
    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }
    public Float getSalary() {
        return salary;
    }
    public void setSalary(Float salary) {
        this.salary = salary;
    }


    public void setGrade(Integer grade) {
        this.grade = grade;
    }


    public Integer getGrade() {
        return grade;
    }


    public void setPassword(String password) {
        this.password = password;
    }


    public String getPassword() {
        return password;
    }
}
<?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.myz.domain">
    <class name="Employee" table="employee">
        <cache usage="read-write"/>
        <id name="id" type="java.lang.Integer">
            <generator class="assigned"></generator>
        </id>
        <property name="name" type="java.lang.String">
            <column name="name"></column>
        </property>
        <property name="email" type="java.lang.String">
            <column name="email" length="64"></column>
        </property>
        <property name="hiredate">
            <column name="hiredate"></column>
        </property>
        <property name="salary" type="java.lang.Float">
            <column name="salary"></column>
        </property>
        <property name="password" type="java.lang.String">
            <column name="password"></column>
        </property>
        <property name="grade" type="java.lang.Integer">
            <column name="grade"></column>
        </property>
        
        <!-- 一个雇员属于一个部门 -->
        <many-to-one name="department" column="department_id"></many-to-one>
    </class>
</hibernate-mapping>

  1.4增加DepartmentServiceInterface接口和DepartmentService实现类到对应包下

package com.myz.service.interfaces;

import com.myz.domain.Department;

public interface DepartmentServiceInterface {
    public void addDepartment(Department d);//添加部门
}
package com.myz.service.imps;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.transaction.annotation.Transactional;

import com.myz.domain.Department;
import com.myz.service.interfaces.DepartmentServiceInterface;

@Transactional
public class DepartmentService implements DepartmentServiceInterface {

    @Resource
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    public void addDepartment(Department d) {
        // TODO Auto-generated method stub
        Session session = sessionFactory.getCurrentSession();
        session.save(d);
    }

}

  1.5在applicationContext.xml中配置DepartmentService

<!-- 配置 DepartmentService,注解方式注入属性-->
    <bean id="departmentService" class="com.myz.service.imps.DepartmentService"></bean>

  1.6在applicationContext.xml中让spring管理Department.hbm.xml

<property name="mappingResources">
            <list>
                <value>com/myz/domain/Employee.hbm.xml</value>
                <value>com/myz/domain/Department.hbm.xml</value>
            </list>
        </property>

 

2.测试添加数据

package com.myz.test;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.myz.domain.Department;
import com.myz.domain.Employee;
import com.myz.service.imps.EmployeeService;
import com.myz.service.interfaces.DepartmentServiceInterface;
import com.myz.service.interfaces.EmployeeServiceInterface;

public class Test {
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        DepartmentServiceInterface ds=(DepartmentServiceInterface) ac.getBean("departmentService");
        
        
//        增加三个雇员到一个部门,均保存
        Employee e1=new Employee(13, "比尔盖茨", "2466@qq.com", new Date(), 4500f, "123456", 1);
        Employee e2=new Employee(14, "比尔盖茨", "2466@qq.com", new Date(), 4500f, "123456", 1);
        Employee e3=new Employee(15, "比尔盖茨", "2466@qq.com", new Date(), 4500f, "123456", 1);
        
        Department d1=new Department(1, "财务部");
        
        Set<Employee> emps=new HashSet<Employee>();
        emps.add(e1);
        emps.add(e2);
        emps.add(e3);
        
        d1.setEmps(emps);
        
        ds.addDepartment(d1);
        
    }
}

 

 

3.在mainFrame.jsp中,显示登录者的信息

  3.1显示登录者的姓名,由于登录时候我们把loginUser放入session中了,可以显示成功

<h2>欢迎${loginUser.name}登录</h2>
    <a href="#">添加雇员</a><br/>
    <a href="#">显示雇员</a><br/>
    <a href="#">查询雇员</a><br/>
    <a href="#">退出系统</a><br/>
  </body>

  3.2显示登录者所在部门,却会报500的错,could not initialize proxy - no Session,这是懒加载引起的,登录时仅返回Employee对象就关闭session了,可后面发现还要查询loginUser.department.name

<body>
  <h2>欢迎${loginUser.name}登录,你所在的部门为${loginUser.department.name}</h2>
    <a href="#">添加雇员</a><br/>
    <a href="#">显示雇员</a><br/>
    <a href="#">查询雇员</a><br/>
    <a href="#">退出系统</a><br/>
  </body>

 

posted @ 2018-02-10 12:29  菜鸡蔡文姬  阅读(257)  评论(0编辑  收藏  举报