代码改变世界

SpringBoot 之 控制器层

2020-05-05 17:40  小伍2013  阅读(650)  评论(0编辑  收藏  举报
@Controller
public class EmployeeController {

    @Autowired
    EmployeeDao employeeDao;

    @Autowired
    DepartmentDao departmentDao;

    @GetMapping("/employees")
    public String index(Model model) {
        Collection<Employee> employees = employeeDao.index();
        model.addAttribute("employees", employees);
        return "employees/index";
    }

    @PostMapping("/employees")
    public String store(Employee employee) {
        employeeDao.store(employee);
        return "redirect:/employees";
    }

    @GetMapping("/employees/{id}")
    public String show(@PathVariable("id") Integer id, Model model) {
        Employee employee = employeeDao.show(id);
        model.addAttribute("employee", employee);
        Collection<Department> departments = departmentDao.index();
        model.addAttribute("departments", departments);
        return "employees/show";
    }

    @PutMapping("/employees/{id}")
    public String update(@PathVariable("id") Integer id, Employee employee) {
        employee.setId(id);
        employeeDao.update(employee);
        return "redirect:/employees";
    }

    @DeleteMapping("/employees/{id}")
    public String destroy(@PathVariable("id") Integer id) {
        employeeDao.destroy(id);
        return "redirect:/employees";
    }
}