1. 程式人生 > >springmvc-2(rest風格的增刪改查)-代碼(1)

springmvc-2(rest風格的增刪改查)-代碼(1)

bin ger ash date quest java mail end collect

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", "[email protected]", 0, DepartmentDao.getDeptById(2)));
emps.put(102, new Employee(102, "wanxiaofei", "[email protected]", 1, DepartmentDao.getDeptById(1)));
emps.put(103, new Employee(103, "wanchao", "[email protected]", 0, DepartmentDao.getDeptById(1)));
emps.put(104, new Employee(104, "gaohan", "[email protected]", 0, DepartmentDao.getDeptById(1)));
emps.put(105, new Employee(105, "lihan", "[email protected]", 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)