Java 实战项目之学生信息管理系统的实现流程
程序员文章站
2022-08-06 18:23:28
一、项目简述功能包括: 用户的登录注册,学生信息管理,教师信息管理,班级信 息管理,采用mvcx项目架构,覆盖增删改查,包括学生, 教币班级的信息导出上传导入等等功能。二、项目运行环境配置: jdk1...
一、项目简述
功能包括: 用户的登录注册,学生信息管理,教师信息管理,班级信 息管理,采用mvcx项目架构,覆盖增删改查,包括学生, 教币班级的信息导出上传导入等等功能。
二、项目运行
环境配置: jdk1.8 + tomcat8.5 + mysql + eclispe (intellij idea,eclispe,myeclispe,sts 都支持)
项目技术: jsp +spring + springmvc + mybatis + html+ css + javascript + jquery + ajax + maven等等。
用户信息管理控制器:
/** * 用户管理控制器 */ @requestmapping("/user/") @controller public class usercontroller { @autowired private iuserservice userservice; @autowired private iroleservice roleservice; @resource private processengineconfiguration configuration; @resource private processengine engine; @getmapping("/index") @apioperation("跳转用户页接口") @preauthorize("hasrole('管理员')") public string index(string menuid,model model){ list<role> roles = queryallrole(); model.addattribute("roles",roles); model.addattribute("menuid",menuid); //用户首页 return "views/user/user_list"; } @getmapping("/listpage") @apioperation("查询用户分页数据接口") @apiimplicitparams({ @apiimplicitparam(name = "userquery", value = "用户查询对象", defaultvalue = "userquery对象") }) @responsebody @preauthorize("hasrole('管理员')") public pagelist listpage(userquery userquery){ return userservice.listpage(userquery); } //添加用户 @postmapping("/adduser") @apioperation("添加用户接口") @responsebody public map<string,object> adduser(user user){ map<string, object> ret = new hashmap<>(); ret.put("code",-1); if(stringutils.isempty(user.getusername())){ ret.put("msg","请填写用户名"); return ret; } if(stringutils.isempty(user.getpassword())){ ret.put("msg","请填写密码"); return ret; } if(stringutils.isempty(user.getemail())){ ret.put("msg","请填写邮箱"); return ret; } if(stringutils.isempty(user.gettel())){ ret.put("msg","请填写手机号"); return ret; } if(stringutils.isempty(user.getheadimg())){ ret.put("msg","请上传头像"); return ret; } if(userservice.adduser(user)<=0) { ret.put("msg", "添加用户失败"); return ret; } ret.put("code",0); ret.put("msg","添加用户成功"); return ret; } /** * 修改用户信息操作 * @param user * @return */ @postmapping("/editsaveuser") @apioperation("修改用户接口") @preauthorize("hasrole('管理员')") @responsebody public message editsaveuser(user user){ if(stringutils.isempty(user.getusername())){ return message.error("请填写用户名"); } if(stringutils.isempty(user.getemail())){ return message.error("请填写邮箱"); } if(stringutils.isempty(user.gettel())){ return message.error("请填写手机号"); } try { userservice.editsaveuser(user); return message.success(); } catch (exception e) { e.printstacktrace(); return message.error("修改用户信息失败"); } } //添加用户 @getmapping("/deleteuser") @apioperation("删除用户接口") @apiimplicitparams({ @apiimplicitparam(name = "id", value = "如:88",required = true) }) @preauthorize("hasrole('管理员')") @responsebody public ajaxresult deleteuser(@requestparam(required = true) long id){ ajaxresult ajaxresult = new ajaxresult(); try { userservice.deleteuser(id); } catch (exception e) { e.printstacktrace(); return new ajaxresult("删除失败"); } return ajaxresult; } @postmapping(value="/deletebatchuser") @apioperation("批量删除用户接口") @preauthorize("hasrole('管理员')") @responsebody public ajaxresult deletebatchuser(string ids){ string[] idsarr = ids.split(","); list list = new arraylist(); for(int i=0;i<idsarr.length;i++){ list.add(idsarr[i]); } try{ userservice.batchremove(list); return new ajaxresult(); }catch(exception e){ return new ajaxresult("批量删除失败"); } } //查询所有角色 public list<role> queryallrole(){ return roleservice.queryall(); } //添加用户的角色 @postmapping("/adduserrole") @apioperation("添加用户角色接口") @apiimplicitparams({ @apiimplicitparam(name = "parammap", value = "如:{userid:1,[1,2,3,4]]}") }) @responsebody public ajaxresult adduserrole(@requestbody map parammap){ ajaxresult ajaxresult = new ajaxresult(); string userid = (string)parammap.get("userid"); list roleids = (list) parammap.get("roleids"); try { //添加用户对应的角色 roleservice.adduserrole(userid,roleids); return ajaxresult; }catch (exception e){ e.printstacktrace(); return new ajaxresult("保存角色失败"); } } //添加用户 @requestmapping("/regsaveuser") @responsebody public long addteacher(user user){ system.out.println("保存用户...."+user); userservice.adduser(user); //保存工作流程操作 identityservice is = engine.getidentityservice(); // 添加用户组 org.activiti.engine.identity.user userinfo = userservice.saveuser(is, user.getusername()); // 添加用户对应的组关系 group stugroup = new groupentityimpl(); stugroup.setid("stugroup"); group tgroup = new groupentityimpl(); tgroup.setid("tgroup"); if(user.gettype() == 2) { //保存老师组 userservice.saverel(is, userinfo, tgroup); } if(user.gettype() == 3) { //保存学生组 userservice.saverel(is, userinfo, stugroup); } long userid = user.getid(); return userid; } /** * 修改密码页面 * @return */ @requestmapping(value="/update_pwd",method=requestmethod.get) public string updatepwd(){ return "views/user/update_pwd"; } /** * 修改密码操作 * @param oldpwd * @param newpwd * @return */ @responsebody @postmapping("/update_pwd") public message updatepassword(@requestparam(name="oldpwd",required=true)string oldpwd, @requestparam(name="newpwd",required=true)string newpwd){ string username = commonutils.getloginuser().getusername(); user userbyusername = userservice.finduserbyusername(username); if(userbyusername!=null){ string password = userbyusername.getpassword(); bcryptpasswordencoder bcryptpasswordencoder = new bcryptpasswordencoder(); boolean matches = bcryptpasswordencoder.matches(oldpwd, password); if(!matches){ return message.error("旧密码不正确");//true } userbyusername.setpassword(bcryptpasswordencoder.encode(newpwd)); if(userservice.edituserpassword(userbyusername)<=0){ return message.error("密码修改失败"); } } return message.success(); } /** * 清除缓存 * @param request * @param response * @return */ @responsebody @postmapping("/clear_cache") public message clearcache(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setheader("cache-control","no-store"); response.setheader("pragrma","no-cache"); response.setdateheader("expires",0); return message.success(); } }
教师管理接口控制器:
@requestmapping("/teacher") @controller @api(tags = "教师管理接口") public class teachercontroller { @autowired private iuserservice userservice; @autowired private iclassesservice classesservice; @resource private processengine engine; @autowired private ioperaterlogservice operaterlogservice; //老师列表 @requestmapping("/index") public string index(model model){ //用户首页 model.addattribute("classes",classesservice.queryall()); return "views/teacher/teacher_list"; } @requestmapping("/listpage") @responsebody public pagelist listpage(userquery userquery){ userquery.settype(2l);//2表示老师 return userservice.listpage(userquery); } /** * 添加教师操作 * @param user * @return */ @postmapping("/addteacher") @apioperation("添加教师接口") @responsebody @preauthorize("hasrole('管理员')") public message addteacher(user user){ if(stringutils.isempty(user.getusername())){ return message.error("请填写用户名"); } if(stringutils.isempty(user.getpassword())){ return message.error("请填写密码"); } if(stringutils.isempty(user.getemail())){ return message.error("请填写邮箱"); } if(stringutils.isempty(user.gettel())){ return message.error("请填写手机号"); } user userbyusername = userservice.finduserbyusername(user.getusername()); if(userbyusername!=null){ return message.error("用户名已存在"); } user.settype(2); if(userservice.adduser(user)<=0){ return message.error("教师添加失败"); } //保存工作流程操作 identityservice is = engine.getidentityservice(); // 添加用户组 org.activiti.engine.identity.user userinfo = userservice.saveuser(is, user.getusername()); group tgroup = new groupentityimpl(); tgroup.setid("tgroup"); userservice.saverel(is, userinfo, tgroup); operaterlogservice.add("添加教师成功,教师名称:"+user.getusername()); return message.success(); } /** * 修改教师操作 * @param user * @return */ @postmapping("/editsavestu") @apioperation("修改教师接口") @preauthorize("hasrole('管理员')") @responsebody public message editsavestu(user user){ if(stringutils.isempty(user.getusername())){ return message.error("请填写用户名"); } if(stringutils.isempty(user.getemail())){ return message.error("请填写邮箱"); } if(stringutils.isempty(user.gettel())){ return message.error("请填写手机号"); } user userbyusername = userservice.finduserbyusername(user.getusername()); if(userbyusername!=null){ if(!userbyusername.getid().equals(user.getid())){ return message.error("该用户名已存在"); } } try { userservice.editsaveuser(user); operaterlogservice.add("编辑教师成功,教师名称:"+user.getusername()); return message.success(); } catch (exception e) { e.printstacktrace(); return message.error("教师信息编辑失败"); } } }
学生管理接口控制器:
@requestmapping("/student") @controller @api(tags = "学生管理接口") public class studentcontroller { @autowired private iuserservice userservice; @autowired private icourseservice courseservice; @autowired private iclassesservice classesservice; @resource private processengine engine; @autowired private ioperaterlogservice operaterlogservice; //老师列表 //后台查询学生列表 @requestmapping("/index") public string index(model model){ //用户首页 model.addattribute("courses",courseservice.queryall()); model.addattribute("classes",classesservice.queryall()); return "views/student/student_list"; } @requestmapping("/listpage") @responsebody public pagelist listpage(userquery userquery){ userquery.settype(3l);//2表示老学生 return userservice.listpage(userquery); } /** * 添加学生操作 * @param user * @return */ @postmapping("/addsavestu") @apioperation("添加学生接口") @preauthorize("hasrole('管理员')") @responsebody public message addsavestudent(user user){ if(stringutils.isempty(user.getusername())){ return message.error("请填写用户名"); } if(stringutils.isempty(user.getpassword())){ return message.error("请填写密码"); } if(stringutils.isempty(user.getemail())){ return message.error("请填写邮箱"); } if(stringutils.isempty(user.gettel())){ return message.error("请填写手机号"); } if(stringutils.isempty(user.getstunum())){ return message.error("请填写学号"); } user userbyusername = userservice.finduserbyusername(user.getusername()); if(userbyusername!=null){ return message.error("用户名已存在"); } if(userservice.findbystunum(user.getstunum())!=null){ return message.error("该学号已存在"); } user.settype(3); if(userservice.adduser(user)<=0){ return message.error("学生添加失败"); } //保存工作流程操作 identityservice is = engine.getidentityservice(); // 添加用户组 org.activiti.engine.identity.user userinfo = userservice.saveuser(is, user.getusername()); // 添加用户对应的组关系 group stugroup = new groupentityimpl(); stugroup.setid("stugroup"); userservice.saverel(is, userinfo, stugroup); operaterlogservice.add("添加学生成功,学生名称:"+user.getusername()); return message.success(); } //修改用户editsaveuser @postmapping("/editsavestu") @apioperation("修改学生接口") @preauthorize("hasrole('管理员')") @responsebody public message editsavestu(user user){ if(stringutils.isempty(user.getusername())){ return message.error("请填写用户名"); } if(stringutils.isempty(user.getemail())){ return message.error("请填写邮箱"); } if(stringutils.isempty(user.gettel())){ return message.error("请填写手机号"); } user userbyusername = userservice.finduserbyusername(user.getusername()); if(userbyusername!=null){ if(!userbyusername.getid().equals(user.getid())){ return message.error("该用户名已存在"); } } try { userservice.editsaveuser(user); operaterlogservice.add("编辑学生成功,学生名称:"+user.getusername()); return message.success(); } catch (exception e) { e.printstacktrace(); return message.error("学生编辑失败"); } } //editsavestuxk @postmapping("/editsavestuxk") @apioperation("修改用户接口") @apiimplicitparams({ @apiimplicitparam(name = "user", value = "学生选课") }) @responsebody public ajaxresult editsavestuxk(user user){ try { userservice.editsavexk(user); operaterlogservice.add("修改学生选课成功,学生名称:"+user.getusername()); return new ajaxresult(); } catch (exception e) { e.printstacktrace(); } return new ajaxresult("修改失败"); } }