Vue+SpringBoot+ElementUI实战学生管理系统-4.后端API编写

1.章节介绍

前一片介绍了项目的表结构设计,这一篇编写后端API,需要的朋友可以拿去自己定制。:)

2.获取源码

源码是捐赠方式获取,详细请QQ联系我 :)!

3.项目截图

登录页

在这里插入图片描述

列表操作

在这里插入图片描述

动态图

后端接口设计

登录接口

@RestController
@RequestMapping("/api")
public class LoginController {
     

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result<LoginInfo> login(@RequestBody User user, HttpSession session){
     
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username",user.getUsername());
        wrapper.eq("password",user.getPassword());
        User entity = userService.getOne(wrapper);

        if (entity == null){
     
            return ResultUtil.fail("用户名密码错误!");
        }
        else {
     
            session.setAttribute("user",entity);
            LoginInfo loginInfo = new LoginInfo(entity.getUsername(), entity.getNickname(), "eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE2MTAzNDg3MjYsImV4cCI6MTYxMDQzNTEyNn0");
            return ResultUtil.ok(loginInfo);
        }
    }

    @GetMapping("logout")
    public Result<String> logout(HttpServletRequest request){
     
        request.getSession().removeAttribute("user");
        return ResultUtil.ok("注销成功");
    }
}

用户接口

@RestController
@RequestMapping("/api/user")
public class UserController {
     
    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public Result<IPage<User>> list(@RequestParam(required = false) String query,
                                    @RequestParam(defaultValue = "1") Integer pageNo,
                                    @RequestParam(defaultValue = "10") Integer pageSize) {
     
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(query)){
     
            queryWrapper.like("username",query);
        }

        Page<User> page = new Page<>(pageNo,pageSize);

        IPage<User> pageResult = userService.page(page, queryWrapper);

        // 设置总记录数
        pageResult.setTotal(userService.count(queryWrapper));

        return ResultUtil.ok(pageResult);
    }

    @PostMapping("/removes")
    public Result<String> remove(@RequestBody List<Integer> ids){
     
        userService.removeByIds(ids);
        return ResultUtil.ok();
    }

    @PostMapping("/remove/{id}")
    public Result<String> remove(@PathVariable Integer id){
     
        userService.removeById(id);
        return ResultUtil.ok();
    }

    @PostMapping("/modify")
    public Result<String> save(@RequestBody User entity){
     
        userService.saveOrUpdate(entity);
        return ResultUtil.ok();
    }

    @GetMapping("/{id}")
    public Result<User> get(@PathVariable Integer id){
     
        return ResultUtil.ok(userService.getById(id));
    }

}

院系接口

@RestController
@RequestMapping("/api/dept")
public class DeptController {
     
    @Autowired
    private DeptService deptService;

    @GetMapping("/listAll")
    public Result<List<Dept>> listAll(){
     
        List<Dept> list = deptService.list(null);
        return ResultUtil.ok(list);
    }

    @GetMapping("/list")
    public Result<IPage<Dept>> list(@RequestParam(required = false) String query,
                                    @RequestParam(defaultValue = "1") Integer pageNo,
                                    @RequestParam(defaultValue = "10") Integer pageSize) {
     
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(query)){
     
            queryWrapper.like("dept_name",query);
        }

        Page<Dept> page = new Page<>(pageNo,pageSize);

        IPage<Dept> pageResult = deptService.page(page, queryWrapper);

        // 设置总记录数
        pageResult.setTotal(deptService.count(queryWrapper));

        return ResultUtil.ok(pageResult);
    }

    @PostMapping("/removes")
    public Result<String> remove(@RequestBody List<Integer> ids){
     
        deptService.removeByIds(ids);
        return ResultUtil.ok();
    }

    @PostMapping("/remove/{id}")
    public Result<String> remove(@PathVariable Integer id){
     
        deptService.removeById(id);
        return ResultUtil.ok();
    }

    @PostMapping("/modify")
    public Result<String> save(@RequestBody Dept entity){
     
        deptService.saveOrUpdate(entity);
        return ResultUtil.ok();
    }

    @GetMapping("/{id}")
    public Result<Dept> get(@PathVariable Integer id){
     
        return ResultUtil.ok(deptService.getById(id));
    }


}

专业接口

@RestController
@RequestMapping("/api/major")
public class MajorController {
     
    @Autowired
    private MajorService majorService;

    @GetMapping("/list")
    public Result<IPage<Major>> list(@RequestParam(required = false) String query,
                                     @RequestParam(defaultValue = "1") Integer pageNo,
                                     @RequestParam(defaultValue = "10") Integer pageSize) {
     
        QueryWrapper<Major> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(query)){
     
            queryWrapper.like("major_name",query);
        }

        Page<Major> page = new Page<>(pageNo,pageSize);

        IPage<Major> pageResult = majorService.listPage(page, query);

