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) {
}
特点:
param 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) 参数的顺序⽆关
参考:
blog.csdn/pengfudian1991/article/details/96336898
blog.csdn/qq_36268103/article/details/109994954

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。