码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot搭建一个简单员工管理系统 (1)准备工作

时间:2021-06-13 09:49:59      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:set   管理系统   class   import   mvc   技术   教学   静态资源   str   

导入静态资源

从网上找到一个模板下载后,将网页放在template文件夹 css资源放在static文件夹下
技术图片

伪造数据库

由于一开始写项目就导入数据库并不是很方便,可以通过代码伪造一个数据库
定义员工 部门类

package com.jie.pojo;

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

//部门表
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
    private Integer id;
    private String departmentName;
}
package com.jie.pojo;

import lombok.AllArgsConstructor;
import lombok.Builder;
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 birth;

    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.birth =new Date();
    }
}

通过java内置的方法模拟数据库

package com.jie.dao;

import com.jie.pojo.Department;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
//它用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean。具体只需将该注解标注在 DAO类上即可。
public class DepartmentDAO {
    private static Map<Integer, Department> department=null;
    //模拟数据库
    static {
        department=new HashMap<>();
        department.put(101,new Department(101,"教学部"));
        department.put(102,new Department(102,"市场部"));
        department.put(103,new Department(103,"教研部"));
        department.put(104,new Department(104,"运营部"));
        department.put(105,new Department(105,"后勤部"));
    }
    //获得所有部门信息
    public Collection<Department> getDepartment(){
        return department.values();
    }
    //通过id得到部门
    public Department getDepartmentByid(Integer id){
        return department.get(id);
    }
}
package com.jie.dao;

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

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Repository
public class EmployeeDAO {
    private static Map<Integer, Employee> employees;
    //员工所有的部门
    @Autowired
    private DepartmentDAO departmentDAO;
    static {
        employees=new HashMap<>();
        employees.put(1001,new Employee(1001,"小A","123@qq.com",1,new Department(101,"教学部")));
        employees.put(1002,new Employee(1002,"小B","124@qq.com",0,new Department(102,"市场部")));
        employees.put(1003,new Employee(1003,"小C","125@qq.com",1,new Department(103,"教研部")));
        employees.put(1004,new Employee(1004,"小D","126@qq.com",0,new Department(104,"运营部")));
        employees.put(1005,new Employee(1005,"小E","127@qq.com",1,new Department(105,"后勤部")));
    }
    private static Integer initid=1005;
    public void save(Employee employee){
        if(null==employee.getId()){
            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);
    }
    //通过id删除
    public void deleteByid(Integer id){
        employees.remove(id);
    }
}

配置首页跳转

写一个自定义的控制类

package com.jie.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
    }
}

SpringBoot搭建一个简单员工管理系统 (1)准备工作

标签:set   管理系统   class   import   mvc   技术   教学   静态资源   str   

原文地址:https://www.cnblogs.com/OfflineBoy/p/14877711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!