        // 设置总记录数
        pageResult.setTotal(majorService.count(queryWrapper));

        return ResultUtil.ok(pageResult);
    }

    @PostMapping("/removes")
    public Result<String> remove(@RequestBody List<Integer> ids){
     
        majorService.removeByIds(ids);
        return ResultUtil.ok();
    }

    @PostMapping("/remove/{id}")
    public Result<String> remove(@PathVariable Integer id){
     
        majorService.removeById(id);
        return ResultUtil.ok();
    }

    @PostMapping("/modify")
    public Result<String> save(@RequestBody Major entity){
     
        majorService.saveOrUpdate(entity);
        return ResultUtil.ok();
    }

    @GetMapping("/{id}")
    public Result<Major> get(@PathVariable Integer id){
     
        return ResultUtil.ok(majorService.getById(id));
    }
}

班级接口

@RestController
@RequestMapping("/api/grade")
public class GradeController {
     
    @Autowired
    private GradeService gradeService;

    @GetMapping("/listAll")
    public Result<List<Grade>> listAll(){
     
        return ResultUtil.ok(gradeService.list(null));
    }

    @GetMapping("/list")
    public Result<IPage<Grade>> list(@RequestParam(required = false) String query,
                                    @RequestParam(defaultValue = "1") Integer pageNo,
                                    @RequestParam(defaultValue = "10") Integer pageSize) {
     
        QueryWrapper<Grade> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(query)){
     
            queryWrapper.like("grade_name",query);
        }

        Page<Grade> page = new Page<>(pageNo,pageSize);

        IPage<Grade> pageResult = gradeService.listPage(page, query);

        // 设置总记录数
        pageResult.setTotal(gradeService.count(queryWrapper));

        return ResultUtil.ok(pageResult);
    }

    @PostMapping("/removes")
    public Result<String> remove(@RequestBody List<Integer> ids){
     
        gradeService.removeByIds(ids);
        return ResultUtil.ok();
    }

    @PostMapping("/remove/{id}")
    public Result<String> remove(@PathVariable Integer id){
     
        gradeService.removeById(id);
        return ResultUtil.ok();
    }

    @PostMapping("/modify")
    public Result<String> save(@RequestBody Grade entity){
     
        gradeService.saveOrUpdate(entity);
        return ResultUtil.ok();
    }

    @GetMapping("/{id}")
    public Result<Grade> get(@PathVariable Integer id){
     
        return ResultUtil.ok(gradeService.getById(id));
    }


}

教师接口

@RestController
@RequestMapping("/api/teacher")
public class TeacherController {
     
    @Autowired
    private TeacherService teacherService;

    @GetMapping("/listAll")
    public Result<List<Teacher>> listAll(){
     
        List<Teacher> list = teacherService.list(null);
        return ResultUtil.ok(list);
    }

    @GetMapping("/list")
    public Result<IPage<Teacher>> list(@RequestParam(required = false) String query,
                                    @RequestParam(defaultValue = "1") Integer pageNo,
                                    @RequestParam(defaultValue = "10") Integer pageSize) {
     
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(query)){
     
            queryWrapper.like("teacher_name",query);
        }

        Page<Teacher> page = new Page<>(pageNo,pageSize);

        IPage<Teacher> pageResult = teacherService.page(page, queryWrapper);

        // 设置总记录数
        pageResult.setTotal(teacherService.count(queryWrapper));

        return ResultUtil.ok(pageResult);
    }

    @PostMapping("/removes")
    public Result<String> remove(@RequestBody List<Integer> ids){
     
        teacherService.removeByIds(ids);
        return ResultUtil.ok();
    }

    @PostMapping("/remove/{id}")
    public Result<String> remove(@PathVariable Integer id){
     
        teacherService.removeById(id);
        return ResultUtil.ok();
    }

    @PostMapping("/modify")
    public Result<String> save(@RequestBody Teacher entity){
     
        teacherService.saveOrUpdate(entity);
        return ResultUtil.ok();
    }

    @GetMapping("/{id}")
    public Result<Teacher> get(@PathVariable Integer id){
     
        return ResultUtil.ok(teacherService.getById(id));
    }
}

学生接口

@RestController
@RequestMapping("/api/student")
public class StudentController {
     
    @Autowired
    private StudentService studentService;

    @Autowired
    private GradeService gradeService;

    @GetMapping("/list")
    public Result<IPage<Student>> list(@RequestParam(required = false) String query,
                                     @RequestParam(defaultValue = "1") Integer pageNo,
                                     @RequestParam(defaultValue = "10") Integer pageSize) {
     
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(query)){
     
            queryWrapper.like("student_name",query);
        }

        Page<Student> page = new Page<>(pageNo,pageSize);

        IPage<Student> pageResult = studentService.listPage(page, query);

