13.5.5 员工管理

13.5.5 员工管理

处理员工的EmployeeController代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package org.fkit.hrm.controller;

import java.util.List;
import org.fkit.hrm.domain.Dept;
import org.fkit.hrm.domain.Employee;
import org.fkit.hrm.domain.Job;
import org.fkit.hrm.service.HrmService;
import org.fkit.hrm.util.tag.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
* @Description: 处理员工请求控制器 <br>
*/
@Controller
public class EmployeeController
{
/**
* 自动注入hrmService
*/
@Autowired
@Qualifier("hrmService")
private HrmService hrmService;

/**
* 处理查询请求
* @param pageIndex 请求的是第几页
* @param String job_id 职位编号
* @param String dept_id 部门编号
* @param employee 模糊查询参数
* @param Model model
*/
@RequestMapping(value = "/employee/selectEmployee")
public String selectEmployee(Integer pageIndex, Integer job_id,
Integer dept_id, @ModelAttribute Employee employee, Model model)
{
// 模糊查询时判断是否有关联对象传递,如果有,创建并封装关联对象
this.genericAssociation(job_id, dept_id, employee);
// 创建分页对象
PageModel pageModel = new PageModel();
// 如果参数pageIndex不为null,设置pageIndex,即显示第几页
if (pageIndex != null)
{
pageModel.setPageIndex(pageIndex);
}
// 查询职位信息,用于模糊查询
List<Job> jobs = hrmService.findAllJob();
// 查询部门信息 ,用于模糊查询
List<Dept> depts = hrmService.findAllDept();
// 查询员工信息
List<Employee> employees = hrmService.findEmployee(employee, pageModel);
// 设置Model数据
model.addAttribute("employees", employees);
model.addAttribute("jobs", jobs);
model.addAttribute("depts", depts);
model.addAttribute("pageModel", pageModel);
// 返回员工页面
return "employee/employee";

}

/**
* 处理添加员工请求
* @param String flag 标记, 1表示跳转到添加页面,2表示执行添加操作
* @param String job_id 职位编号
* @param String dept_id 部门编号
* @param Employee employee 接收添加参数
* @param ModelAndView mv
*/
@RequestMapping(value = "/employee/addEmployee")
public ModelAndView addEmployee(String flag, Integer job_id,
Integer dept_id, @ModelAttribute Employee employee, ModelAndView mv)
{
if (flag.equals("1"))
{
// 查询职位信息
List<Job> jobs = hrmService.findAllJob();
// 查询部门信息
List<Dept> depts = hrmService.findAllDept();
// 设置Model数据
mv.addObject("jobs", jobs);
mv.addObject("depts", depts);
// 返回添加员工页面
mv.setViewName("employee/showAddEmployee");
} else
{
// 判断是否有关联对象传递,如果有,创建关联对象
this.genericAssociation(job_id, dept_id, employee);
// 添加操作
hrmService.addEmployee(employee);
// 设置客户端跳转到查询请求
mv.setViewName("redirect:/employee/selectEmployee");
}
// 返回
return mv;

}

/**
* 处理删除员工请求
* @param String ids 需要删除的id字符串
* @param ModelAndView mv
*/
@RequestMapping(value = "/employee/removeEmployee")
public ModelAndView removeEmployee(String ids, ModelAndView mv)
{
// 分解id字符串
String[] idArray = ids.split(",");
for (String id : idArray)
{
// 根据id删除员工
hrmService.removeEmployeeById(Integer.parseInt(id));
}
// 设置客户端跳转到查询请求
// mv.setView(new RedirectView("/hrmapp/employee/selectEmployee"));
// mv.setViewName("forward:/employee/selectEmployee");
mv.setViewName("redirect:/employee/selectEmployee");
// 返回ModelAndView
return mv;
}

/**
* 处理修改员工请求
* @param String flag 标记,1表示跳转到修改页面,2表示执行修改操作
* @param String job_id 职位编号
* @param String dept_id 部门编号
* @param Employee employee 要修改员工的对象
* @param ModelAndView mv
*/
@RequestMapping(value = "/employee/updateEmployee")
public ModelAndView updateEmployee(String flag, Integer job_id,
Integer dept_id, @ModelAttribute Employee employee, ModelAndView mv)
{
if (flag.equals("1"))
{
// 根据id查询员工
Employee target = hrmService.findEmployeeById(employee.getId());
// 需要查询职位信息
List<Job> jobs = hrmService.findAllJob();
// 需要查询部门信息
List<Dept> depts = hrmService.findAllDept();
// 设置Model数据
mv.addObject("jobs", jobs);
mv.addObject("depts", depts);
mv.addObject("employee", target);
// 返回修改员工页面
mv.setViewName("employee/showUpdateEmployee");
} else
{
// 创建并封装关联对象
this.genericAssociation(job_id, dept_id, employee);
System.out.println("updateEmployee -->> " + employee);
// 执行修改操作
hrmService.modifyEmployee(employee);
// 设置客户端跳转到查询请求
mv.setViewName("redirect:/employee/selectEmployee");
}
// 返回
return mv;
}

/**
* 由于部门和职位在Employee中是对象关联映射, 所以不能直接接收参数,需要创建Job对象和Dept对象
*/
private void genericAssociation(Integer job_id, Integer dept_id,
Employee employee)
{
if (job_id != null)
{
Job job = new Job();
job.setId(job_id);
employee.setJob(job);
}
if (dept_id != null)
{
Dept dept = new Dept();
dept.setId(dept_id);
employee.setDept(dept);
}
}
}

单击左侧菜单”员工管理“下面的”添加员工“命令,跳转到添加员工界面
输入需要添加的员工信息,单击”添加”按钮,若添加成功则跳转到员工査询界面,显示所有员工信息。
选中职位、性别、所属部门,输入姓名、身份证号码、手机等,单击”搜索”按钮可以完成模糊査询功能。选择每一行最后一列的”操作”按钮,可以进入修改页面,对选中的员工进行修改操作。选择每一行第一列的复选框,单击”删除”按钮,则可以对选中的员工进行删除操作。