SpringBoot模擬員工數據庫並實現增刪改查操作
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","[email protected]",1,new Department(101,"教學部"))); employees.put(1002,new Employee(1002,"BB","[email protected]",1,new Department(101,"教研部"))); employees.put(1003,new Employee(1003,"CC","[email protected]",1,new Department(101,"市場部"))); employees.put(1004,new Employee(1004,"DD","[email protected]",1,new Department(101,"運營部"))); employees.put(1005,new Employee(1005,"EE","[email protected]",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); } }
3:總結
到此這篇關於SpringBoot模擬員工數據庫並實現增刪改查操作的文章就介紹到這瞭,更多相關SpringBoot模擬數據庫增刪改查內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- SpringMVC RESTFul實體類創建及環境搭建
- 強烈推薦IDEA提高開發效率的必備插件
- 深入淺出理解Java Lambda表達式之四大核心函數式的用法與范例
- SpringBoot2 JPA解決懶加載異常的問題
- Springboot-Management的項目實踐