Advertisement

尚硅谷智慧校园--5、学生功能的实现

阅读量:

目录

  • 回显搜索条件中班级选项

    • 在Controller中添加方法
  • 查询学生信息(分页带条件)

    • 在Service及实现类中添加方法
    • 在Controller添加方法
  • 异步图片上传处理的头像

    • 在Controller中添加方法
  • 添加和修改学生信息

    • 在Controller添加方法
  • 学生的批量删除

    • 在Controller添加方法

回显搜索条件中班级选项

在Controller中添加方法

clazzController

复制代码
    /** * 查询所有班级信息
     * @return
     */
    @ApiOperation("查询所有班级信息")
    @GetMapping("/getClazzs")
    public Result getClazzs(){
        List<Clazz> clazzList = clazzService.list();
        return Result.ok(clazzList);
    }
    
    
      
      
      
      
      
      
      
      
      
      
    

查询学生信息(分页带条件)

在Service及实现类中添加方法

StudentService

复制代码
    /** * 查询学生信息(分页带条件)
     * @param page
     * @param student 分页查询的条件
     * @return
     */
    IPage<Student> getStudentByOpr(Page<Student> page, Student student);
    
    
      
      
      
      
      
      
      
    

StudentServiceImpl

复制代码
    /** * 查询学生信息(分页带条件)
     * @param page
     * @param student 分页查询的条件
     * @return
     */
    @Override
    public IPage<Student> getStudentByOpr(Page<Student> page, Student student) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
     
        // 若分页的查询条件不为空,则添加条件
        String name = student.getName();
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name", name);
        }
        String clazzName = student.getClazzName();
        if(!StringUtils.isEmpty(clazzName)){
            queryWrapper.eq("clazz_name", clazzName);
        }
        queryWrapper.orderByDesc("id");
        Page<Student> studentPage = baseMapper.selectPage(page, queryWrapper);
        return studentPage;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

在Controller添加方法

StudentController

复制代码
    /** * @Author zhang
     * @Date 2022/4/18 - 16:11
     * @Version 1.0
     */
    @RestController
    @RequestMapping("/sms/studentController")
    public class StudentController {
     
    @Autowired
    private StudentService studentService;
     
    /** * 查询学生信息(分页带条件)
     * 请求路径:/sms/studentController/getStudentByOpr/{pageNo}/{pageSize}?name=xxx&clazzName=xxx
     * @param pageNo 分页查询的页码
     * @param pageSize 分页查询每页的数据量
     * @param student 分页查询的条件
     * @return
     */
    @ApiOperation("查询学生信息(分页带条件)")
    @GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
    public Result getStudentByOpr(
            @ApiParam("分页查询的页码") @PathVariable("pageNo") Integer pageNo,
            @ApiParam("分页查询每页的数据量") @PathVariable("pageSize") Integer pageSize,
            // 使用Student对象接收name和clazzName参数
            @ApiParam("分页查询的条件") Student student
    ){
        Page<Student> page = new Page<>(pageNo, pageSize);
        IPage<Student> studentIPage = studentService.getStudentByOpr(page, student);
        return Result.ok(studentIPage);
    }
     
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

异步图片上传处理的头像

Request URL: /sms/system/headerImgUpload
Request Method: POST

先将图片保存,再将图片路径与其他信息保存到数据库
在这里插入图片描述

在Controller中添加方法

SystemController

复制代码
    /** * 文件上传统一入口
     * @param multipartFile 上传的头像文件
     * @return
     */
    @ApiOperation("文件上传统一入口")
    @PostMapping("/headerImgUpload")
    public Result headerImgUpload(
            // 接收图片
            @ApiParam("上传的头像文件") @RequestPart("multipartFile") MultipartFile multipartFile
            ){
        // 生成文件名
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        String originalFilename = multipartFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String newFileName = uuid.concat(originalFilename.substring(index));
     
        // 保存文件 将文件发送到第三方/独立的图片服务器上,这里保存到本机
        String portraitPath = "D:/Java/尚硅谷毕设项目/myzhxy/target/classes/public/upload/".concat(newFileName);
        try {
            multipartFile.transferTo(new File(portraitPath)); // 将图片传输到指定位置
        } catch (IOException e) {
            e.printStackTrace();
        }
     
        // 响应图片路径
        String path = "upload/".concat(newFileName);
        return Result.ok(path);
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

添加和修改学生信息

在Controller添加方法

Request URL: /sms/studentController/addOrUpdateStudent
Request Method: POST

若为添加学生信息,则需要设置密码,后端需要将密码转为密文;若为修改学生信息,无法修改密码

StudentController

复制代码
    /** * 添加或修改学生信息
     * @param student 添加或修改的学生的JSON格式的信息
     * @return
     */
    @ApiOperation("添加或修改学生信息")
    @PostMapping("/addOrUpdateStudent")
    public Result addOrUpdateStudent(
            @ApiParam("添加或修改的学生的JSON格式的信息") @RequestBody Student student
    ){
        // 若id为空,则为添加学生信息,需要将密码转为密文
        Integer id = student.getId();
        if(null == id || 0 == id){
            student.setPassword(MD5.encrypt(student.getPassword()));
        }
     
        studentService.saveOrUpdate(student);
        return Result.ok();
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

学生的批量删除

Request URL: http://localhost:8080/sms/studentController/delStudentById
Request Method: DELETE

请求参数是要删除的学生的 ID 的 JSON 数组
在这里插入图片描述

在Controller添加方法

StudentController

复制代码
    /** * 删除或批量删除学生信息
     * @param ids 要删除的学生ID的JSON数组
     * @return
     */
    @ApiOperation("删除或批量删除学生信息")
    @DeleteMapping("/delStudentById")
    public Result delStudentById(
            @ApiParam("要删除的学生ID的JSON数组") @RequestBody List<Integer> ids
    ) {
        studentService.removeByIds(ids);
        return Result.ok();
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
    

全部评论 (0)

还没有任何评论哟~