package com.likefr.springboot.controller;

import com.likefr.springboot.dao.DepartmentDao;
import com.likefr.springboot.dao.EmployeeDao;
import com.likefr.springboot.pojo.Department;
import com.likefr.springboot.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;

@Controller
public class EmployeeContrlloer {
    @Autowired
    EmployeeDao employeeDao;

    @Autowired
    DepartmentDao departmentDao;

    @RequestMapping("/emps")
    public String list(Model model) {
        Collection<Employee> employees = employeeDao.getAll();
        model.addAttribute("emps", employees);
        return "emp/list";
    }


    //编辑
    @GetMapping("/emp/{id}")
    public String Update(@PathVariable("id") Integer id, Model model) {
        Employee employeeById = employeeDao.getEmployeeById(id);
        model.addAttribute("employee", employeeById);

        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments", departments);
        return "emp/update";
    }


    @PostMapping("/updatwEmp")
    public String updateEmp(Employee employee) {
        employeeDao.save(employee);
        return "redirect:/emps";
    }


    @GetMapping("/deleteEmp/{id}")
    public String deleteEmp(@PathVariable("id") int id, Model model) {
            employeeDao.delete(id);

        return "redirect:/emps";
    }











































/*
    @GetMapping("/emp")
    public String toAddpage(Model model) {
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments", departments);
        return "emp/add";
    }

    @PostMapping("/emp")
    public String Addpage(Employee employee) {
        employeeDao.save(employee);
        return "redirect:/emps";
    }*/


}
最后修改:2020 年 12 月 01 日
如果觉得我的文章对你有用,请随意赞赏