利用SSM框架怎么制作一个学生管理系统?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
概述
基于Spring + Spring MVC 的学生管理系统,使用Maven进行包管理。
主要代码
@RequestMapping("/student") @Controller public class StudentController { @Autowired private StudentService studentService; @Autowired private ClazzService clazzService; /** * 学生列表页 * @param model * @return */ @RequestMapping(value="/list",method=RequestMethod.GET) public ModelAndView list(ModelAndView model){ model.setViewName("student/student_list"); List<Clazz> clazzList = clazzService.findAll(); model.addObject("clazzList",clazzList ); model.addObject("clazzListJson",JSONArray.fromObject(clazzList)); return model; } /** * 获取学生列表 * @param name * @param page * @return */ @RequestMapping(value="/get_list",method=RequestMethod.POST) @ResponseBody public Map<String, Object> getList( @RequestParam(value="name",required=false,defaultValue="") String name, @RequestParam(value="clazzId",required=false) Long clazzId, HttpServletRequest request, Page page ){ Map<String, Object> ret = new HashMap<String, Object>(); Map<String, Object> queryMap = new HashMap<String, Object>(); queryMap.put("username", "%"+name+"%"); Object attribute = request.getSession().getAttribute("userType"); if("2".equals(attribute.toString())){ //说明是学生 Student loginedStudent = (Student)request.getSession().getAttribute("user"); queryMap.put("username", "%"+loginedStudent.getUsername()+"%"); } if(clazzId != null){ queryMap.put("clazzId", clazzId); } queryMap.put("offset", page.getOffset()); queryMap.put("pageSize", page.getRows()); ret.put("rows", studentService.findList(queryMap)); ret.put("total", studentService.getTotal(queryMap)); return ret; } /** * 编辑学生信息 * @param clazz * @return */ @RequestMapping(value="/edit",method=RequestMethod.POST) @ResponseBody public Map<String, String> edit(Student student){ Map<String, String> ret = new HashMap<String, String>(); if(StringUtils.isEmpty(student.getUsername())){ ret.put("type", "error"); ret.put("msg", "学生姓名不能为空!"); return ret; } if(StringUtils.isEmpty(student.getPassword())){ ret.put("type", "error"); ret.put("msg", "学生登录密码不能为空!"); return ret; } if(student.getClazzId() == null){ ret.put("type", "error"); ret.put("msg", "请选择所属班级!"); return ret; } if(isExist(student.getUsername(), student.getId())){ ret.put("type", "error"); ret.put("msg", "该姓名已存在!"); return ret; } student.setSn(StringUtil.generateSn("S", "")); if(studentService.edit(student) <= 0){ ret.put("type", "error"); ret.put("msg", "学生添加失败!"); return ret; } ret.put("type", "success"); ret.put("msg", "学生修改成功!"); return ret; } /** * 添加学生信息 * @param student * @return */ @RequestMapping(value="/add",method=RequestMethod.POST) @ResponseBody public Map<String, String> add(Student student){ Map<String, String> ret = new HashMap<String, String>(); if(StringUtils.isEmpty(student.getUsername())){ ret.put("type", "error"); ret.put("msg", "学生姓名不能为空!"); return ret; } if(StringUtils.isEmpty(student.getPassword())){ ret.put("type", "error"); ret.put("msg", "学生登录密码不能为空!"); return ret; } if(student.getClazzId() == null){ ret.put("type", "error"); ret.put("msg", "请选择所属班级!"); return ret; } if(isExist(student.getUsername(), null)){ ret.put("type", "error"); ret.put("msg", "该姓名已存在!"); return ret; } student.setSn(StringUtil.generateSn("S", "")); if(studentService.add(student) <= 0){ ret.put("type", "error"); ret.put("msg", "学生添加失败!"); return ret; } ret.put("type", "success"); ret.put("msg", "学生添加成功!"); return ret; } /** * 删除学生信息 * @param ids * @return */ @RequestMapping(value="/delete",method=RequestMethod.POST) @ResponseBody public Map<String, String> delete( @RequestParam(value="ids[]",required=true) Long[] ids ){ Map<String, String> ret = new HashMap<String, String>(); if(ids == null || ids.length == 0){ ret.put("type", "error"); ret.put("msg", "请选择要删除的数据!"); return ret; } try { if(studentService.delete(StringUtil.joinString(Arrays.asList(ids), ",")) <= 0){ ret.put("type", "error"); ret.put("msg", "删除失败!"); return ret; } } catch (Exception e) { // TODO: handle exception ret.put("type", "error"); ret.put("msg", "该学生下存在其他信息,请勿冲动!"); return ret; } ret.put("type", "success"); ret.put("msg", "学生删除成功!"); return ret; } /** * 上传用户头像图片 * @param photo * @param request * @param response * @return * @throws IOException */ @RequestMapping(value="/upload_photo",method=RequestMethod.POST) @ResponseBody public Map<String, String> uploadPhoto(MultipartFile photo, HttpServletRequest request, HttpServletResponse response ) throws IOException{ Map<String, String> ret = new HashMap<String, String>(); if(photo == null){ //文件没有选择 ret.put("type", "error"); ret.put("msg", "请选择文件!"); return ret; } if(photo.getSize() > 10485760){ //文件没有选择 ret.put("type", "error"); ret.put("msg", "文件大小超过10M,请上传小于10M的图片!"); return ret; } String suffix = photo.getOriginalFilename().substring(photo.getOriginalFilename().lastIndexOf(".") + 1,photo.getOriginalFilename().length()); if(!"jpg,png,gif,jpeg".contains(suffix.toLowerCase())){ ret.put("type", "error"); ret.put("msg", "文件格式不正确,请上传jpg,png,gif,jpeg格式的图片!"); return ret; } String savePath = request.getServletContext().getRealPath("/") + "\\upload\\"; System.out.println(savePath); File savePathFile = new File(savePath); if(!savePathFile.exists()){ savePathFile.mkdir();//如果不存在,则创建一个文件夹upload } //把文件转存到这个文件夹下 String filename = new Date().getTime() + "." + suffix; photo.transferTo(new File(savePath + filename )); ret.put("type", "success"); ret.put("msg", "图片上传成功!"); ret.put("src", request.getServletContext().getContextPath() + "/upload/" + filename); return ret; } private boolean isExist(String username,Long id){ Student student = studentService.findByUserName(username); if(student != null){ if(id == null){ return true; } if(student.getId().longValue() != id.longValue()){ return true; } } return false; } }
运行配置
1、首先安装Mysql5.7,设置用户名为root,密码为root,并保证其在运行状态,并执行sql文件导入数据。
2、然后再配置Maven到环境变量中,在源代码目录下运行
3、使用浏览器访问http://localhost:8080即可进入系统。
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。