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

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")
2 weeks ago
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
2 weeks ago
) {
//参数校验
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")
2 weeks ago
public boolean addStudent(@RequestBody Student student) {
return studentService.save(student);
}
//编辑学生
@PutMapping("/edit")
2 weeks ago
public boolean editStudent(@RequestBody Student student) {
return studentService.updateById(student);
}
//删除学生
@DeleteMapping("/delete/{id}")
2 weeks ago
public boolean deleteStudent(@PathVariable String id) {
if (id.contains(",")) {
String[] ids = id.split(",");
return studentService.removeByIds(Arrays.asList(ids));
2 weeks ago
} else {
return studentService.removeById(id);
}
}
}