idea快捷生成代码段和方法

idea快捷生成代码段和方法,第1张

idea快捷生成代码段和方法 一、生成代码段 1、事件原型

当我们要创建Mapper映射文件时,我们在idea中是没有模板的,我们需要从外部引入

2、解决:自己生成快捷代码段


3、结果 3.1 新建xml文件

3.2 输入m…即可

3.3 自动生成

二、抽取封装方法 1、事件原型

有重复的代码,我们可以将其封装为一个方法

    //添加一名用户
    @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、解决:选择要抽取的代码块


3、结果:生成了jsonResponse方法
    //添加一名用户
    @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);
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5437903.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存