150_员工管理系统:增删改查

目录

员工列表

package com.qing.controller;

import com.qing.dao.EmployeeDao;
import com.qing.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.RequestMapping;

import java.util.Collection;

/**
 * 员工Controller
 */
@Controller
public class EmployeeController {

    @Autowired
    private EmployeeDao employeeDao;

    /**
     * 员工列表
     * @param model
     * @return
     */
    @RequestMapping("/emp/list")
    public String list(Model model) {
        Collection<Employee> list = employeeDao.list();
        model.addAttribute("emps", list);
        return "emp/list";
    }
}

新增员工

package com.qing.controller;

import com.qing.dao.DepartmentDao;
import com.qing.dao.EmployeeDao;
import com.qing.pojo.Department;
import com.qing.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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;

/**
 * 员工Controller
 */
@Controller
public class EmployeeController {

    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private DepartmentDao departmentDao;

    /**
     * 员工列表
     * @param model
     * @return
     */
    @RequestMapping("/emp/list")
    public String list(Model model) {
        Collection<Employee> list = employeeDao.list();
        model.addAttribute("emps", list);
        return "emp/list";
    }

    /**
     * 去新增员工页面
     * @param model
     * @return
     */
    @GetMapping("/emp")
    public String toAdd(Model model) {
        Collection<Department> departments = departmentDao.list();
        model.addAttribute("departments", departments);
        return "emp/add";
    }

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping("/emp")
    public String add(Employee employee) {
        employeeDao.save(employee);
        return "redirect:/emp/list";
    }
}

@GetMapping("/emp")和@PostMapping("/emp")可以同名,不同的请求方式,进不同的接口

thymeleaf视图解析器解析重定向和转发

150_员工管理系统:增删改查
150_员工管理系统:增删改查

修改员工

package com.qing.controller;

import com.qing.dao.DepartmentDao;
import com.qing.dao.EmployeeDao;
import com.qing.pojo.Department;
import com.qing.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
 */
@Controller
public class EmployeeController {

    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private DepartmentDao departmentDao;

    /**
     * 员工列表
     * @param model
     * @return
     */
    @RequestMapping("/emp/list")
    public String list(Model model) {
        Collection<Employee> list = employeeDao.list();
        model.addAttribute("emps", list);
        return "emp/list";
    }

    /**
     * 去修改员工页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/emp/{id}")
    public String toUpdate(@PathVariable("id") Integer id, Model model) {
        Employee employee = employeeDao.getEmployee(id);
        model.addAttribute("emp", employee);
        Collection<Department> departments = departmentDao.list();
        model.addAttribute("departments", departments);
        return "emp/update";
    }

    /**
     * 修改员工
     * @param employee
     * @return
     */
    @PostMapping("/emp/update")
    public String update(Employee employee) {
        employeeDao.update(employee);
        return "redirect:/emp/list";
    }
}

删除员工

package com.qing.controller;

import com.qing.dao.DepartmentDao;
import com.qing.dao.EmployeeDao;
import com.qing.pojo.Department;
import com.qing.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
 */
@Controller
public class EmployeeController {

    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private DepartmentDao departmentDao;

    /**
     * 员工列表
     * @param model
     * @return
     */
    @RequestMapping("/emp/list")
    public String list(Model model) {
        Collection<Employee> list = employeeDao.list();
        model.addAttribute("emps", list);
        return "emp/list";
    }

    /**
     * 删除员工
     * @param id
     * @return
     */
    @GetMapping("/emp/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        employeeDao.delete(id);
        return "redirect:/emp/list";
    }
}

上一篇:C#.net和java实现国密SM3算法


下一篇:Mybatis框架快速入门-3