溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot模擬員工數(shù)據(jù)庫并實(shí)現(xiàn)增刪改查的操作方法

發(fā)布時(shí)間:2021-09-24 15:11:31 來源:億速云 閱讀:117 作者:柒染 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)SpringBoot模擬員工數(shù)據(jù)庫并實(shí)現(xiàn)增刪改查的操作方法,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

1:首先創(chuàng)建一個pojo層在里面定義數(shù)據(jù)

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

SpringBoot模擬員工數(shù)據(jù)庫并實(shí)現(xiàn)增刪改查的操作方法

SpringBoot模擬員工數(shù)據(jù)庫并實(shí)現(xiàn)增刪改查的操作方法

2:編寫dao層注入數(shù)據(jù):

部門層:

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 {
    //模擬數(shù)據(jù)庫中的數(shù)據(jù)

    private  static Map<Integer, Department> department = null;
    static {
        department = new HashMap<Integer,Department>(); //創(chuàng)建一個部門表

        department.put(101,new Department(101,"教學(xué)部"));
        department.put(102,new Department(101,"教研部"));
        department.put(103,new Department(101,"市場部"));
        department.put(104,new Department(101,"運(yùn)營部"));
        department.put(105,new Department(101,"清潔部"));
    }

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

        return  department.values();

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

    }

}

SpringBoot模擬員工數(shù)據(jù)庫并實(shí)現(xiàn)增刪改查的操作方法

員工層:

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 {

    //模擬數(shù)據(jù)庫中的數(shù)據(jù)
    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,"教學(xué)部")));
        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,"運(yùn)營部")));
        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);
        
    }
    //查詢?nèi)繂T工
    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 {
    //模擬數(shù)據(jù)庫中的數(shù)據(jù)

    private  static Map<Integer, Department> department = null;
    static {
        department = new HashMap<Integer,Department>(); //創(chuàng)建一個部門表

        department.put(101,new Department(101,"教學(xué)部"));
        department.put(102,new Department(101,"教研部"));
        department.put(103,new Department(101,"市場部"));
        department.put(104,new Department(101,"運(yùn)營部"));
        department.put(105,new Department(101,"清潔部"));
    }

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

        return  department.values();

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

    }

}

上述就是小編為大家分享的SpringBoot模擬員工數(shù)據(jù)庫并實(shí)現(xiàn)增刪改查的操作方法了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI