You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.0 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package cn.zyp.stusystem.controller;
import cn.zyp.stusystem.entity.Student;
import cn.zyp.stusystem.service.StudentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/api/student")
public class StudentController {
@Autowired
private StudentService studentService;
//获取学生列表
@GetMapping("/list")
public Map<String, Object> getStudentList(
@RequestParam Integer page, // 页码从1开始
@RequestParam Integer size, // 每页条数
@RequestParam Integer grade, // 年级1-3
@RequestParam(required = false) Long classId, // 班级ID
@RequestParam(required = false) String name
) {
//参数校验
if (page == null || page < 1) {
page = 1;
}
IPage<Student> studentPage = studentService.selectPageStudent(page, size, grade, classId, name);
Map<String, Object> result = new HashMap<>();
result.put("list", studentPage.getRecords());
result.put("total", studentPage.getTotal());
//测试
System.out.println("后端返回的学生数据:" + result);
return result;
}
//新增学生
@PostMapping("/add")
public boolean addStudent(@RequestBody Student student) {
return studentService.save(student);
}
//编辑学生
@PutMapping("/edit")
public boolean editStudent(@RequestBody Student student) {
return studentService.updateById(student);
}
//删除学生
@DeleteMapping("/delete/{id}")
public boolean deleteStudent(@PathVariable String id) {
if (id.contains(",")) {
String[] ids = id.split(",");
return studentService.removeByIds(Arrays.asList(ids));
} else {
return studentService.removeById(id);
}
}
}