当前位置: 首页 > news >正文

Springboot项目控制层注释

Springboot主流的 ----------------------- 简略写法

package com.dx.wlmq.controller;import com.dx.wlmq.domain.Address;
import com.dx.wlmq.service.AddresssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController                // 相当于 @Controller+@ResponseBody
@CrossOrigin(origins = "*")    // 取消跨域
@RequestMapping("/address") // 访问路径
public class AddresssController {@Autowired  // 自动装配AddresssService addresssService;/*** 查询方法* *///相当于 @RequestMapping(value = "/list", method = RequestMethod.GET,produces = "text/html;charset=UTF-8" )@GetMapping(value = "/list")public Object index11() {return addresssService.Addresslist();}//    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")@PostMapping("/add")public Object index12(@RequestBody Address address) {return addresssService.Addressadd(address);}//    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")@DeleteMapping("/delete")public Object index13(Integer id) {return addresssService.Addressdelete(id);}//    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")@PutMapping(value = "/update")public Object index14(@RequestBody Address address) {return addresssService.Addressupdate(address);}}

原Mybits主流写法(案例)

package com.dx.wlmq.controller;import com.alibaba.fastjson.JSON;
import com.dx.wlmq.domain.Role;
import com.dx.wlmq.service.RolesService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;@Controller
@RequestMapping("/roles")
public class RolesController {@Resource(name = "rolesService")RolesService rolesService;@RequestMapping(value = "/list", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")@ResponseBodypublic Object index11(HttpServletResponse response) {response.setContentType("text/html;charset=UTF-8");return JSON.toJSONString(rolesService.Rolelist());}@RequestMapping(value = "/add", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")@ResponseBodypublic Object index12(HttpServletResponse response,@RequestBody Role role) {response.setContentType("text/html;charset=UTF-8");return JSON.toJSONString(rolesService.Roleadd(role));}@RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")@ResponseBodypublic Object index13(HttpServletResponse response, Integer id) {response.setContentType("text/html;charset=UTF-8");return JSON.toJSONString(rolesService.Roledelete(id));}@RequestMapping(value = "/update", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")@ResponseBodypublic Object index14(HttpServletResponse response,@RequestBody Role role) {response.setContentType("text/html;charset=UTF-8");return JSON.toJSONString(rolesService.Roleupdate(role));}}

Springboot主流的 ----------------------- 简略写法(优点:写法更简略,更直接)

简单写法的笔记:

《《《《《《《《《------   springboot@注释解析最简写法    ------》》》》》》》》》


在控制层开头:

书写@RestController                // 相当于 @Controller+@ResponseBody
添加取消跨域注释 @CrossOrigin(origins = "*")

书写类,然后在第一行添加 @Autowired  //作用是自动装配 ,装配了业务层的调用实例   
    private SysEmpService sysEmpService;

然后增删改查都有不同的Mapping注释,当这样写注解的时候,return的值直接为JSON格式,详细如下:
    /**
     * 查询方法注解写法
     * */
    @GetMapping(value = "/list")    //相当于 @RequestMapping(value = "/list", method = RequestMethod.GET,produces = "text/html;charset=UTF-8" )
    public Object index11() {
        return addresssService.Addresslist();
    }

     /**
     * 增加方法注解写法
     * */
    @PostMapping("/add")           // 相当于   @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public Object index12(@RequestBody Address address) {
        return addresssService.Addressadd(address);
    }

    /**
     * 删除方法注解写法
     * */
    @DeleteMapping("/delete")   //相当于    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public Object index13(Integer id) {
        return addresssService.Addressdelete(id);
    }

    /**
     * 修改方法注解写法
     * */
    @PutMapping(value = "/update")   //相当于 @RequestMapping(value="/update", method=RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public Object index14(@RequestBody Address address) {
        return addresssService.Addressupdate(address);
    }


在服务层,将调用Dao层的时用装配注入的注解@Autowired,但服务层实现类里的@Service("rolesService")不能省略

在Dao层的mapper里面添加注解:@Mapper


http://www.mrgr.cn/news/55667.html

相关文章:

  • Docker 入门 - 拉取/创建镜像 + 运行和管理容器
  • 使用finalshell远程ssh连接ubuntu
  • 【第三章·基本算术运算】第四节:自动类型转换与强制类型转换运算符
  • msvcp140.dll丢失的解决方法吃鸡
  • 生成模型初认识
  • 同步防击穿与异步防击穿策略的区别及适用场景
  • Axure大屏可视化模板:打造跨领域数据分析平台的原型设计案例
  • 10月20日
  • PMP敏捷专题课:敏捷实践指南
  • pushgateway的快速使用教程(附Java版通用推送代码)
  • EWM 库存盘点
  • 数组转List
  • Python机器学习中的主成分分析(PCA)全面解析与应用
  • jmeter学习(8)界面的使用
  • 旧电脑安装Win11提示“这台电脑当前不满足windows11系统要求”,安装中断。怎么办?
  • 比亚迪智驾变阵:整合自研,冲刺量产,目标让10万级也搭NOA
  • 红队老子养成记3 - 学会反弹shell的多种姿势,拿shell拿手软!!(全网最多姿势!)
  • IO编程——消息队列
  • 【H2O2|全栈】JS入门知识(五)
  • 碰到这个问题请更新或重新安装fastapi版本
  • 时序分解 | Matlab实现EEMD+SE集合经验模态分解+样本熵计算的时间序列信号分解
  • Android 性能之 Game Mode
  • 基于微信小程序的驾校预约系统设计与实现
  • P3924 康娜的线段树
  • 2025医药化工水处理新技术、新工艺、新装备发展论坛3月4日济南举办
  • 【VUE小型网站开发】优化通用配置