本次实战项目主要是借鉴b站上的视频资源【狂神说Java】SpringBoot最新教程IDEA版通俗易懂完成的,有需求的话,可以直接去b站观看完整的视频教程,本文若有不对之处,望不吝赐教,谢谢~
(2)导入静态资源
静态资源下载
注意本项目暂时是采用模拟数据库的方法来获取数据,未用到数据库的相关操作。
(1)引入lombok依赖,注意还需要安装lombok插件。
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
(2)创建部门类
Department.java
package com.example.employee_management.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @className: Department
* @description: 部分信息类
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
/**
* 部门id
*/
private Integer departmentId;
/**
* 部门名
*/
private String departmentName;
}
(3)创建员工类
Employee.java
package com.example.employee_management.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
/**
* @className: Employee
* @description: 员工信息类
*/
@Data
@NoArgsConstructor
public class Employee {
/**
* 员工id
*/
private Integer employeeId;
/**
* 员工姓名
*/
private String employeeName;
/**
* 员工邮箱
*/
private String email;
/**
* 员工性别 0-女 1-男
*/
private Integer gender;
/**
* 员工部门
*/
private Department department;
/**
* 员工生日 默认为创建日期
*/
private Date birth;
public Employee(Integer employeeId, String employeeName, String email, Integer gender, Department department) {
this.employeeId = employeeId;
this.employeeName = employeeName;
this.email = email;
this.gender = gender;
this.department = department;
//默认生日为创建日期
this.birth = new Date();
}
}
(1)部门dao
DepartmentDAO.java
package com.example.employee_management.dao;
import com.example.employee_management.pojo.Department;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* @className: DepartmentDao
* @description: 部门dao
*/
@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,"后勤部"));
}
/**
* 获得所有部门信息
* @return
*/
public Collection<Department> getAllDepartments(){
return departmentMap.values();
}
/**
* 通过id得到部门
* @param departmentId
* @return
*/
public Department getDepartmentById(Integer departmentId){
return departmentMap.get(departmentId);
}
}
(2)员工dao
EmployeeDao.java
package com.example.employee_management.dao;
import com.example.employee_management.pojo.Department;
import com.example.employee_management.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;
/**
* @className: EmployeeDao
* @description: 员工dao
*/
@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,"a","[email protected]",1,new Department(101,"教学部")));
employeeMap.put(1002,new Employee(1002,"b","[email protected]",1,new Department(101,"市场部")));
employeeMap.put(1003,new Employee(1003,"c","[email protected]",1,new Department(101,"教研部")));
employeeMap.put(1004,new Employee(1004,"d","[email protected]",1,new Department(101,"运营部")));
employeeMap.put(1005,new Employee(1005,"e","[email protected]",1,new Department(101,"后勤部")));
}
/**
* 员工id,相当于数据库中的主键
*/
private static Integer initId=1006;
/**
* 保存新增员工
* @param employee
*/
public void save(Employee employee){
//id自增
if(employee.getEmployeeId()==null){
employee.setEmployeeId(initId++);
}
//设置部门
employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getDepartmentId()));
//放入容器中去
employeeMap.put(employee.getEmployeeId(),employee);
}
/**
* 得到所有员工信息
* @return
*/
public Collection<Employee> getAllEmployees(){
return employeeMap.values();
}
/**
* 通过id得到员工信息
* @param employeeId
* @return
*/
public Employee getEmployeeById(Integer employeeId){
return employeeMap.get(employeeId);
}
/**
* 通过id删除员工信息
* @param employeeId
*/
public void deleteEmployeeById(Integer employeeId){
employeeMap.remove(employeeId);
}
}
本篇文章主要是介绍项目的准备工作。需要注意的主要有两个地方:
lombok
在这里使用lombok生成类的set/get方法以提高开发效率。
模拟数据库
按道理应结合mybatis直接使用数据库的,但是这里是采用模拟数据库的方法,不使用数据库,而用程序生成数据,在实际开发中是不可取的。