Files
gc-plan/week3/src/main/java/com/learn/controller/jpa/StudentJpaController.java
2026-04-29 23:45:17 +08:00

134 lines
4.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<Map<String, Object>> list(
@RequestParam(required = false) String keyword) {
java.util.List<Student> students;
if (keyword != null && !keyword.trim().isEmpty()) {
students = service.searchByKeyword(keyword);
} else {
students = service.list();
}
return ok(students);
}
/** 分页查询(第 5 天) */
@GetMapping("/page")
public ResponseEntity<Map<String, Object>> page(
@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "5") int pageSize) {
Page<Student> page = service.page(pageNum, pageSize);
Map<String, Object> 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<Map<String, Object>> getById(@PathVariable Long id) {
return service.getById(id)
.map(s -> ok(s))
.orElse(notFound(id));
}
/** 新增 */
@PostMapping
public ResponseEntity<Map<String, Object>> add(@Valid @RequestBody Student student) {
Student saved = service.add(student);
Map<String, Object> 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<Map<String, Object>> update(
@PathVariable Long id, @Valid @RequestBody Student student) {
return service.update(id, student)
.map(updated -> {
Map<String, Object> 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<Map<String, Object>> delete(@PathVariable Long id) {
if (service.delete(id)) {
return ResponseEntity.ok(Map.of("code", 200, "message", "删除成功"));
}
return notFound(id);
}
/** 分数统计 */
@GetMapping("/stats")
public ResponseEntity<Map<String, Object>> stats(
@RequestParam(defaultValue = "80") int min,
@RequestParam(defaultValue = "100") int max) {
long count = service.countByScoreRange(min, max);
Map<String, Object> result = new HashMap<>();
result.put("code", 200);
result.put("range", min + "-" + max);
result.put("count", count);
return ResponseEntity.ok(result);
}
// ==================== 工具方法 ====================
private ResponseEntity<Map<String, Object>> ok(Object data) {
Map<String, Object> result = new HashMap<>();
result.put("code", 200);
result.put("data", data);
return ResponseEntity.ok(result);
}
private ResponseEntity<Map<String, Object>> notFound(Long id) {
Map<String, Object> error = new HashMap<>();
error.put("code", 404);
error.put("message", "学生不存在ID: " + id);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error);
}
}