Spring Boot——增删查改(1)
2019-07-15 本文已影响0人
草样逍遥
Spring Boot增删查改
标签: SpringBoot
增删查改是最为基础的功能,在很多场景下都有运用。本文对增删查改功能进行简单的编写,满足一定的题目需求。
题目设置
本题要求使用Spring boot来完成对Employee CURD(增删改查)
API接口的实现。
- 要求每一个
Employee
包含id
、name
、age
、gender
四个字段。 - 至少需要完成查询所有
Employee
的API接口,并返回一个包含所有Employee
的JSON,要求接口路径:http://localhost:8080/employees
- 要求使用Postman来向目标接口路径发送请求,获取Response,Response JSON格式如下:
[
{
"id": 0,
"name": "小明",
"age": 20,
"gender": "男"
},
{
"id": 1,
"name": "小红",
"age": 19,
"gender": "女"
},
{
"id": 2,
"name": "小智",
"age": 15,
"gender": "男"
},
{
"id": 3,
"name": "小刚",
"age": 16,
"gender": "男"
},
{
"id": 4,
"name": "小霞",
"age": 15,
"gender": "女"
}
]
- 将结果截图并回复
- 完成增删查改的所有接口
环境描述
- java8
- Intellij-IDEA
- Linux
模型介绍
使用Spring Boot来进行开发,可以参照三层模型:DAO、Service和Controller层。下面,对这三层的意义的作用进行介绍。
- DAO:DAO层主要是做数据持久层的工作,负责与数据库进行联络的任务封装于此。通常情况下,会先设计DAO的接口,然后,定义此接口的实现类。最后,可在模块中调用此接口来进行数据业务的处理,而不用关心此接口的具体实现类是哪一个。
- Service:Service层主要负责业务模块的逻辑应用设计。同样是首先设计接口,再设计其实现的类,接着再Spring的配置文件中配置其实现的关联。这样我们就可以在应用中调用Service接口来进行业务处理。Service层的业务实现,具体要调用到已定义的DAO层的接口,封装Service层的业务逻辑有利于通用的业务逻辑的独立性和重复利用性,程序显得非常简洁。
- Controller:Controller层负责具体的业务模块流程的控制,在此层里面要调用Serice层的接口来控制业务流程,控制的配置也同样是在Spring的配置文件里面进行,针对具体的业务流程,会有不同的控制器,我们具体的设计过程中可以将流程进行抽象归纳,设计出可以重复利用的子单元流程模块,这样不仅使程序结构变得清晰,也大大减少了代码量。
程序设计
我们采用的是gradle5.2.1的版本,build.gradle
文件如下配置:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
首先对实体进行定义:
//Employee.java
package com.thoughtworks.employee;
public class Employee {
public Employee(){
}
public Employee(int id, String name, int age, String gender) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
private int id;
private String name;
private int age;
private String gender;
}
包括id,姓名,年龄和性别信息。
1. DAO层设计
首先是DAO层接口设计:
//IEmployeeDao.java
package com.thoughtworks.employee;
import java.util.List;
public interface IEmployeeDao {
Employee getEmployeeByName(Integer id);
List<Employee> getEmployeeList();
String addEmployee(Employee employee);
String updateEmployee(Integer id, Employee employee);
String deleteEmployee(Integer id);
}
为了简单起见,我们不采用数据库来存储Employee的信息,而是采用Map来存储。在DAO层的接口中,我们定义了五种成员方法,他们对应了五种不同的操作:1)使用Id来查找姓名,2)获得所有Employee成员信息,3)添加Employee成员,4)更新成员,5)删除成员。DAO层接口的实现类如下所示:
//EmployeeDao.java
package com.thoughtworks.employee;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Repository
public class EmployeeDao implements IEmployeeDao{
public EmployeeDao() {}
private static Map<Integer,Employee> employees = new HashMap<>();
static {
Employee e0 = new Employee(0,"小明",20,"男");
Employee e1 = new Employee(1,"小红",19,"女");
Employee e2 = new Employee(2,"小智",15,"男");
Employee e3 = new Employee(3,"小刚",16,"男");
Employee e4 = new Employee(4,"小霞",15,"女");
employees.put(e0.getId(),e0);
employees.put(e1.getId(),e1);
employees.put(e2.getId(),e2);
employees.put(e3.getId(),e3);
employees.put(e4.getId(),e4);
}
@Override
public Employee getEmployeeByName(Integer id) {
return employees.get(id);
}
@Override
public List<Employee> getEmployeeList() {
List<Employee> employeeList = new ArrayList<>(employees.values());
return employeeList;
}
@Override
public String addEmployee(Employee employee) {
employees.put(employee.getId(),employee);
return "success";
}
@Override
public String updateEmployee(Integer id, Employee employee) {
employees.get(id).setName(employee.getName());
employees.get(id).setAge(employee.getAge());
employees.get(id).setGender(employee.getGender());
return "success";
}
@Override
public String deleteEmployee(Integer id) {
employees.remove(id);
return "success";
}
}
使用注解@Repository
来标记。
Service层设计
//IDataService.java
package com.thoughtworks.employee;
import java.util.List;
public interface IDataServices {
Employee getEmployeeByName(Integer id);
List<Employee> getEmployeeList();
String addEmployee(Employee employee);
String updateEmployee(Integer id, Employee employee);
String deleteEmployee(Integer id);
}
//DataService.java
package com.thoughtworks.employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DataServices implements IDataServices{
@Autowired
private EmployeeDao employeeDao;
@Override
public Employee getEmployeeByName(Integer id) {
return employeeDao.getEmployeeByName(id);
}
@Override
public List<Employee> getEmployeeList() {
return employeeDao.getEmployeeList();
}
@Override
public String addEmployee(Employee employee) {
return employeeDao.addEmployee(employee);
}
@Override
public String updateEmployee(Integer id, Employee employee) {
return employeeDao.updateEmployee(id,employee);
}
@Override
public String deleteEmployee(Integer id) {
return employeeDao.deleteEmployee(id);
}
}
使用注解@Autowired
来注明调用Employee的实例化部分。
Controller层设计
//EmployeeController.java
package com.thoughtworks.employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@RestController
@RequestMapping(value = "/employees")
public class EmployeeController {
@Autowired
private DataServices dataServices;
@RequestMapping(value = "/",method = RequestMethod.GET)
public List<Employee> getEmployeeList() {
return dataServices.getEmployeeList();
}
@RequestMapping(value = "/", method = RequestMethod.POST)
public String postEmployee(@RequestBody Employee employee) {
return dataServices.addEmployee(employee);
}
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public Employee getEmployee(@PathVariable Integer id) {
return dataServices.getEmployeeByName(id);
}
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public String putEmployee(@PathVariable Integer id, @RequestBody Employee employee) {
return dataServices.updateEmployee(id,employee);
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public String deleteEmployee(@PathVariable Integer id) {
return dataServices.deleteEmployee(id);
}
}