当前位置:首页 » 《随便一记》 » 正文

SpringBoot模拟一个员工数据库并且实现增删改查_justleavel的博客

3 人参与  2021年12月19日 11:26  分类 : 《随便一记》  评论

点击全文阅读


1:首先创建一个pojo层在里面定义数据

Department部门:

package com.example.springbootweb.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-09-19 10:25
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
    private  Integer id;
    private String department;
}

Employee部门:

package com.example.springbootweb.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-09-19 10:26
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
    private Integer id;
    private  String lastname;
    private  String email;
    private  Integer gender; //0代表女 1代表男
    private  Department department;
    private Data birth;
}

请添加图片描述
请添加图片描述

2:编写dao层注入数据:

部门层:

package com.example.springbootweb.dao;

import com.example.springbootweb.pojo.Department;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-09-19 10:28
 */
//部门dao
public class DepartmentDao {
    //模拟数据库中的数据

    private  static Map<Integer, Department> department = null;
    static {
        department = new HashMap<Integer,Department>(); //创建一个部门表

        department.put(101,new Department(101,"教学部"));
        department.put(102,new Department(101,"教研部"));
        department.put(103,new Department(101,"市场部"));
        department.put(104,new Department(101,"运营部"));
        department.put(105,new Department(101,"清洁部"));
    }

    //获得所有部门信息
    public Collection<Department> getDepartment(){

        return  department.values();

    }
    //通过id得到部门
    public  Department getDepartment(Integer id){
        return  department.get(id);

    }

}

请添加图片描述
员工层:

package com.example.springbootweb.dao;

import com.example.springbootweb.pojo.Department;
import com.example.springbootweb.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;

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-09-19 10:44
 */
@Repository
public class EmployeeDao {

    //模拟数据库中的数据
    private  static Map<Integer, Employee> employees = null;
    //员工有所属的部门
    @Autowired
    private  DepartmentDao departmentDao;

    static {
        employees = new  HashMap<Integer,Employee>();

        employees.put(1001,new Employee(1001,"AA","2831826106@qq.com",1,new Department(101,"教学部")));
        employees.put(1002,new Employee(1002,"BB","2831826106@qq.com",1,new Department(101,"教研部")));
        employees.put(1003,new Employee(1003,"CC","2831826106@qq.com",1,new Department(101,"市场部")));
        employees.put(1004,new Employee(1004,"DD","2831826106@qq.com",1,new Department(101,"运营部")));
        employees.put(1005,new Employee(1005,"EE","2831826106@qq.com",1,new Department(101,"清洁部")));

    }
    
    //主键自增
    private  static  Integer ininID = 1006;
    // 增加一个员工
    public  void  save(Employee employee){
        if (employee.getId()== null){
            employee.setId(ininID++);
        }
        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);
        
    }
    //删除员工拖过ID
    public  void  delete(Integer id){
        employees.remove(id);
    }
    


}

部门层

package com.example.springbootweb.dao;

import com.example.springbootweb.pojo.Department;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-09-19 10:28
 */
//部门dao
@Repository
public class DepartmentDao {
    //模拟数据库中的数据

    private  static Map<Integer, Department> department = null;
    static {
        department = new HashMap<Integer,Department>(); //创建一个部门表

        department.put(101,new Department(101,"教学部"));
        department.put(102,new Department(101,"教研部"));
        department.put(103,new Department(101,"市场部"));
        department.put(104,new Department(101,"运营部"));
        department.put(105,new Department(101,"清洁部"));
    }

    //获得所有部门信息
    public Collection<Department> getDepartmentByid(){

        return  department.values();

    }
    //通过id得到部门
    public  Department getDepartmentByid(Integer id){
        return  department.get(id);

    }

}


点击全文阅读


本文链接:http://zhangshiyu.com/post/31721.html

部门  员工  市场部  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1