13 datos de preparación de proyectos Springboot y clases dao

13.1 Descarga de recursos estáticos

https://download.csdn.net/download/no996yes885/88151513

13.2 Ubicación de recursos estáticos

        Los archivos de estilo CSS se colocan en el directorio CSS estático; la img estática se coloca debajo de la imagen; el directorio de plantilla se coloca debajo del resto de los archivos html. 

        

13.3 Crear dos clases de entidad

        Dependencias de importación: lombok

<!--lombok-->
<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
</dependency>

        Agregue la anotación @Data: cree automáticamente set, get y otros métodos;

        Agregue la anotación @AllArgsConstructor: cree automáticamente métodos constructores parametrizados;

        Agregue la anotación NoArgsConstructor: cree automáticamente un constructor sin argumentos;

         Departamento:

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;
}

        Empleado:

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 Crear la clase dao correspondiente a la clase de entidad

        DepartamentoDao:

        Tenga en cuenta que en el orden de carga de esta clase, las propiedades estáticas y el código deben cargarse primero y luego los demás. Utilice bloques de código estático combinados con Map para simular la base de datos. Los métodos eliminar, valores y obtener se utilizan para eliminar y consultar la base de datos.

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);
    }
}

         EmpleadoDao:

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","[email protected]",0,new Department(101,"教学部")));
        employees.put(1002,new Employee(1002,"AA","[email protected]",1,new Department(101,"市场部")));
        employees.put(1003,new Employee(1003,"AA","[email protected]",0,new Department(101,"教研部")));
        employees.put(1004,new Employee(1004,"AA","[email protected]",1,new Department(101,"运营部")));
        employees.put(1005,new Employee(1005,"AA","[email protected]",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);
    }

}

Supongo que te gusta

Origin blog.csdn.net/no996yes885/article/details/132068864
Recomendado
Clasificación