13.1 静态资源下载
https://download.csdn.net/download/no996yes885/88151513
13.2 静态资源位置
css样式文件放在static的css目录下;static的img下放图片;template目录下放其余的html文件。
13.3 创建两个实体类
导入依赖:lombok
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
加入@Data注解:自动创建set、get等方法;
加入@AllArgsConstructor注解:自动创建有参构造方法;
加入NoArgsConstructor注解:自动创建无参构造方法;
Department:
package jiang.com.springbootstudy.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
private Integer id;
private String departmentName;
}
Employee:
package jiang.com.springbootstudy.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 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(); // 一创建对象调用有参的话,自动生成了日期属性
}
}
13.4 创建实体类对应的dao类
DepartmentDao:
注意,这里类中的加载顺序,静态的属性和代码要先加载,然后再加载其他的。使用静态代码块结合Map来模拟数据库,数据库的删查使用的是remove、values、get方法。
package jiang.com.springbootstudy.dao;
import jiang.com.springbootstudy.pojo.Department;
import lombok.Data;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Data
@Repository
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:
package jiang.com.springbootstudy.dao;
import jiang.com.springbootstudy.pojo.Department;
import jiang.com.springbootstudy.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;
@Repository //自动注入需要的注解
public class EmployeeDao {
private static Map<Integer, Employee> employees = null;
// 员工所属的部门
// 放数据,先加载静态的东西,比如说静态的遍历或者静态的代码
@Autowired //自动注入
private DepartmentDao departmentDao;
static {
employees = new HashMap<Integer, Employee>();//前面只是定义了成员变量的类型,并没有赋值,null无法调用put方法,所以要创建对象。
employees.put(1001,new Employee(1001,"AA","A24736743@qq.com",0,new Department(101,"教学部")));
employees.put(1002,new Employee(1002,"AA","B24736743@qq.com",1,new Department(101,"市场部")));
employees.put(1003,new Employee(1003,"AA","C24736743@qq.com",0,new Department(101,"教研部")));
employees.put(1004,new Employee(1004,"AA","D24736743@qq.com",1,new Department(101,"运营部")));
employees.put(1005,new Employee(1005,"AA","E24736743@qq.com",0,new Department(101,"后勤部")));
}
// 主键自增
private static Integer initId = 1006; // static和没有有什么区别?
// 增加员工
public void save(Employee employee){
if (employee.getId()==null){
employee.setId(initId++);//自增变量的值也会改变
}
employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId())); //这句话可以忽略,原理是调用了接口的getDepartmentById方法传入从员工的部门属性获取的部门id得到一个部门,然后把部门属性赋值给了员工
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);
}
}