java department_十二、增加Department

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}登录

添加雇员

显示雇员

查询雇员

退出系统

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package indi.wrenn.studentsystem.bean; import java.util.HashMap; import java.util.List; public class Student { private String student_Name; //学生姓名 private String student_ID; //学生学号 private String sex; //学生性别 private String classe; //学生班级 private String grade; //学生年级 public HashMap<String, String> getScores() { return scores; } public void setScores(HashMap<String, String> scores) { this.scores = scores; } private String Major_ID; //学生所属专业边编号 private String Department_ID; //学生所属院系编号 private String Major_Name; //学生所属院系名称 private String Department_Name;//学生所属专业名称 private HashMap<String, String> scores; //学生成绩 public String getMajor_Name() { return Major_Name; } public void setMajor_Name(String major_Name) { Major_Name = major_Name; } public String getDepartment_Name() { return Department_Name; } public void setDepartment_Name(String department_Name) { Department_Name = department_Name; } public String getStudent_Name() { return student_Name; } public void setStudent_Name(String student_Name) { this.student_Name = student_Name; } public String getStudent_ID() { return student_ID; } public void setStudent_ID(String student_ID) { this.student_ID = student_ID; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getClasse() { return classe; } public void setClasse(String classe) { this.classe = classe; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } public String getMajor_ID() { return Major_ID; } public void setMajor_ID(String major_ID) { Major_ID = major_ID; } public String getDepartment_ID() { return Department_ID; } public void setDepartment_ID(String department_ID) { Department_ID = department_ID; } }分析代码
最新发布
06-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值