springboot_数据增删改查

前期数据准备(模拟数据库)

@Repository
public class DepartmentDao {
    //模拟数据库中的数据
    private static Map<Integer, Department> departmentMap = null;
    static {
        departmentMap = new HashMap<>();
        departmentMap.put(101,new Department(101,"教学部"));
        departmentMap.put(102,new Department(102,"市场部"));
        departmentMap.put(103,new Department(103,"教研部"));
        departmentMap.put(104,new Department(104,"运营部"));
        departmentMap.put(105,new Department(105,"后勤部"));
    }
    //获得所有部门信息
    public Collection<Department> getDepartment(){
        return departmentMap.values();
    }
    //通过ID获取部门
    public Department getDepartmentById(Integer id){
        return departmentMap.get(id);
    }
}
@Repository
public class EmployeeDao {

    private static Map<Integer, Employee> employeeMap = null;
    @Autowired
    private DepartmentDao departmentDao;
    static {
        employeeMap = new HashMap<>();
        employeeMap.put(1001,new Employee(1001,"AA","123456AA@qq.com",1,new Department(101,"教学部")));
        employeeMap.put(1002,new Employee(1002,"BB","123456BB@qq.com",0,new Department(102,"市场部")));
        employeeMap.put(1003,new Employee(1003,"CC","123456CC@qq.com",1,new Department(103,"教研部")));
        employeeMap.put(1004,new Employee(1004,"DD","123456DD@qq.com",0,new Department(104,"运营部")));
        employeeMap.put(1005,new Employee(1005,"EE","123456EE@qq.com",1,new Department(105,"后勤部")));
    }
    //主键自增
    private static Integer initId = 1;
    //增加操作
    public void add(Employee employee){
        if (employee.getId() == null){
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employeeMap.put(employee.getId(),employee);
    }
    //查询全部员工
    public Collection<Employee> getAllEmployee(){
        return employeeMap.values();
    }
    //按id查询信息
    public Employee getEmployeeById(Integer id){
        return employeeMap.get(id);
    }
    //通过id删除员工
    public void deleteEmployeeById(Integer id){
        employeeMap.remove(id);
    }
}

1、员工列表展示(查)

  • thymeleaf提取公共页面
    • 提取:th:fragment="sidebar"
    • 插入:<div th:replace="~{commons/commons::sidebar}"></div>
      或者<div th:insert="~{commons/commons::sidebar}"></div>
    • 如果传递参数:使用():
      <div th:insert="~{commons/commons::sidebar(active='main.html')}"></div>
    • 接收判断:
      <a th:class="${active=='main.html'?'nav-link active':'nav-link'}" th:href="@{/index.html}">
  • 列表循环展示
    • Controller返回前端数据:
      @Controller
      public class EmployeeController {
          @Autowired
          EmployeeDao employeeDao;
      
          @RequestMapping("/emps")
          public String list(Model model){
              Collection<Employee> employees = employeeDao.getAllEmployee();
              model.addAttribute("emps",employees);
              return "emp/list";
          }
      }
      
    • 遍历循环:
      <tr th:each="emp:${emps}">
          <td th:text="${emp.getId()}"></td>
          <td th:text="${emp.getLastName()}"></td>
          <td th:text="${emp.getEmail()}"></td>
          <td th:text="${emp.getGender()==0?'女':'男'}"></td>
          <td th:text="${emp.getDepartment().getDepartmentName()}"></td>
          <td th:text="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm:ss')}"></td>
      </tr>
      

2、添加员工(增)

  • 提交按钮
    <!-- 设置action,进入对应的controller  请求方法:post -->
    <form th:action="@{/emp}" method="post">
    
    //进入@PostMapping的controller
    @PostMapping("/emp")
    public String addEmp(Employee employee){
        System.out.println(employee);
        employeeDao.add(employee);
        return "redirect:/emps";
    }
    
  • 跳转到添加页面
    <!-- 点击添加添加员工,进行页面跳转 -->
    <h2><a class="btn btn-sm btn-success" th:href="emp">添加员工</a></h2>
    
    //对应的controller,@GetMapping,此controller进行页面的跳转
    //model设置departments参数返回前端显示
    @GetMapping("/emp")
    public String toAddPage(Model model){
        Collection<Department> department = departmentDao.getDepartment();
        model.addAttribute("departments",department);
        return "emp/add";
    }
    
      <!-- 前端遍历departments进行显示 -->
      <select class="form-control" name="department.id">
          <option th:each="dept:${departments}" th:text="${dept.getDepartmentName()}" th:value="${dept.getId()}"></option>
      </select>
    
  • 添加员工成功
  • 返回首页
    @PostMapping("/emp")
        public String addEmp(Employee employee){
            System.out.println(employee);
            employeeDao.add(employee);
            //重定向到员工信息页面,
            return "redirect:/emps";
        }
    
    //跳转到对应这,返回list页面。
    @RequestMapping("/emps")
    public String list(Model model){
    Collection<Employee> employees = employeeDao.getAllEmployee();
    model.addAttribute("emps",employees);
    return "emp/list";
    }
    

3、员工修改信息(改)

  • 编辑按钮
    <!-- 添加id参数,以便于修改页面显示对应的信息-->
    <!-- http://localhost:8080/emp/1001 -->
    <a class="btn btn-sm btn-primary" th:href="@{/emp/}+${emp.getId()}">编辑</a>
    
  • 跳转页面controller
    //去员工的修改页面
        @GetMapping("/emp/{id}")
        public String toUpdatePage(@PathVariable("id")Integer id,Model model){
            //查出原来的数据
            Employee employee = employeeDao.getEmployeeById(id);
            model.addAttribute("employee",employee);
            //查询所有部门的信息
            Collection<Department> department = departmentDao.getDepartment();
            model.addAttribute("departments",department);
            return "emp/update";
        }
    
  • 添加隐藏域,防止在用户展示页面会自增长
    <!--添加隐藏域-->
    <input type="hidden" name="id" th:value="${employee.getId()}">
    
  • 修改成功,跳转页面
    <form th:action="@{/empUpdate}" method="post"></form>
    
    //去员工的修改页面
    @PostMapping("/empUpdate")
    public String updateEmp(Employee employee){
        employeeDao.add(employee);
        return "redirect:/emps";
    }
    

4、删除信息(删)

  • 删除的标签

    <a class="btn btn-sm btn-danger" th:href="@{/delEmp/}+${emp.getId()}">删除</a>
    
  • 跳转的controller

    //删除员工
    @GetMapping("/delEmp/{id}")
    public String deleteEmp(@PathVariable("id")Integer id){
        employeeDao.deleteEmployeeById(id);
        return "redirect:/emps";
    }
    

5、注销

  • 前端
    <a class="nav-link" th:href="@{/user/logout}">注销</a>
    
  • controller
    @RequestMapping("/user/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/index.html";
    }
    
posted @ 2021-06-29 21:57  DingJie1024  阅读(629)  评论(0编辑  收藏  举报