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);
}
}