        // 设置总记录数
        pageResult.setTotal(studentService.count(queryWrapper));

        return ResultUtil.ok(pageResult);
    }

    @PostMapping("/removes")
    public Result<String> remove(@RequestBody List<Integer> ids){
     
        studentService.removeByIds(ids);
        return ResultUtil.ok();
    }

    @PostMapping("/remove/{id}")
    public Result<String> remove(@PathVariable Integer id){
     
        studentService.removeById(id);
        return ResultUtil.ok();
    }

    @PostMapping("/modify")
    public Result<String> save(@RequestBody Student entity){
     
        studentService.saveOrUpdate(entity);
        return ResultUtil.ok();
    }

    @GetMapping("/{id}")
    public Result<Student> get(@PathVariable Integer id){
     
        return ResultUtil.ok(studentService.getById(id));
    }


    //接受文件上传
    @RequestMapping("/upload")
    @ResponseBody
    public Result<String> uploadFile(MultipartFile file, HttpServletResponse response) {
     
        //解析excel文件
        List<ArrayList<String>> rows = ExcelUtil.analysis(file);
        List<Student> list = new ArrayList<>();
        if(rows.size()>0){
     
            //2.插入数据
            Student entity = null;
            int size=0;
            String gradeName; // 班级名称
            Integer gradeId;
            for (int i = 0;i<rows.size();i++){
     
                entity = new Student();
                List<String> row = rows.get(i);
                size = row.size();

                // 不足9个列的 补全
                for (int j = size; j < 9; j++) {
     
                    row.add("");
                }

                // 学号
                entity.setStudentNo(row.get(0));
                // 姓名
                entity.setStudentName(row.get(1));
                entity.setGender(row.get(2).equals("女")?"F":"M");
                entity.setIdno(row.get(3));
                entity.setPhone(row.get(4));
                entity.setAddress(row.get(5));
                gradeName = row.get(6);
                gradeId = getGradeIdByName(gradeName);
                if(gradeId == null){
     
                    continue;
                }
                entity.setGradeId(gradeId);
                entity.setEnrollDate(DateUtil.format(row.get(7),"yyyy-MM-dd"));
                entity.setRemark(row.get(8));

                list.add(entity);
            }
        }

        studentService.saveBatch(list);

        return ResultUtil.ok("导入成功!");
    }

    /**
     *  导出
     * @param response
     * @throws Exception
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response)  throws Exception{
     
        // 创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建sheet
        HSSFSheet sheet = workbook.createSheet("sheet1");
        String fileName = "学生列表.xls"; // 设置要导出的文件的名字
        // 获取数据集合
        List<Student> list = studentService.list(null);
        // 生成标题行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("序号");
        row.createCell(1).setCellValue("学号");
        row.createCell(2).setCellValue("姓名");
        row.createCell(3).setCellValue("性别");
        row.createCell(4).setCellValue("身份证");
        row.createCell(5).setCellValue("联系方式");
        row.createCell(6).setCellValue("家庭地址");
        row.createCell(7).setCellValue("班级");
        row.createCell(8).setCellValue("入学日期");
        row.createCell(9).setCellValue("备注");

        Student entity=null;
        for (int i = 0; i < list.size(); i++) {
     
            entity = list.get(i);
            row = sheet.createRow(i+1); // 从第2行开始填充数据
            // 序号
            row.createCell(0).setCellValue(String.valueOf(i+1));
            row.createCell(1, CellType.STRING).setCellValue(entity.getStudentNo());
            row.createCell(2,CellType.STRING).setCellValue(entity.getStudentName());
            row.createCell(3).setCellValue(entity.getGender().equals("F")?"女":"男");
            row.createCell(4).setCellValue(entity.getIdno());
            row.createCell(5).setCellValue(entity.getPhone());
            row.createCell(6).setCellValue(entity.getAddress());
            row.createCell(7,CellType.STRING).setCellValue(getGradeNameById(entity.getGradeId()));
            row.createCell(8,CellType.STRING).setCellValue(DateUtil.format(entity.getEnrollDate(),"yyyy-MM-dd"));
            row.createCell(9).setCellValue(entity.getRemark());
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition",
                "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
        response.flushBuffer();

        workbook.write(response.getOutputStream());
    }

    private Integer getGradeIdByName(String gradeName){
     
        QueryWrapper<Grade> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("grade_name", gradeName);
        Grade one = gradeService.getOne(queryWrapper);
        if(one == null){
     
            return null;
        }else{
     
            return one.getId();
        }
    }


    private String getGradeNameById(Integer gradeId){
     
        Grade one = gradeService.getById(gradeId);
        if(one == null){
     
            return null;
        }else{
     
            return one.getGradeName();
        }
    }


}

你可能感兴趣的:(#,Spring-Boot,Java项目实战,#,Vue实战,vue前后分离,springboot学生管理,vue学生管理,vue+elementui)