- 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.findById() to get Employee entity by Id
public Employee getEmployeeById(int id) {
return repository.findById(id).get();
}
员工服务
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;
public Employee getEmployeeById(int id) {
return repository.findById(id).get();
}
public List<Employee> getAllEmployees(){
return null;
}
public void saveOrUpdate(Employee employee) {
repository.save(employee);
}
public void deleteEmployeeById(int id) {
}
}
更新控制器
// Use service.getEmployeeById() to get Employee entity from database
@GetMapping("/employee/{id}")
public Employee getEmployee(@PathVariable("id") int id) {
return employeeService.getEmployeeById(id);
}
员工控制器
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;
@GetMapping("/employees")
public List<Employee> getAllEmployees(){
return null;
}
@GetMapping("/employee/{id}")
public Employee getEmployee(@PathVariable("id") int id) {
return employeeService.getEmployeeById(id);
}
@DeleteMapping("/employee/{id}")
public void deleteEmployee(@PathVariable("id") int id) {
}
@PostMapping("/employee")
public void addEmployee(@RequestBody Employee employee) {
employeeService.saveOrUpdate(employee);
}
@PutMapping("/employee")
public void updateEmployee(@RequestBody Employee employee) {
}
}
运行应用程序
在 eclipse 中,运行应用程序设置期间准备的员工应用程序配置
Eclipse 控制台将显示类似的输出。
[INFO] Scanning for projects... ... 2021-07-24 20:51:14.823 INFO 9760 --- [ restartedMain] c.t.s.SprintBootH2Application : Started SprintBootH2Application in 7.353 seconds (JVM running for 8.397)
服务器启动并运行后,首先使用 Postman 发出 POST 请求来添加记录。
在 POSTMAN 中设置以下参数。
HTTP 方法 - POST
URL - http://localhost:8080/emp/employee
BODY -员工 JSON
{
"id": "1",
"age": "35",
"name": "Julie",
"email": "julie@gmail.com"
}
单击发送按钮并检查响应状态是否正常。现在发出 GET 请求来获取该记录。
在 POSTMAN 中设置以下参数。
HTTP 方法 - GET
URL - http://localhost:8080/emp/employee/1
单击发送按钮并验证响应。
{
"id": "1",
"age": "35",
"name": "Julie",
"email": "julie@gmail.com"
}