当前位置: 首页 > 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.lryc.cn/news/469445.html

相关文章:

  • 从Docker容器中备份整个PostgreSQL
  • 从小需求看大格局:如何用技术智慧赢得客户信任
  • 模型 支付矩阵
  • 擎创科技声明
  • 二叉树习题其六【力扣】【算法学习day.13】
  • 互联网的无形眼睛:浏览器指纹与隐私保护攻略
  • 后端技术:有哪些常见的应用场景?
  • 【Unity 安装教程】
  • C++ 二级测试卷及答案
  • Java基础(7)图书管理系统
  • 使用 Spring Boot 实现图片上传
  • 深度解析跨境支付之产品架构
  • Linux下的线程同步与死锁避免
  • 【Python爬虫实战】Selenium自动化网页操作入门指南
  • mono源码交叉编译 linux arm arm64全过程
  • 矩阵的可解性:关于Ax=b的研究
  • 10.22.2024刷华为OD C题型(三)--for循环例子
  • QT:MaintenanceTool 模块安装工具
  • 同标签实现监听LocalStorage
  • JAVA高性能缓存项目
  • 智慧农业大数据平台:智汇田园,数驭未来
  • Go语言基础教程:可变参数函数
  • 高并发场景下解决并发数据不一致
  • OpenAI GPT-o1实现方案记录与梳理
  • Excel:vba实现生成随机数
  • Python | Leetcode Python题解之第506题相对名次
  • 安全见闻(6)
  • Promise、async、await 、异步生成器的错误处理方案
  • 腾讯云:数智教育专场-学习笔记
  • Ovis: 多模态大语言模型的结构化嵌入对齐