Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

SpringBoot simulates the employee database and implements the operation method of adding, deleting, modifying and querying.

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about the SpringBoot simulation staff database and the operation methods of adding, deleting, modifying and querying. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

1: first create a pojo layer to define the data in it

Department Department:

Package com.example.springbootweb.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;/** * @ author ${Fan Tao} * @ Description * @ create 2021-09-19 10:25 * / @ Data@AllArgsConstructor@NoArgsConstructorpublic class Department {private Integer id; private String department;}

Employee Department:

Package com.example.springbootweb.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;/** * @ author ${Fan Tao} * @ Description * @ create 2021-09-19 10:26 * / @ Data@AllArgsConstructor@NoArgsConstructorpublic class Employee {private Integer id; private String lastname; private String email; private Integer gender; / / 0 for female 1 for male private Department department; private Data birth;}

2: write dao layer injection data:

Departmental level:

Package com.example.springbootweb.dao;import com.example.springbootweb.pojo.Department;import java.util.Collection;import java.util.HashMap;import java.util.Map;/** * @ author ${Fan Tao} * @ Description * @ create 2021-09-19 10:28 * / / departmental daopublic class DepartmentDao {/ / data in the simulation database private static Map department = null; static {department = new HashMap () / create a department table department.put (101MagneNew Department); department.put (102Magi new Department); department.put (103Magi new Department (101C); department.put (104Jing new Department (101O); department.put (105J new Department (101J)) } / / get all department information public Collection getDepartment () {return department.values ();} / / get department public Department getDepartment (Integer id) {return department.get (id);}} through id

Employee level:

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 ${Fan Tao} * @ Description * @ create 2021-09-19 10:44 * / @ Repositorypublic class EmployeeDao {/ / data in the simulation database private static Map employees = null; / / employee has a department @ Autowired private DepartmentDao departmentDao; static {employees = new HashMap () Employees.put (1001 AA new Employee (1001, "AA", "2831826106@qq.com", 1je new Department (101, "Teaching Department")); employees.put (1002 new Department (1002, "BB", "2831826106@qq.com"); employees.put (1003 new Employee (1003, "CC", "2831826106@qq.com")) Employees.put (1004 new Employee (1004, "DD", "2831826106@qq.com", 1 private static Integer ininID new Department (101, "Operations")); employees.put (1005 new Department (1005, "EE", "2831826106@qq.com");} / / main key self-increasing private static Integer ininID = 1006 / / add an employee public void save (Employee employee) {if (employee.getId ()) = = null) {employee.setId (ininID++);} employee.setDepartment (departmentDao.getDepartmentByid (employee.getDepartment (). GetId (); employees.put (employee.getId (), employee) } / / query all employees public Collection getAll () {return employees.values ();} / / query employee public Employee getEmployeeByid (Integer id) {return employees.get (id) through ID;} / / delete employee drag ID public void delete (Integer id) {employees.remove (id);}}

Departmental level

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 ${Fan Tao} * @ Description * @ create 2021-09-19 10:28 * / / departmental dao@Repositorypublic class DepartmentDao {/ / data in the simulation database private static Map department = null; static {department = new HashMap () / create a department table department.put (101MagneNew Department); department.put (102Magi new Department); department.put (103Magi new Department (101C); department.put (104Jing new Department (101O); department.put (105J new Department (101J)) } / / get all department information public Collection getDepartmentByid () {return department.values ();} / / get department public Department getDepartmentByid (Integer id) {return department.get (id) through id;}} the above is the operation method of simulating employee database and adding, deleting, modifying and querying SpringBoot that the editor shared for you. If you happen to have similar doubts, please refer to the above analysis for understanding. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report