springboot(8)——员工管理系统之首页配置

1 项目准备工作

在这里插入图片描述

数据准备阶段
Employee .java

package com.zs.helloword.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

// 员工表
@Data
@NoArgsConstructor
public class Employee {
    
    
    private Integer id;
    private String lastName;
    private String email;
    private Integer gender;  // 0女 1男

    private Department department;
    private Date brith;

    public Employee(Integer id, String lastName, String email, Integer gender, Department department) {
    
    
        this.id = id;
        this.lastName = lastName;
        this.email = email;
        this.gender = gender;
        this.department = department;
        // 默认的创建日期
        this.brith = new Date();
    }
}

Department.java

package com.zs.helloword.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
    
    
    private Integer id;
    private String departmentName;
}

DepartmentDao.java

package com.zs.helloword.dao;

import com.zs.helloword.pojo.Department;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

// 部门Dao
@Repository
public class DepartmentDao {
    
    
    // 模拟数据库中的数据
    private static Map<Integer, Department> departments = null;
    static {
    
    
        departments = new HashMap<Integer,Department>(); // 创建一个部门表

        departments.put(101,new Department(101,"教学部1"));
        departments.put(102,new Department(102,"教学部2"));
        departments.put(103,new Department(103,"教学部3"));
        departments.put(104,new Department(104,"教学部4"));
        departments.put(105,new Department(105,"教学部5"));
    }

    // 获得所有部门信息
    public Collection<Department> getDepartments() {
    
    
        return departments.values();
    }
    // 通过id得到部门信息
    public Department getDepartmentById(Integer id) {
    
    
        return departments.get(id);
    }
}

EmployeeDao.java

package com.zs.helloword.dao;

import com.zs.helloword.pojo.Department;
import com.zs.helloword.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

// 员工Dao
@Repository
public class EmployeeDao {
    
    
    // 模拟数据库中的数据
    private static Map<Integer, Employee> employees = null;
    // 员工所属的部门
    @Autowired
    private DepartmentDao departmentDao;

    static {
    
    
        employees = new HashMap<Integer,Employee>();  // 创建一个部门表
        employees.put(1001, new Employee(1001,"zs1","[email protected]",1,new Department(101,"教学部1")));
        employees.put(1002, new Employee(1002,"zs2","[email protected]",0,new Department(102,"教学部2")));
        employees.put(1003, new Employee(1003,"zs3","[email protected]",0,new Department(103,"教学部3")));
        employees.put(1004, new Employee(1004,"zs4","[email protected]",1,new Department(104,"教学部4")));
        employees.put(1005, new Employee(1005,"zs5","[email protected]",1,new Department(105,"教学部5")));
    }
    // 主键自增
    // 增加一个员工
    private static Integer initId = 1006;
    public void save(Employee employee) {
    
    
        if (employee.getId() == null) {
    
    
            employee.setId(initId++);
        }

        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(),employee);

    }

    // 查询全部员工
    public Collection<Employee> getAll() {
    
    
        return employees.values();
    }

    // 通过ID查询员工
    public Employee getEmployeeById(Integer id) {
    
    
        return employees.get(id);
    }

    // 删除员工
    public void delete(Integer id) {
    
    
        employees.remove(id);
    }
}

测试1
在这里插入图片描述
配置首页

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    
    
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
    
    
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zs18753479279/article/details/112546692