员工管理系统
1、准备工作
静态资源:链接:https://pan.baidu.com/s/1qtUDuJNVupr872kVDO-veg
提取码:fabo
gitee:https://gitee.com/planting-trees-chen/spring-boot
新建springboot项目
导入jar包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.9.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
</dependencies>
添加静态资源
数据库文件,模拟
实体类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;
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();
}
}
dao层
//部门dao
@Repository
public class DepartmentDao {
//模拟数据库数据
private static Map<Integer, Department> departments = null;
static {
departments = new HashMap<>();
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> getDepartment() {
return departments.values();
}
//通过ID的到部门
public Department getDepartmentById(Integer id) {
return departments.get(id);
}
}
//员工dao
@Repository
public class EmployeeDao {
private static Map<Integer, Employee> employees = null;
//员工所属部门
@Autowired
private DepartmentDao departmentDao;
static {
employees = new HashMap<>(); //创建部门表
employees.put(1001, new Employee(1001, "AA", "[email protected]", 1, new Department(101, "教学部")));
employees.put(1002, new Employee(1002, "BB", "[email protected]", 0, new Department(102, "市场部")));
employees.put(1003, new Employee(1003, "CC", "[email protected]", 1, new Department(103, "教研部")));
employees.put(1004, new Employee(1004, "DD", "[email protected]", 0, new Department(104, "运营部")));
employees.put(1005, new Employee(1005, "EE", "[email protected]", 1, new Department(105, "后勤部")));
}
//主键自增
private static Integer initId = 1006;
//添加员工
public void save(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);
}
}
MyMvcConfig配置视图
@Configuration
public class MyMvcConfig implements WebMvcConfigurer