1.增加Department
1.1在com.myz.domain包下增加Department类
packagecom.myz.domain;importjava.util.Set;public classDepartment {private Integer id;//部门编号
private String name;//部门名称
private Set emps;//员工
publicDepartment() {
}publicDepartment(Integer id, String name) {this.id =id;this.name =name;
}public SetgetEmps() {returnemps;
}public void setEmps(Setemps) {this.emps =emps;
}publicInteger getId() {returnid;
}public voidsetId(Integer id) {this.id =id;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}
}
1.2增加Department.hbm.xml
/p>
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
1.3修改Employee类,修改Employee.hbm.xml
packagecom.myz.domain;importjava.util.Date;public classEmployee {private Integer id;//编号
private String name;//名字
private String email;//邮件
private Date hiredate;//雇用日期
private Float salary;//工资
private String password;//密码
private Integer grade;//等级
private Department department;//所属部门
publicDepartment getDepartment() {returndepartment;
}public voidsetDepartment(Department department) {this.department =department;
}publicEmployee() {
}publicEmployee(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);
}publicInteger getId() {returnid;
}public voidsetId(Integer id) {this.id =id;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicString getEmail() {returnemail;
}public voidsetEmail(String email) {this.email =email;
}publicDate getHiredate() {returnhiredate;
}public voidsetHiredate(Date hiredate) {this.hiredate =hiredate;
}publicFloat getSalary() {returnsalary;
}public voidsetSalary(Float salary) {this.salary =salary;
}public voidsetGrade(Integer grade) {this.grade =grade;
}publicInteger getGrade() {returngrade;
}public voidsetPassword(String password) {this.password =password;
}publicString getPassword() {returnpassword;
}
}
/p>
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
1.4增加DepartmentServiceInterface接口和DepartmentService实现类到对应包下
packagecom.myz.service.interfaces;importcom.myz.domain.Department;public interfaceDepartmentServiceInterface {public void addDepartment(Department d);//添加部门
}
packagecom.myz.service.imps;importjavax.annotation.Resource;importorg.hibernate.SessionFactory;importorg.hibernate.classic.Session;importorg.springframework.transaction.annotation.Transactional;importcom.myz.domain.Department;importcom.myz.service.interfaces.DepartmentServiceInterface;
@Transactionalpublic class DepartmentService implementsDepartmentServiceInterface {
@ResourceprivateSessionFactory sessionFactory;publicSessionFactory getSessionFactory() {returnsessionFactory;
}public voidsetSessionFactory(SessionFactory sessionFactory) {this.sessionFactory =sessionFactory;
}public voidaddDepartment(Department d) {//TODO Auto-generated method stub
Session session =sessionFactory.getCurrentSession();
session.save(d);
}
}
1.5在applicationContext.xml中配置DepartmentService
1.6在applicationContext.xml中让spring管理Department.hbm.xml
com/myz/domain/Employee.hbm.xml
com/myz/domain/Department.hbm.xml
2.测试添加数据
packagecom.myz.test;importjava.util.Date;importjava.util.HashSet;importjava.util.List;importjava.util.Set;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.Transaction;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importcom.myz.domain.Department;importcom.myz.domain.Employee;importcom.myz.service.imps.EmployeeService;importcom.myz.service.interfaces.DepartmentServiceInterface;importcom.myz.service.interfaces.EmployeeServiceInterface;public classTest {public static voidmain(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 emps=new HashSet();
emps.add(e1);
emps.add(e2);
emps.add(e3);
d1.setEmps(emps);
ds.addDepartment(d1);
}
}
3.在mainFrame.jsp中,显示登录者的信息
3.1显示登录者的姓名,由于登录时候我们把loginUser放入session中了,可以显示成功
欢迎${loginUser.name}登录
添加雇员
显示雇员
查询雇员
退出系统