栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于SSM实现学生管理系统

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于SSM实现学生管理系统

本文实例为大家分享了SSM实现学生管理系统的具体代码,供大家参考,具体内容如下

概述

基于Spring + Spring MVC 的学生管理系统,使用Maven进行包管理。

主要代码

@RequestMapping("/student")
@Controller
public class StudentController {
 
 @Autowired
 private StudentService studentService;
 @Autowired
 private ClazzService clazzService;
 
 @RequestMapping(value="/list",method=RequestMethod.GET)
 public ModelAndView list(ModelAndView model){
 model.setViewName("student/student_list");
 List clazzList = clazzService.findAll();
 model.addObject("clazzList",clazzList );
 model.addObject("clazzListJson",JSONArray.fromObject(clazzList));
 return model;
 }
 
 
 @RequestMapping(value="/get_list",method=RequestMethod.POST)
 @ResponseBody
 public Map getList(
 @RequestParam(value="name",required=false,defaultValue="") String name,
 @RequestParam(value="clazzId",required=false) Long clazzId,
 HttpServletRequest request,
 Page page
 ){
 Map ret = new HashMap();
 Map queryMap = new HashMap();
 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;
 }
 
 
 @RequestMapping(value="/edit",method=RequestMethod.POST)
 @ResponseBody
 public Map edit(Student student){
 Map ret = new HashMap();
 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;
 }
 
 
 
 @RequestMapping(value="/add",method=RequestMethod.POST)
 @ResponseBody
 public Map add(Student student){
 Map ret = new HashMap();
 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;
 }
 
 
 @RequestMapping(value="/delete",method=RequestMethod.POST)
 @ResponseBody
 public Map delete(
 @RequestParam(value="ids[]",required=true) Long[] ids
 ){
 Map ret = new HashMap();
 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;
 }
 
 
 @RequestMapping(value="/upload_photo",method=RequestMethod.POST)
 @ResponseBody
 public Map uploadPhoto(MultipartFile photo,
 HttpServletRequest request,
 HttpServletResponse response
 ) throws IOException{
 Map ret = new HashMap();
 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即可进入系统。

功能展示

1. 首页登陆

2.2 管理

2.3 管理

2.4 管理

2.5 管理

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/129803.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号