package com.learn.controller.jpa; import com.learn.entity.Student; import com.learn.service.jpa.StudentJpaService; import jakarta.validation.Valid; import org.springframework.data.domain.Page; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.Map; /** * JPA 版本的学生管理 REST API * * 访问路径全部以 /api/jpa/students 开头, * 与 MyBatis-Plus 版本(/api/mp/students)区分,方便对比测试。 */ @RestController @RequestMapping("/api/jpa/students") public class StudentJpaController { private final StudentJpaService service; public StudentJpaController(StudentJpaService service) { this.service = service; } /** 查询全部 / 搜索 */ @GetMapping public ResponseEntity> list( @RequestParam(required = false) String keyword) { java.util.List students; if (keyword != null && !keyword.trim().isEmpty()) { students = service.searchByKeyword(keyword); } else { students = service.list(); } return ok(students); } /** 分页查询(第 5 天) */ @GetMapping("/page") public ResponseEntity> page( @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) { Page page = service.page(pageNum, pageSize); Map result = new HashMap<>(); result.put("code", 200); result.put("total", page.getTotalElements()); result.put("pages", page.getTotalPages()); result.put("current", page.getNumber() + 1); result.put("data", page.getContent()); return ResponseEntity.ok(result); } /** 根据 ID 查询 */ @GetMapping("/{id}") public ResponseEntity> getById(@PathVariable Long id) { return service.getById(id) .map(s -> ok(s)) .orElse(notFound(id)); } /** 新增 */ @PostMapping public ResponseEntity> add(@Valid @RequestBody Student student) { Student saved = service.add(student); Map result = new HashMap<>(); result.put("code", 201); result.put("message", "添加成功"); result.put("data", saved); return ResponseEntity.status(HttpStatus.CREATED).body(result); } /** 更新 */ @PutMapping("/{id}") public ResponseEntity> update( @PathVariable Long id, @Valid @RequestBody Student student) { return service.update(id, student) .map(updated -> { Map result = new HashMap<>(); result.put("code", 200); result.put("message", "更新成功"); result.put("data", updated); return ResponseEntity.ok(result); }) .orElse(notFound(id)); } /** 删除 */ @DeleteMapping("/{id}") public ResponseEntity> delete(@PathVariable Long id) { if (service.delete(id)) { return ResponseEntity.ok(Map.of("code", 200, "message", "删除成功")); } return notFound(id); } /** 分数统计 */ @GetMapping("/stats") public ResponseEntity> stats( @RequestParam(defaultValue = "80") int min, @RequestParam(defaultValue = "100") int max) { long count = service.countByScoreRange(min, max); Map result = new HashMap<>(); result.put("code", 200); result.put("range", min + "-" + max); result.put("count", count); return ResponseEntity.ok(result); } // ==================== 工具方法 ==================== private ResponseEntity> ok(Object data) { Map result = new HashMap<>(); result.put("code", 200); result.put("data", data); return ResponseEntity.ok(result); } private ResponseEntity> notFound(Long id) { Map error = new HashMap<>(); error.put("code", 404); error.put("message", "学生不存在,ID: " + id); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error); } }