springboot+vue+mybatisplus项目实战-学生成绩管理系统06

661 篇文章 4 订阅
112 篇文章 0 订阅

Controller

scoreController.java

package com.shrimpking.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shrimpking.exception.BizException;
import com.shrimpking.pojo.Score;
import com.shrimpking.req.SearchReq;
import com.shrimpking.res.Result;
import com.shrimpking.service.ScoreService;
import com.shrimpking.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 成绩表 前端控制器
 * </p>
 *
 * @author shrimpking
 * @since 2023-11-04
 */
@RestController
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    //新增
    @PostMapping("/add")
    public Result<String> add(@RequestBody @Validated Score score){
        //先查询同一个考试,同一个学生是否已有
        LambdaQueryWrapper<Score> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Score::getExamName,score.getExamName());
        queryWrapper.eq(Score::getStudentId,score.getStudentId());
        int count = this.scoreService.count(queryWrapper);
        if(count > 0){
            throw new BizException("成绩已存在");
        }

        boolean save = this.scoreService.save(score);
        if(!save) return Result.failure("新增失败");
        return Result.success("新增成功");
    }

    //分页数据
    @GetMapping("/pageList")
    public Result<PageVO> pageList(SearchReq searchReq){
        //创建分页对象
        IPage<Score> page = new Page<>(searchReq.getCurrentPage(), searchReq.getPageSize());
        LambdaQueryWrapper<Score> queryWrapper = new LambdaQueryWrapper<>();
        //搜索关键字是否为空,为空,不加like条件
        queryWrapper.like(
                StringUtils.isNotBlank(searchReq.getSearchWord()),
                Score::getExamName,
                searchReq.getSearchWord());
        this.scoreService.page(page,queryWrapper);
        //封装返回对象
        PageVO pageVO = PageVO.createPageVO(page);
        return Result.success(pageVO);
    }

    //删除
    @GetMapping("/delete")
    public Result<String> delete(Long id){
        boolean remove = this.scoreService.removeById(id);
        if(!remove) return Result.failure("删除失败");
        return Result.success("删除成功");
    }

    //根据id查询成绩
    @GetMapping("/findById")
    public Result<Score> findById(Long id){
        Score score = this.scoreService.getById(id);
        return Result.success(score);
    }

    //更新
    @PostMapping("/update")
    public Result<String> update(@RequestBody @Validated Score score){
        //先查询同一个考试,同一个学生是否已有
        LambdaQueryWrapper<Score> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Score::getExamName,score.getExamName());
        queryWrapper.eq(Score::getStudentId,score.getStudentId());
        Score one = this.scoreService.getOne(queryWrapper);
        //如果查找的对象不为空,且id不同,说明已有同类数据
        if(one != null && !one.getId().equals(score.getId())){
            //不是自己
            throw new BizException("成绩已存在");
        }

        boolean update = this.scoreService.updateById(score);
        if(!update) return Result.failure("更新失败");
        return Result.success("更新成功");
    }

    @GetMapping("/findAll")
    public Result<List<Score>> findAll(){
        List<Score> list = this.scoreService.list();
        return Result.success(list);
    }

}

studentController.java

package com.shrimpking.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shrimpking.exception.BizException;
import com.shrimpking.pojo.Student;
import com.shrimpking.req.SearchReq;
import com.shrimpking.res.Result;
import com.shrimpking.service.StudentService;
import com.shrimpking.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author shrimpking
 * @since 2023-11-04
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    //validated是开启校验字段为空的

    //新增
    @PostMapping("/add")
    public Result<String> add(@RequestBody @Validated Student student){
        //先查询序号是否已有
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getNumber,student.getNumber());
        int count = this.studentService.count(queryWrapper);
        if(count > 0) {
            throw new BizException("学号已存在");
        }

        boolean save = this.studentService.save(student);
        if(!save) return Result.failure("新增失败");
        return Result.success("新增成功");
    }

    //分页数据
    @GetMapping("/pageList")
    public Result<PageVO> pageList(SearchReq searchReq){
        //创建分页对象
        IPage<Student> page = new Page<>(searchReq.getCurrentPage(),searchReq.getPageSize());
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        //判断搜索关键词是否有,为空时,不加like条件
        queryWrapper.like(
                StringUtils.isNotBlank(searchReq.getSearchWord()),
                Student::getRealName,
                searchReq.getSearchWord());
        this.studentService.page(page,queryWrapper);
        //封装返回对象
        PageVO pageVO = PageVO.createPageVO(page);
        return Result.success(pageVO);
    }

    //删除
    @GetMapping("/delete")
    public Result<String> delete(Long id){
        boolean remove = this.studentService.removeById(id);
        if(!remove) return Result.failure("删除失败");
        return Result.success("删除成功");
    }

    //根据id查询学生
    @GetMapping("/findById")
    public Result<Student> findById(Long id){
        Student student = this.studentService.getById(id);
        return Result.success(student);
    }

    //新增
    @PostMapping("/update")
    public Result<String> update(@RequestBody @Validated Student student){
        //先查询序号是否已有
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getNumber,student.getNumber());
        Student one = this.studentService.getOne(queryWrapper);
        //如果查找的对象不为空,且id不同,说明已有同类数据
        if(one != null && !one.getId().equals(student.getId())){
            //不是自己
            throw new BizException("学号已存在");
        }

        boolean update = this.studentService.updateById(student);
        if(!update) return Result.failure("更新失败");
        return Result.success("更新成功");
    }

    @GetMapping("/findAll")
    public Result<List<Student>> findAll(){
        List<Student> list = this.studentService.list();
        return Result.success(list);
    }


}

Usercontroller.java

package com.shrimpking.controller;


import com.shrimpking.req.LoginReq;
import com.shrimpking.res.Result;
import com.shrimpking.service.UserService;
import com.shrimpking.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author shrimpking
 * @since 2023-11-04
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    //json登录方式
    @PostMapping("/login")
    public Result<UserVO> login(@RequestBody @Validated LoginReq loginReq){
        //日志
        log.info("login params: {}",loginReq);
        return this.userService.login(loginReq);
    }

    //表单登录方式
    @PostMapping("/login2")
    public Result<UserVO> login2(@Validated LoginReq loginReq){
        //日志
        log.info("login2 params: {}",loginReq);
        return this.userService.login(loginReq);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值