乘风原创程序

  • 基于SSM实现学生管理系统
  • 2020/12/17 11:08:40
  • 本文实例为大家分享了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即可进入系统。

    功能展示

    1. 首页登陆

    2.2 管理

    2.3 管理

    2.4 管理

    2.5 管理