Spring Boot——增删查改(1)

2019-07-15  本文已影响0人  草样逍遥

Spring Boot增删查改

标签: SpringBoot


增删查改是最为基础的功能,在很多场景下都有运用。本文对增删查改功能进行简单的编写,满足一定的题目需求。

题目设置

本题要求使用Spring boot来完成对Employee CURD(增删改查) API接口的实现。

[
    {
      "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": "女"
    }
  ]

环境描述

模型介绍

使用Spring Boot来进行开发,可以参照三层模型:DAO、Service和Controller层。下面,对这三层的意义的作用进行介绍。

程序设计

我们采用的是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);
    }
}

上一篇下一篇

猜你喜欢

热点阅读