- Spring Boot 与 H2 教程
- Spring Boot 与 H2 - 首页
- Spring Boot 与 H2 - 概览
- Spring Boot 与 H2 - 环境设置
- Spring Boot 与 H2 - 项目设置
- Spring Boot 与 H2 - REST API
- Spring Boot 与 H2 - H2 控制台
- Spring Boot 与 H2 示例
- Spring Boot 与 H2 - 添加记录
- Spring Boot 与 H2 - 获取记录
- Spring Boot 与 H2 - 获取所有记录
- Spring Boot 与 H2 - 更新记录
- Spring Boot 与 H2 - 删除记录
- Spring Boot 与 H2 - 单元测试控制器
- Spring Boot 与 H2 - 单元测试服务
- Spring Boot 与 H2 - 单元测试仓库
- Spring Boot 与 H2 实用资源
- Spring Boot 与 H2 - 快速指南
- Spring Boot 与 H2 - 实用资源
- Spring Boot 与 H2 - 讨论
Spring Boot 与 H2 - 删除记录
现在让我们更新迄今为止创建的项目,以准备一个完整的删除记录 API 并测试它。
更新服务
// Use repository.deleteById() to delete an Employee record public void deleteEmployeeById(int id) { repository.deleteById(id); }
EmployeeService
package com.tutorialspoint.service; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.tutorialspoint.entity.Employee; import com.tutorialspoint.repository.EmployeeRepository; @Service public class EmployeeService { @Autowired EmployeeRepository repository; // get an employee by id public Employee getEmployeeById(int id) { return repository.findById(id).get(); } // get all employees public List<Employee> getAllEmployees(){ List<Employee> employees = new ArrayList<Employee>(); repository.findAll().forEach(employee -> employees.add(employee)); return employees; } // create or update an employee public void saveOrUpdate(Employee employee) { repository.save(employee); } // delete an employee by id public void deleteEmployeeById(int id) { repository.deleteById(id); } }
更新控制器
// Use service.deleteEmployeeById() to delete an employee by id @DeleteMapping("/employee/{id}") public void deleteEmployee(@PathVariable("id") int id) { employeeService.deleteEmployeeById(id); }
EmployeeController
package com.tutorialspoint.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.tutorialspoint.entity.Employee; import com.tutorialspoint.service.EmployeeService; @RestController @RequestMapping(path = "/emp") public class EmployeeController { @Autowired EmployeeService employeeService; // get all employees @GetMapping("/employees") public List<Employee> getAllEmployees(){ return employeeService.getAllEmployees(); } // get an employee by id @GetMapping("/employee/{id}") public Employee getEmployee(@PathVariable("id") int id) { return employeeService.getEmployeeById(id); } // delete an employee by id @DeleteMapping("/employee/{id}") public void deleteEmployee(@PathVariable("id") int id) { employeeService.deleteEmployeeById(id); } // create an employee @PostMapping("/employee") public void addEmployee(@RequestBody Employee employee) { employeeService.saveOrUpdate(employee); } // update an employee @PutMapping("/employee") public void updateEmployee(@RequestBody Employee employee) { employeeService.saveOrUpdate(employee); } }
运行应用程序
在 eclipse 中,运行 员工应用程序配置(在 应用程序设置 期间准备的)。
Eclipse 控制台将显示类似的输出。
[INFO] Scanning for projects... ... 2024-08-20T17:12:31.294+05:30 INFO 8844 --- [springboot-h2] [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 2024-08-20T17:12:31.307+05:30 INFO 8844 --- [springboot-h2] [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/' 2024-08-20T17:12:31.310+05:30 INFO 8844 --- [springboot-h2] [ restartedMain] c.t.s.SpringbootH2Application : Started SpringbootH2Application in 0.474 seconds (process running for 722.918)
服务器启动并运行后,使用 Postman 进行 POST 请求 -
在 POSTMAN 中,设置以下参数。
HTTP 方法 - POST
URL - https://127.0.0.1:8080/emp/employee
正文 - 员工 JSON
{ "id": "1", "age": "35", "name": "Julie", "email": "[email protected]" }
单击“发送”按钮,并检查响应状态是否为正常。
现在,进行删除请求以删除这些记录。
在 POSTMAN 中,设置以下参数。
HTTP 方法 - DELETE
URL - https://127.0.0.1:8080/emp/employee/1
单击“发送”按钮,并验证响应状态是否为正常。
现在,进行 GET 请求以获取所有记录。
在 POSTMAN 中,设置以下参数。
HTTP 方法 - GET
URL - https://127.0.0.1:8080/emp/employees
单击“发送”按钮,并验证响应。
广告