首页 > 其他分享 >230125_50_SpringBoot入门

230125_50_SpringBoot入门

时间:2023-01-25 21:12:58浏览次数:59  
标签:SpringBoot bill 50 private 230125 new Department import com

  • SpringBoot实战:员工管理系统

  • 1.静态资源导入

可以从百度网盘获取资源:链接: https://pan.baidu.com/s/1x-6U_NCNEhIXOq0CcvRW-g 提取码: mg94 复制这段内容后打开百度网盘手机App,操作更方便哦

将资源导入到resources目录下对应的目录,将html导入到templates目录;将img,css和js导入到static目录,如下图

  • 2.导入lombok依赖

  • 导入依赖

<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <version>1.18.4</version>
   <scope>provided</scope>
</dependency>

相关注解:

@Data 注解的主要作用是提高代码的简洁,使用这个注解可以省去代码中大量的get()、 set()、 toString()等方法;注在类上,提供类的get、set、equals、hashCode、canEqual、toString方法

@AllArgsConstructor : 注在类上,提供类的全参构造
@NoArgsConstructor : 注在类上,提供类的无参构造

安装lombok插件,安装插件后不报错。

  • 3.准备工作
  • 新建Department类
package com.bill.pojo;

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

/**
 * @Auther: wangchunwen
 * @Date: 2023/1/25 - 01 - 25 - 17:33
 * @Description: com.bill.pojo
 * @version: 1.0
 */

// 部门表

@Data
@AllArgsConstructor // 有参构造
@NoArgsConstructor // 无参构造
public class Department {
    private Integer id;
    private String departmentName;

}
  • 新建DeparmentDao
package com.bill.dao;

import com.bill.pojo.Department;

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

/**
 * @Auther: wangchunwen
 * @Date: 2023/1/25 - 01 - 25 - 18:02
 * @Description: com.bill.dao
 * @version: 1.0
 */
// 部门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,"运维部"));
    }

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

    // 通过id获取部门信息
    public Department getDepartmentById(Integer id){
        return departments.get(id);
    }


}
  • 新建Employee类
package com.bill.pojo;

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

import java.util.Date;

/**
 * @Auther: wangchunwen
 * @Date: 2023/1/25 - 01 - 25 - 17:59
 * @Description: com.bill.pojo
 * @version: 1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
    private Integer id;
    private String lastName;
    private String email;
    private Integer gender; // 0:女;1:男

    private Department department;
    private Date birth;
}
  • 新建EmployeeDao
package com.bill.dao;

import com.bill.pojo.Department;
import com.bill.pojo.Employee;

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

/**
 * @Auther: wangchunwen
 * @Date: 2023/1/25 - 01 - 25 - 20:30
 * @Description: com.bill.dao
 * @version: 1.0
 */

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,"bill","[email protected]",0,new Department(101,"技术部"),new Date(2023/01/24)));
        employees.put(1002,new Employee(1002,"tom","[email protected]",1,new Department(101,"产品部"),new Date(2023/01/23)));
        employees.put(1003,new Employee(1003,"jack","[email protected]",1,new Department(101,"财务部"),new Date(2023/01/22)));
        employees.put(1004,new Employee(1004,"rose","[email protected]",0,new Department(101,"运维部"),new Date(2023/01/21)));
    }

    // 主键自增!
    private static Integer initId = 1006;

    // 增加一个员工
    public void save(Employee employee){
        if (employee.getId() == null){
            employee.setId(initId++);
        }

        employee.setDepartment(departmentDao.getDepartmentById(employee.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 remove(Integer id){
        employees.remove(id);
    }

}

标签:SpringBoot,bill,50,private,230125,new,Department,import,com
From: https://www.cnblogs.com/wcwblog/p/17067285.html

相关文章