zoukankan      html  css  js  c++  java
  • 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);
        }
  • 相关阅读:
    Azure的CentOS上安装LIS (Linux Integration Service)
    使用PowerShell在Azure China创建Data Warehouse
    通过php的MongoDB driver连接Azure的DocumentDB PaaS
    Azure RBAC管理ASM资源
    Azure基于角色的用户接入控制(RBAC)
    通过PowerShell命令给Azure VM添加CustomScriptExtension
    手把手教你创建Azure ARM Template
    MySQL数据表列转行
    MySQL
    MySQL游标使用
  • 原文地址:https://www.cnblogs.com/sitian2050/p/11692404.html
Copyright © 2011-2022 走看看