SpringBoot项目实战-准备工作

文章目录

SpringBoot项目实战-准备工作

一、springboot准备工作

1.新建一个springboot项目;

2.导入静态资源,html静态资源模板放在templates目录下,js、css、img等静态资源放在static目录下;

3.创建员工和部门两个实体类:

//员工
import java.util.Date;
@Data
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();
    }
}
//部门
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
    private Integer id;
    private String departmentName;
}

4.新建一个dao包,在dao包下模拟数据,并添加相关的方法:

  • 部门数据
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,"后勤部"));
        departments.put(106,new Department(106,"财务部"));
    }

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

    //通过id获得部门信息
    public Department getDepartmentById(Integer id){
        return departments.get(id);
    }
}
  • 员工数据
@Repository
public class EmployeeDao {
    //模拟数据库中的数据
    private static Map<Integer, Employee> employees = null;

    //员工所属的部门
    private DepartmentDao departmentDao;

    static {
        employees = new HashMap<>();
        employees.put(2020001, new Employee(2020001, "张五哥", "149u122@qq.com", 1, new Department(101, "销售部")));
        employees.put(2020002, new Employee(2020002, "常二春", "149u122@qq.com", 1, new Department(101, "销售部")));
        employees.put(2020003, new Employee(2020003, "李二蛋", "149u122@qq.com", 1, new Department(101, "销售部")));
        employees.put(2020004, new Employee(2020004, "牛二花", "149u122@qq.com", 0, new Department(101, "销售部")));


    }
    //设置主键自增
    private static Integer getId = 2020005;
    //增添新员工
    public void save(Employee employee){
        if(employee.getId()==null){
            employee.setId(getId++);
        }

        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));

        employees.put(employee.getId(),employee);
    }

    //查询全部员工信息
    public Collection<Employee> getEmployees(){
        return employees.values();
    }

    //通过id查询
    public Employee getEmployeeById(Integer id){
        return employees.get(id);
    }

    //删除员工
    public void delEmployee(Integer id){
        employees.remove(id);
    }
}

5.首页实现

在项目目录下新建一个config包,在包下创建一个MyMvcConfig类,在这个配置类中配置首页:

//应为类型要求为WebMvcConfigurer,所以我们实现其接口
//可以使用自定义类扩展MVC的功能
@Controller
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 浏览器发送/index , 就会跳转到index页面;
        registry.addViewController("/index").setViewName("index");
    }
}

启动项目,访问localhost:8080可以看到首页index.html

上一篇:MySQL之多表查询


下一篇:oracle之多表查询