• msql中@RequestParam、@Param、@PathVariable的用法


    @RequestParam的用法

    1.可以对传入参数指定参数名,将请求参数绑定至方法参数

    // 下面的对传入参数指定为aa,如果前端不传aa参数名,会报错  
     @RequestParam(value="aa") String inputStr  

    2.可以通过required=false或者true来要求@RequestParam配置的前端参数是否一定要传 

    //required的值,默认为true,表示请求中一定要有相应的参数,否则将报错
    public
    String filesUpload(@RequestParam(value="aa", required=true) String inputStr, HttpServletRequest request)

    3.如果@requestParam注解的参数是int类型,并且required=false,此时如果不传参数的话,会报错。原因是,required=false时,不传参数的话,会给参数赋值null,这样就会把null赋值给了int,因此会报错。

    public String filesUpload(@RequestParam(value="aa", required=false) int inputStr, HttpServletRequest request) 
    //若是前端页面不传参的话,此处就会报错。当然可以用Integer代替int

    4.defaultValue:默认值,表示如果请求中没有同名参数时的默认值

     public ResponseEntity<EasyUIResult> querycategoryId(@RequestParam(@RequestParam(value="page",defaultValue="1")Integer page,
                    @RequestParam(value="rows",defaultValue="10")Integer rows )

    @Param的用法

    用来注解单一属性,当用注解来简化xml配置的时候(比如Mybatis的Mapper.xml中的sql参数引入),@Param注解的作用是给参数命名,参数命名后就能根据名字得到参数值,正确的将参数传入sql语句中(一般通过#{}的方式,${}会有sql注入的问题)。

    1.使用@Param注解

    Mapper接口方法:

    public int getUsersDetail(@Param("uid") int userid);

    2.对应的Sql Mapper.xml文件

     <select id="selectById" resultType="com.taotao.bean" >
               select * from user where userid=#{uid}
     </select>

    @PathVariable的用法

    • @PathVariable 映射 URL 绑定的占位符
    • 带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义
    • 通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx“) 绑定到操作方法的入参中。
    //@PathVariable可以用来映射URL中的占位符到目标方法的参数中
    @RequestMapping(value="{itemCatId}",method=RequestMethod.GET)
        public ResponseEntity<ItemCat> findByItemId(@PathVariable(value="itemCatId")Integer itemCatId){
            try {
                ItemCat itemCat=itemCatService.queryById(itemCatId);
                return ResponseEntity.ok(itemCat);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
  • 相关阅读:
    web.xml中/与/*的区别
    restController相关
    mvc:resources
    RequestMethod 相关
    Springside学习
    命名规范的反思
    C++ 构造中调用构造
    C++ 匿名对象的生命周期
    C++ 构造函数的对象初始化列表
    C++ 类的构造函数使用规则
  • 原文地址:https://www.cnblogs.com/sitian2050/p/11692404.html
Copyright © 2020-2023  润新知