SpringBoot,Spring 中常用注解@RequestMapping/@GetMapping/@PostMapping /@PutMapping/@DeleteMapping介绍

1、@Controller
@Controller 用来响应页面,表示当前的类为控制器。


2、@RestController
@RestController 是@ResponseBody和@Controller的结合。
表明当前类是控制器且返回的是一组数据,不是页面。


3、@Autowired
这个注解的作用是将其他的类,接口引入,类似于之前的类的初始化等,用这个注解,类中或接口的方法就可以直接调用了。


4、@RequestMapping
当前台界面调用Controller处理数据时候告诉控制器怎么操作
作用:URL映射。


5、@GetMapping
@RequestMapping(method = RequestMethod.GET)的简写
作用:对应查询,表明是一个查询URL映射

 /*restful 部分*/
    @GetMapping("/heroes")
    public PageInfo<Hero> list(@RequestParam(value = "start",defaultValue = "1") int start,@RequestParam(value = "size",defaultValue = "5") int size) throws Exception{
        PageHelper.startPage(start, size,"id desc");
        List<Hero> hs=heroService.list();
        System.out.println(hs.size());
        PageInfo<Hero> page = new PageInfo<>(hs,5);
        return page;
    }

    @GetMapping("/heroes/{id}")
    public Hero get(@PathVariable("id") int id) {
        System.out.println(id);
        Hero h = heroService.get(id);
        System.out.println(h);
        return h;
    }


6、@PostMapping
@RequestMapping(method = RequestMethod.POST)的简写
作用:对应增加,表明是一个增加URL映射

   @PostMapping("/heroes")
    public String add(@RequestBody Hero h){
        heroService.add(h);
        return "success";
    }


7、@PutMapping
@RequestMapping(method = RequestMethod.PUT)的简写
作用:对应更新,表明是一个更新URL映射

@PutMapping("/heroes/{id}")
    public String update(@RequestBody Hero h) throws Exception {
        heroService.update(h);
        return "success";
    }
     


8、@DeleteMapping
@RequestMapping(method = RequestMethod.DELETE)的简写
作用:对应删除,表明是一个删除URL映射
 

   @DeleteMapping("/heroes/{id}")
    public String delete(Hero h){
        heroService.delete(h.getId());
        return "success";
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值