springboot员工管理系统:1.前期准备工作

springboot员工管理系统:前期准备工作

本项目参考狂神老师的springboot项目,项目具体讲解视频请参考:
https://www.bilibili.com/video/BV1PE411i7CV?t=5&p=20

第一步自然是先创建一个干净的springboot项目,如何创建前面的博客已经详细介绍~

1、导入依赖

写项目的第一步就是先检查项目的配置文件有没有问题,并导入我们需要的依赖

<dependencies>
   <!--web依赖-->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>

   <!--测试依赖-->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
   </dependency>

   <!--模板引擎依赖-->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
   </dependency>

   <!--lombok依赖-->
   <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
   </dependency>
</dependencies>
2、pojo实体类

Department类

//部门表
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
    private Integer id;
    private String departmentName;
}

Employee类

//员工表
@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();
    }
}
3、Dao层

实体类编写完毕后,我们来编写对实体类进行操作的Dao层,由于我没有先行编写设计数据库,所以我们在Dao层中用java代码以伪数据的方式来模拟数据库的操作。

DepartmentDao

//部门Dao
@Repository//spring接管Dao
public class DepartmentDao {

    //当我们没有数据库时,可以采用以下方式模拟数据库
    private static Map<Integer, Department> departments = null;

    static {
        departments = new HashMap<Integer, Department>();//创建一个部门表
        departments.put(101, new Department(101, "教学部"));
        departments.put(102, new Department(102, "市场部"));
        departments.put(103, new Department(103, "教研部"));
        departments.put(104, new Department(104, "运营部"));
        departments.put(105, new Department(105, "后勤部"));
    }

    //获得所有部门信息
    public Collection<Department> getDepartments() {
        return departments.values();
    }

    //通过id得到部门
    public Department getDepartmentByID(Integer id) {
        return departments.get(id);
    }
}

EmployeeDao

//员工Dao
@Repository//spring接管Dao
public class EmployeeDao {

    //当我们没有数据库时,可以采用以下方式模拟数据库
    private static Map<Integer, Employee> employees = null;

    //员工应当有所属的部门
    @Autowired//令spring托管Dao之后需要加这个注解,否则会报空指针
    private DepartmentDao departmentDao;

    static {//静态代码块会优先加载
        employees = new HashMap<Integer, Employee>();//创建一个部门表

        employees.put(1001, new Employee(1001, "hsyA", "A1416555145@qq.com", 0, new Department(101, "教学部")));
        employees.put(1002, new Employee(1002, "hsyB", "B1416555145@qq.com", 1, new Department(102, "市场部")));
        employees.put(1003, new Employee(1003, "hsyC", "C1416555145@qq.com", 0, new Department(103, "教研部")));
        employees.put(1004, new Employee(1004, "hsyD", "D1416555145@qq.com", 1, new Department(104, "运营部")));
        employees.put(1005, new Employee(1005, "hsyE", "E1416555145@qq.com", 0, new Department(105, "后勤部")));
    }

    //增加一个员工(主键自增!!!)
    private static Integer initID=1006;
    public void add(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);
    }
}
4、导入静态资源

然后我们就可以把静态资源导入进项目,作为一个后端程序员,我们自然是不会花太多时间放在页面上,为了响应“不要重复造轮子”的号召,我们从bootstrap上找了一些个模板嘿嘿,大家也可以去找自己喜欢的,这些是我用的,非常普通,如果有想要的可以私聊我

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

注意!!!

1、css,js等放在static文件夹下

2、html 放在 templates文件夹下

最终的项目结构应当如下图:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好汤圆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值