springboot简单项目(5) 员工信息管理

信息展示

编写一个控制类 调用DAO

package com.jie.controller;

import com.jie.dao.EmployeeDAO;
import com.jie.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
public class EmployeeController {
    @Autowired
    EmployeeDAO employeeDAO;
    @RequestMapping("/emps")
    public String list(Model model){
        Collection<Employee> employees = employeeDAO.getAll();
        model.addAttribute("emps",employees);
        return "emp/list";
    }
}

让主界面点击员工管理跳到写好的控制类

springboot简单项目(5) 员工信息管理

在显示页面加入该语句:

springboot简单项目(5) 员工信息管理

实现添加员工功能

控制类实现两个方法

package com.jie.controller;

import com.jie.dao.DepartmentDAO;
import com.jie.dao.EmployeeDAO;
import com.jie.pojo.Department;
import com.jie.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
public class EmployeeController {
    @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")
    public String toAddPage(Model model){
        Collection<Department> departments = departmentDAO.getDepartment();
        model.addAttribute("departments",departments);
        return "emp/add";
    }
    @PostMapping("/emp")
    public String toAddEmp(Employee employee){
        employeeDAO.save(employee);
        return "redirect:/emps";
    }
}

添加页面表单内容

<form th:action="@{/emp}" method="post">
                <div class="form-group">
                    <label>Last name</label>
                    <input type="text" name="lastName" class="form-control" placeholder="jie">
                </div>
                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="email" class="form-control" placeholder="26220902.com">
                </div>
                <div class="form-group">
                    <label>Gender</label><br>
                    <div class="form-check form-check-inline">
                        <input class="form-check-input" type="radio" name="gender" value="1">
                        <label class="form-check-label">男</label>
                    </div>
                    <div class="form-check form-check-inline">
                        <input class="form-check-input" type="radio" name="gender" value="0">
                        <label class="form-check-label">女</label>
                    </div>
                </div>
                <div class="form-group">
                    <label>Department</label>
                    <select class="form-control" name="department.id">
                        <option th:each="dept:${departments}" th:text="${dept.getDepartmentName()}" th:value="${dept.getId()}"></option>
                    </select>
                </div>
                <div class="form-group">
                    <label>Birth</label>
                    <input type="text" class="form-control" name="birth" placeholder="">
                </div>
                <button type="submit" class="btn btn-primary">提交</button>
            </form>

实现修改功能

上一篇:逆元,排列组合


下一篇:Python 循环结构练习