学生信息管理系统:添加、修改学生信息

本系统提供学生信息管理功能,包括添加学生、修改学生信息等功能。系统对添加学生时学号重复进行校验,并提供错误提示。

1. 添加学生时,查看学号是否存在模块

函数实现:

checkStudentId(CheckUserId checkUserId):通过输入的学号查找学生信息,如果能够查找到,则说明该学号已经存在,返回错误信息;否则,返回正确信息。

代码示例:

@RequestMapping(value = "/checkStudentId")
@ResponseBody
public CheckUserId checkStudentId(@RequestBody CheckUserId checkUserId) throws Exception {

    Integer userid = checkUserId.getUserid();
    StudentCustom studentCustom = studentService.findById(userid);
    if (studentCustom != null) {
        // 学号已经存在
        checkUserId.setFlag(false);
        checkUserId.setErrorMsg('该学号已经存在,请重新输入!');
    } else {
        // 学号不存在,可以添加
        checkUserId.setFlag(true);
        checkUserId.setErrorMsg('');
    }
    return checkUserId;
}

2. 添加学生信息页面显示模块

函数实现:

addStudentUI(Model model):获取所有学院信息,并将其传递给前端页面。

代码示例:

//  添加学生信息页面显示
@RequestMapping(value = "/addStudent", method = {RequestMethod.GET})
public String addStudentUI(Model model) throws Exception {

    List<College> list = collegeService.finAll();

    model.addAttribute('collegeList', list);

    return "admin/addStudent";
}

3. 添加学生信息操作模块

函数实现:

addStudent(StudentCustom studentCustom, Model model):将前端传递过来的学生信息保存到数据库中,如果保存失败,则返回错误信息;否则,将学生信息添加到登录表中,并重定向到学生信息页面。

代码示例:

// 添加学生信息操作
@RequestMapping(value = "/addStudent", method = {RequestMethod.POST})
public String addStudent(StudentCustom studentCustom, Model model) throws Exception {

    Boolean result = studentService.save(studentCustom);

    if (!result) {
        model.addAttribute('message', '学号重复');
        return "error";
    }
    // 添加成功后,也添加到登录表
    Userlogin userlogin = new Userlogin();
    userlogin.setUsername(studentCustom.getUserid().toString());
    userlogin.setPassword("123");
    userlogin.setRole(2);
    userloginService.save(userlogin);

    // 重定向
    return "redirect:/admin/showStudent";
}

4. 修改学生信息页面显示模块

函数实现:

editStudentUI(Integer id, Model model):通过传递的学生id查找学生信息,如果能够查找到,则将学生信息和所有学院信息传递给前端页面;否则,重定向到学生信息页面。

代码示例:

// 修改学生信息页面显示
@RequestMapping(value = "/editStudent", method = {RequestMethod.GET})
public String editStudentUI(Integer id, Model model) throws Exception {
    if (id == null) {
        // 加入没有带学生id就进来的话就返回学生显示页面
        return "redirect:/admin/showStudent";
    }
    StudentCustom studentCustom = studentService.findById(id);
    if (studentCustom == null) {
        throw new CustomException("未找到该名学生");
    }
    List<College> list = collegeService.finAll();

    model.addAttribute('collegeList', list);
    model.addAttribute('student', studentCustom);

    return "admin/editStudent";
}

5. 修改学生信息处理模块

函数实现:

editStudent(StudentCustom studentCustom):通过传递的学生id更新学生信息,并重定向到学生信息页面。

代码示例:

// 修改学生信息处理
@RequestMapping(value = "/editStudent", method = {RequestMethod.POST})
public String editStudent(StudentCustom studentCustom) throws Exception {

    studentService.updataById(studentCustom.getUserid(), studentCustom);

    // 重定向
    return "redirect:/admin/showStudent";
}

注意:

  • 以上代码示例仅供参考,具体实现方式可能因项目需求有所不同。
  • 代码中使用 @RequestMapping 注解来映射 URL,使用 @ResponseBody 注解来返回 JSON 数据,使用 Model 对象来传递数据到页面。
  • 具体实现细节,例如数据库操作、错误处理等,需要根据项目实际情况进行设计。
  • 为了提高代码的可读性和可维护性,建议采用良好的代码规范,并添加必要的注释。
  • 为了提高系统的安全性,建议对用户输入进行必要的校验和过滤,防止出现 SQL 注入、跨站脚本攻击等安全问题。
  • 最后,要对系统进行充分的测试,确保系统能够正常运行,并满足用户需求。
学生信息管理系统:添加、修改学生信息

原文地址: https://www.cveoy.top/t/topic/oMso 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录