标签:put aof obj 增加用户 方法 .com 信息 key tde
DepartmentDao.java:
package com.springmvc.Dao;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import com.springmvc.pojo.Department;
public class DepartmentDao {
private static Map<Integer,Department> depts;
static {
depts=new HashMap<Integer,Department>();
depts.put(1, new Department(1, "SaleDepartment"));
depts.put(2, new Department(2, "HR"));
}
public static Collection<Department>getAllDepts(){
return depts.values();
}
public static Department getDeptById(Integer id) {
return depts.get(id);
}
}
------------------------------------------------------------------------------------------------------------------------------------------
EmployeeDao.java:
package com.springmvc.Dao;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import com.springmvc.pojo.Employee;
public class EmployeeDao {
private static Map<Integer, Employee> emps;
static {
emps = new HashMap<Integer, Employee>();
emps.put(101, new Employee(101, "wantao", "895484122@qq.com", 0, DepartmentDao.getDeptById(2)));
emps.put(102, new Employee(102, "wanxiaofei", "895484123@qq.com", 1, DepartmentDao.getDeptById(1)));
emps.put(103, new Employee(103, "wanchao", "895484124@qq.com", 0, DepartmentDao.getDeptById(1)));
emps.put(104, new Employee(104, "gaohan", "895484125@qq.com", 0, DepartmentDao.getDeptById(1)));
emps.put(105, new Employee(105, "lihan", "895484126@qq.com", 0, DepartmentDao.getDeptById(1)));
}
// 获取所有员工的信息,通过map集合完
// success.jsp中遍历map,<%%> JSTL foreach
// Map转换为Collection
public static Collection<Employee> getAllEmployees() {
return emps.values();
}
public static Employee getEmpById(Integer id) {
return emps.get(id);
}
private static Integer key = 106;
public static void save(Employee emp) {
emp.setId(key);
emp.setDept(DepartmentDao.getDeptById(emp.getDept().getDeptId()));
emps.put(key++, emp);
}
public static void delete(Integer id) {
emps.remove(id);
}
public static void update(Integer id,Employee emp) {
emp.setDept(DepartmentDao.getDeptById(emp.getDept().getDeptId()));
emps.put(id, emp);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------
EmployeeHandler.java:
package com.springmvc.handler;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.springmvc.Dao.DepartmentDao;
import com.springmvc.Dao.EmployeeDao;
import com.springmvc.pojo.Employee;
@Controller
public class EmployeeHandler {
private final String SUCCESS = "success";
private final String ADD = "add";
@RequestMapping(value = "/showEmp", method = RequestMethod.GET)
public String showEmp(Map<String, Object> map) {
map.put("emps", EmployeeDao.getAllEmployees());
return SUCCESS;
}
// addEmp方法,完成用戶输入功能的跳转:1.增加用户2.更改用户
@RequestMapping(value = "/addEmp/{id}", method = RequestMethod.GET)
public String addEmp(@PathVariable("id") Integer id, Map<String, Object> map) {
// 获取所有的性别
// 获取所有的部门信息
Map<Integer, Object> genders = new HashMap<Integer, Object>();
Employee employee = new Employee();
genders.put(0, "MALE");
genders.put(1, "FEMALE");
map.put("genders", genders);
if (id != 0) {
employee = EmployeeDao.getEmpById(id);
}
map.put("depts", DepartmentDao.getAllDepts());
map.put("command", employee);
return ADD;
}
@RequestMapping(value = "/emp", method = RequestMethod.POST)
public String add(Employee emp) {
EmployeeDao.save(emp);
return "redirect:/showEmp";
}
@RequestMapping(value = "/emp/{id}", method = RequestMethod.DELETE)
public String delete(@PathVariable("id") Integer id) {
EmployeeDao.delete(id);
return "redirect:/showEmp";
}
@ModelAttribute
public void sex(Integer id,Map<String,Object> map) {
Employee employee=EmployeeDao.getEmpById(id);
map.put("employee", employee);
}
@RequestMapping(value="/emp",method=RequestMethod.PUT)
public String update(Employee emp) {
EmployeeDao.update(emp.getId(), emp);
return "redirect:/showEmp";
}
}
----------------------------------------------------------------------------------------------------------------------
Department.java:
package com.springmvc.pojo;
public class Department {
private Integer deptId;
private String deptName;
public Department() {
}
public Department(Integer deptId, String deptName) {
super();
this.deptId = deptId;
this.deptName = deptName;
}
@Override
public String toString() {
return "Department [deptId=" + deptId + ", deptName=" + deptName + "]";
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
--------------------------------------------------------------------------------------------------------------------------
Employee.java:
package com.springmvc.pojo;
public class Employee {
private int id;
private String name;
private String mail;
private int gender;
private Department dept;
public Employee() {
}
public Employee(int id, String name, String mail, int gender, Department dept) {
super();
this.id = id;
this.name = name;
this.mail = mail;
this.gender = gender;
this.dept = dept;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", mail=" + mail + ", gender=" + gender + ", dept=" + dept
+ "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
}
springmvc-2(rest风格的增删改查)-代码(1)
标签:put aof obj 增加用户 方法 .com 信息 key tde
原文地址:http://www.cnblogs.com/wantao/p/7988022.html