当我们要创建Mapper映射文件时,我们在idea中是没有模板的,我们需要从外部引入
2、解决:自己生成快捷代码段
有重复的代码,我们可以将其封装为一个方法
//添加一名用户 @PostMapping("/add") public void addUser(User user,HttpServletResponse response) throws IOException { int count = userService.addUser(user); response.setContentType("application/json;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); if (count>0){ String s = JSONObject.toJSONString("添加成功"); }else { String s = JSONObject.toJSONString("添加失败"); } response.getWriter().print(s); } //删除一名用户 @PostMapping("/delByUserId") public void delByUserId(Integer id,HttpServletResponse response) throws IOException { int count = userService.delByUserId(id); response.setContentType("application/json;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); if (count>0){ String s = JSONObject.toJSONString("删除成功"); }else { String s = JSONObject.toJSONString("删除失败"); } response.getWriter().print(s); } //修改一名用户 @PostMapping("/updateUser") public void updateUser(User user,HttpServletResponse response) throws IOException { int count = userService.updateUser(user); response.setContentType("application/json;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); if (count>0){ String s = JSONObject.toJSONString("修改成功"); }else { String s = JSONObject.toJSONString("修改失败"); } response.getWriter().print(s); } //查询一名用户 @PostMapping("/queryUserById") public void queryUserById(Integer id,HttpServletResponse response) throws IOException { User user = userService.queryUserById(id); response.setContentType("application/json;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String s = JSONObject.toJSONString(user); response.getWriter().print(s); }2、解决:选择要抽取的代码块
//添加一名用户 @PostMapping("/add") public void addUser(User user,HttpServletResponse response) throws IOException { int count = userService.addUser(user); if (count>0){ jsonResponse(response, "添加成功"); }else { jsonResponse(response,"添加失败"); } } //删除一名用户 @PostMapping("/delByUserId") public void delByUserId(Integer id,HttpServletResponse response) throws IOException { int count = userService.delByUserId(id); if (count>0){ jsonResponse(response, "删除成功"); }else { jsonResponse(response,"删除失败"); } } //修改一名用户 @PostMapping("/updateUser") public void updateUser(User user,HttpServletResponse response) throws IOException { int count = userService.updateUser(user); if (count>0){ jsonResponse(response, "修改成功"); }else { jsonResponse(response,"修改失败"); } } //查询一名用户 @PostMapping("/queryUserById") public void queryUserById(Integer id,HttpServletResponse response) throws IOException { User user = userService.queryUserById(id); jsonResponse(response, user); } private void jsonResponse(HttpServletResponse response, Object obj) throws IOException { response.setContentType("application/json;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String s = JSONObject.toJSONString(obj); response.getWriter().print(s); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)