springboot-项目实战:准备阶段
1 搭建一个springboot项目
2 导入静态资源文件
文件地址:员工管理系统静态资源文件下载
导入的位置:
3 初步创建包结构
4 在pom.xml中导入lombok
<!--导入lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
5 在pojo层编写两个实体类
Department.java
package com.lv.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//部门表
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Department {
private Integer id;
private String departmentName;
}
Employee.java
package com.lv.pojo;
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 date;
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.date = new Date();
}
}
6 在dao层编写实体类对应的操作类
DepartmentDao.java
package com.lv.dao;
import com.lv.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> departmentMap = null;
static {
departmentMap = new HashMap<Integer, Department>(); //创建一个部门表
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,"后勤部"));
}
//获得所有部门信息
public Collection<Department> getAllDepartment(){
return departmentMap.values();
}
//通过id得到部门
public Department getDepartmentById(Integer id){
return departmentMap.get(id);
}
}
EmployeeDao.java
package com.lv.dao;
import com.lv.pojo.Department;
import com.lv.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> employeeMap = null;
//员工所属的部门
@Autowired
private DepartmentDao departmentDao;
static {
employeeMap = new HashMap<Integer,Employee>();//创建一个员工表
employeeMap.put(1001,new Employee(1001,"AA","AA@qq.com",0,new Department(101,"教学部")));
employeeMap.put(1002,new Employee(1002,"BB","BB@qq.com",1,new Department(102,"市场部")));
employeeMap.put(1003,new Employee(1003,"CC","CC@qq.com",1,new Department(103,"教研部")));
employeeMap.put(1004,new Employee(1004,"DD","DD@qq.com",0,new Department(104,"运营部")));
employeeMap.put(1005,new Employee(1005,"EE","EE@qq.com",1,new Department(105,"后勤部")));
}
//主键自增
private static Integer initId = 1006;
//增加一个员工
public void employeeAdd(Employee employee){
if (employee.getId()==null){
employee.setId(initId++);
}
employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
employeeMap.put(employee.getId(),employee);
}
//查询全部员工信息
public Collection<Employee> getAllEmployee(){
return employeeMap.values();
}
//通过id查询员工
public Employee getEmployeeById(Integer id){
return employeeMap.get(id);
}
//删除员工
public void employeeDelete(Integer id){
employeeMap.remove(id);
}
}