SpringBoot模拟员工数据库并实现增删改查操作
程序员文章站
2022-07-10 13:50:01
目录1:首先创建一个pojo层在里面定义数据department部门:package com.example.springbootweb.pojo;import lombok.allargsconst...
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); } }
3:总结
到此这篇关于springboot模拟员工数据库并实现增删改查操作的文章就介绍到这了,更多相关springboot模拟数据库增删改查内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 软文广告的形式有哪些?怎么做软文营销