zoukankan      html  css  js  c++  java
  • Spring中三个注解@PathVariable、@Param和@RequestParam间的区别

    @PathVariable

    代码示例:

    @ResponseBody
    @RequestMapping("/user/{uid}")
    public User getUserById(@PathVariable("uid") Long uid) {
    }
    

    特点:

      1) 应用在Controller层

      2) @PathVariable是spring3.0的一个新功能:可接收请求路径中占位符的值,通过 @PathVariable 可以将URL中占位符参数{uid}绑定到处理器类的方法形参uid中 —— @PathVariable(“uid“)

      3) 请求路径中占位符的名字可与方法参数名不一样

    @RequestParam

    代码示例:

    @ResponseBody
    @RequestMapping(value = "/user/get", method = RequestMethod.POST)
    public List<User> getUserList(@RequestParam("uid") Integer id, @RequestParam("uname") String name) {
    }
    

    特点:

      1) @RequestParam主要应用在Controller层

      2) 前端提交的form表单数据中的属性名和方法中的参数名不一致时 ,springMVC就无法自动封装参数,所以需要 @RequestParam("前端所传属性名") 来指定前端提交的表单的属性的名称

      3) 前端提交的form表单数据中的属性名和方法中的参数名一致时,可以不使用此注解

    @Param

    代码示例:

    @Select("select * from user where uid = #{uid} and uname = #{uname}")
    List<User> getUserList(@Param("uid") Integer id, @Param("uname") String name);
    

    特点:

      1) @Param主要应用在Dao层

      2) 注解中的sql语句有多个条件参数,且和方法中的参数名称不一致,此时可以使用@Param注解

      3) 只有一个参数时,可以不使用注解(不过还是建议使用= =)

      4) 参数的顺序无关

    参考:

    https://blog.csdn.net/pengfudian1991/article/details/96336898

    https://blog.csdn.net/qq_36268103/article/details/109994954

  • 相关阅读:
    C#中async/await中的异常处理
    Xunit
    Markdown安装与简单使用
    .Net Ajax跨域请求总结
    centos 安装 Vmare tool
    linux安装Java
    linux常用命令整理
    autofac解析Mvc和Webapi的坑
    swarm on ubuntu
    deploy service on swarm
  • 原文地址:https://www.cnblogs.com/sfnz/p/15060192.html
Copyright © 2011-2022 走看看