SpringBoot模拟员工数据库并实现增删改查操作
更新时间:2021年9月20日 16:00 点击:1674
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模拟数据库增删改查内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!
相关文章
- 操作类就是把一些常用的一系列的数据库或相关操作写在一个类中,这样调用时我们只要调用类文件,如果要执行相关操作就直接调用类文件中的方法函数就可以实现了,下面整理了...2016-11-25
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题
这篇文章主要介绍了解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-04-28- 这篇文章主要介绍了Intellij IDEA连接Navicat数据库的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借价值,需要的朋友可以参考下...2021-03-25
- 在开发过程中,我们经常会将日期时间的毫秒数存放到数据库,但是它对应的时间看起来就十分不方便,我们可以使用一些函数将毫秒转换成date格式。 一、 在MySQL中,有内置的函数from_unixtime()来做相应的转换,使用如下: 复制...2014-05-31
- 这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
- C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
- 通过内网连另外一台机器的mysql服务, 确发现速度N慢! 等了大约几十秒才等到提示输入密码。 但是ping mysql所在服务器却很快! 想到很久之前有过类似的经验, telnet等一些服务在连接请求的时候,会做一些反向域名解析(如果...2015-10-21
- 某些时候,例如为了搭建一个测试环境,或者克隆一个网站,需要复制一个已存在的mysql数据库。使用以下方法,可以非常简单地实现。假设已经存在的数据库名字叫db1,想要复制一份,命名为newdb。步骤如下:1. 首先创建新的数据库newd...2015-10-21
- mysqldump命令的用法1、导出所有库系统命令行mysqldump -uusername -ppassword --all-databases > all.sql 2、导入所有库mysql命令行mysql>source all.sql; 3、导出某些库系统命令行mysqldump -uusername -ppassword...2015-10-21
- 1005:创建表失败1006:创建数据库失败1007:数据库已存在,创建数据库失败1008:数据库不存在,删除数据库失败1009:不能删除数据库文件导致删除数据库失败1010:不能删除数据目录导致删除数据库失败1011:删除数据库...2013-09-23
- 这篇文章主要介绍了详解springBoot启动时找不到或无法加载主类解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
- 这篇文章主要介绍了SpringBoot集成Redis实现消息队列的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-10
- 这篇文章主要介绍了c#从数据库里取得数据并异步更新ui的方法,大家参考使用吧...2020-06-25
- 这篇文章主要介绍了解决Springboot get请求是参数过长的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-17
- yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2使用中的一些基本的增删改查操作。 User::find()->all(); //返回所有用户数据; User::findOne($id); //返回 主键...2015-11-24
Spring Boot项目@RestController使用重定向redirect方式
这篇文章主要介绍了Spring Boot项目@RestController使用重定向redirect方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-02- 这篇文章主要介绍了Springboot+TCP监听服务器搭建过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-10-28
- 这篇文章主要介绍了springBoot 项目排除数据库启动方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-10