毕业设计|基于SSM+JSP的家校通管理系统(一)https://developer.aliyun.com/article/1423936
四,核心代码展示
4.1 教室管理
@Controller @RequestMapping("/classroom") public class ClassroomController extends BaseController { /** * 依赖注入 start dao/service/=== */ @Autowired private ClassroomService classroomService; @Autowired private SchoolService schoolService; @Autowired private GradeService gradeService; @Autowired private TeacherService teacherService; /** * 分页查询 返回list对象(通过对象) * * @param request * @param response * @return */ @RequestMapping(value = "/findByObj.do") public String findByObj(Classroom classroom, Model model, HttpServletRequest request, HttpServletResponse response,HttpSession session) { Pager<Classroom> pagers=new Pager<Classroom>(); Integer schoolId = (Integer) session.getAttribute("schoolId"); if(!isEmpty(classroom.getName())){ Map<String, Object> map = getMap(); map.put("name",classroom.getName()); map.put("schoolId",schoolId); //模糊查询分页查询 pagers = classroomService.findByMap(map); }else{ classroom.setSchoolId(schoolId); //分页查询 pagers = classroomService.findByEntity(classroom); } for (Classroom classroom1 : pagers.getDatas()) { Teacher obj = teacherService.load(classroom1.getId()); if(obj!=null){ classroom1.setTeacherName(obj.getRealName()); } Grade g=gradeService.load(classroom1.getGradeId()); if(g!=null){ classroom1.setGradeName(g.getName()); } } model.addAttribute("pagers", pagers); //存储查询条件 model.addAttribute("obj", classroom); return "classroom/classroom"; } /** * 分页查询 返回list对象(通过Map) * * @param request * @param response * @return */ @RequestMapping(value = "/findByMap.do") public String findByMap(Classroom classroom, Model model, HttpServletRequest request, HttpServletResponse response,HttpSession session) { //通过map查询 Map<String,Object> params = new HashMap<String,Object>(); if(!isEmpty(classroom.getName())){ params.put("name", classroom.getName()); } if(!isEmpty(classroom.getGradeId())){ params.put("gradeId", classroom.getGradeId()); } if(!isEmpty(classroom.getIsDelete())){ params.put("isDelete", classroom.getIsDelete()); } //分页查询 Pager<Classroom> pagers = classroomService.findByMap(params); model.addAttribute("pagers", pagers); //存储查询条件 model.addAttribute("obj", classroom); return "classroom/classroom"; } /** * 跳至添加页面 * @return */ @RequestMapping(value = "/add.do") public String add(Model model,HttpSession session) { Integer schoolId = (Integer) session.getAttribute("schoolId"); School school=new School(); school.setId(schoolId); List<School> schoolList=schoolService.listAllByEntity(school); model.addAttribute("schoolList", schoolList); return "classroom/add"; } /** * 添加执行 * @return */ @RequestMapping(value = "/exAdd.do") public String exAdd(Classroom classroom, Model model, HttpServletRequest request, HttpServletResponse response) { classroom.setIsDelete(0); classroom.setStatus(0); classroomService.insert(classroom); return "redirect:/classroom/findByObj.do"; } /** * 跳至修改页面 * @return */ @RequestMapping(value = "/update.do") public String update(Integer id,Model model) { Classroom obj = classroomService.load(id); model.addAttribute("obj",obj); return "classroom/update"; } /** * 添加修改 * @return */ @RequestMapping(value = "/exUpdate.do") public String exUpdate(Classroom classroom, Model model, HttpServletRequest request, HttpServletResponse response) { classroomService.update(classroom); return "redirect:/classroom/findByObj.do"; } /** * 删除通过主键 * @return */ @RequestMapping(value = "/delete.do") public String delete(Integer id, Model model, HttpServletRequest request, HttpServletResponse response) { //真正删除 classroomService.deleteById(id); //通过参数删除 //Map<String,Object> params = new HashMap<String,Object>(); //params.put("id", id); //classroomService.deleteBySqId("deleteBySql", params); //状态删除 //Classroom load = classroomService.load(id); //load.setIsDelete(1); //classroomService.update(load); return "redirect:/classroom/findByObj.do"; } // --------------------------------------- 华丽分割线 ------------------------------ /** * 通过id查询 返回list json(通过map) * * @param request * @param response * @return */ @RequestMapping(value = "/findByGradeId.json", produces=MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) @ResponseBody public String findByGradeId(Classroom classroom, Model model, HttpServletRequest request, HttpServletResponse response) { Map<String, Object> map = getMap(); map.put("gradeId",classroom.getGradeId()); List<Classroom> classroomList =classroomService.list("listBySqlId", map); //分页查询 JSONObject jsonObject = JsonUtil2.getJsonObject(); jsonObject.put("obj", classroomList); return jsonObject.toString(); } /** * 分页查询 返回list json(通过对象) * * @param request * @param response * @return */ @RequestMapping(value = "/findByObj.json", produces=MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) @ResponseBody public String findByObjByEntity(Classroom classroom, Model model, HttpServletRequest request, HttpServletResponse response) { //分页查询 Pager<Classroom> pagers = classroomService.findByEntity(classroom); JSONObject jsonObject = JsonUtil2.getJsonObject(); jsonObject.put("pagers", pagers); jsonObject.put("obj", classroom); return jsonObject.toString(); } /** * 分页查询 返回list json(通过Map) * * @param request * @param response * @return */ @RequestMapping(value = "/findByMap.json", produces=MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) @ResponseBody public String findByMapMap(Classroom classroom, Model model, HttpServletRequest request, HttpServletResponse response) { //通过map查询 Map<String,Object> params = new HashMap<String,Object>(); if(!isEmpty(classroom.getName())){ params.put("name", classroom.getName()); } if(!isEmpty(classroom.getGradeId())){ params.put("gradeId", classroom.getGradeId()); } if(!isEmpty(classroom.getIsDelete())){ params.put("isDelete", classroom.getIsDelete()); } //分页查询 Pager<Classroom> pagers = classroomService.findByMap(params); JSONObject jsonObject = JsonUtil2.getJsonObject(); jsonObject.put("pagers", pagers); jsonObject.put("obj", classroom); return jsonObject.toString(); } /** * ajax 添加 * @param * @return */ @RequestMapping(value = "/exAdd.json", produces=MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) @ResponseBody public String exAddJson(Classroom classroom, Model model, HttpServletRequest request, HttpServletResponse response) { classroomService.insert(classroom); JSONObject jsonObject = JsonUtil2.getJsonObject(); jsonObject.put("message", "添加成功"); return jsonObject.toString(); } /** * ajax 修改 * @param * @return */ @RequestMapping(value = "/exUpdate.json", produces=MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) @ResponseBody public String exUpdateJson(Classroom classroom, Model model, HttpServletRequest request, HttpServletResponse response) { classroomService.update(classroom); JSONObject jsonObject = JsonUtil2.getJsonObject(); jsonObject.put("message", "修改成功"); return jsonObject.toString(); } /** * ajax 删除 * @return */ @RequestMapping(value = "/delete.json", produces=MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) @ResponseBody public String exDeleteJson(Integer id, Model model, HttpServletRequest request, HttpServletResponse response) { //真正删除 classroomService.deleteById(id); //通过参数删除 //Map<String,Object> params = new HashMap<String,Object>(); //params.put("id", id); //classroomService.deleteBySqId("deleteBySql", params); //状态删除 //Classroom load = classroomService.load(id); //load.setIsDelete(1); //classroomService.update(load); JSONObject jsonObject = JsonUtil2.getJsonObject(); jsonObject.put("message", "删除成功"); return jsonObject.toString(); } /** * 单文件上传 * @param file * @param request * @param model * @return */ @RequestMapping(value = "/saveFile") public String saveFile(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, Model model) { System.out.println("开始"); String path = request.getSession().getServletContext().getRealPath("/upload"); String fileName = file.getOriginalFilename(); System.out.println(path); File targetFile = new File(path, fileName); if(!targetFile.exists()){ targetFile.mkdirs(); } //保存 try { file.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } return ""; } /** * springMvc多文件上传 * @param files * @param id * @return */ @RequestMapping(value = "/saveFiles") public String saveFiles(@RequestParam("file") CommonsMultipartFile[] files,Integer id,HttpServletRequest request){ for(int i = 0;i<files.length;i++){ System.out.println("fileName---------->" + files[i].getOriginalFilename()); if(!files[i].isEmpty()){ int pre = (int) System.currentTimeMillis(); try { //拿到输出流,同时重命名上传的文件 String filePath = request.getRealPath("/upload"); File f=new File(filePath); if(!f.exists()){ f.mkdirs(); } String fileNmae=new Date().getTime() + files[i].getOriginalFilename(); File file=new File(filePath+"/"+pre + files[i].getOriginalFilename()); if(!file.exists()){ file.createNewFile(); } files[i].transferTo(file); } catch (Exception e) { e.printStackTrace(); System.out.println("上传出错"); } } } return ""; } }
毕业设计|基于SSM+JSP的家校通管理系统(三)https://developer.aliyun.com/article/1423938