天天看點

@requestparam和@PathVariable和@param和@RequestParam差別[email protected]差別[email protected]與@RequestParam都為spring的注解,都可以用于在Controller層接收前端傳遞的資料,不過兩者的應用場景不同。[email protected]是mybatis注解,用于dao/mapper層,與mapper.xml字段對應[email protected]

[email protected]差別

@requestparam是spring的注解,主要用于controller中

解決前台參數名稱與背景接收參數變量名稱不一緻的問題,等價于request.getParam

value:參數名字,即入參的請求參數名字,如username表示請求的參數區中的name為
username的參數的值将傳入;

required:是否必須,預設是true,表示請求中一定要有相應的參數,否則将報404
錯誤碼;

defaultValue:預設值,表示如果請求中沒有同名參數時的預設值,預設值可以是
SpEL表達式,如“#{systemProperties['java.vm.version']}”。
           
@ResponseBody
    @RequestMapping("login")
    public String login(@RequestParam(value = "username") final String username,
                        @RequestParam(value = "password",required = false) final String password,
                        @RequestParam(value = "valcode",required = false) final String valcode) {
                        }             
           

接收請求行中URL後的查詢串參數,當通路URL為

localhost:8080/demo1?name=Aaron&age=18

時,将會把查詢串中的參數按名綁定到demo1方法的相應形參上

@RequestMapping(value="/demo1")
public void demo1 (@RequestParam String name, @RequestParam int age ){
   ...
}
           

[email protected]與@RequestParam都為spring的注解,都可以用于在Controller層接收前端傳遞的資料,不過兩者的應用場景不同。

@PathVariable主要用于接收http://host:port/path/{參數值}資料。

當通路URL為 localhost:8080/demo2/Bob/12時,将會把URL占位符的的參數按名綁定到demo2方法的相應形參上

@RequestMapping(value="/demo2/{name}/{id}")
public void demo2(@PathVariable String name, @PathVaribale int id){
	...
}
           

[email protected]是mybatis注解,用于dao/mapper層,與mapper.xml字段對應

public interface Mapper { 
   
@Select("select s_id id,s_name name,class_id classid"+ 
        "from student where  s_name= #{aaaa} and class_id = #{bbbb}") 
 public Student select(@Param("aaaa") String name,@Param("bbbb")int class_id);  
           

[email protected]

在post請求中,當通路URL為/user/UpdateAddressByUaId

@RequestMapping(value = "/user/UpdateAddressByUaId")
    public R UpdateAddressByUaId(@RequestBody UserAddress userAddress){
        R rrr = myService.confirmAddress(userAddress);
        return rrr;
    }
           

自用整理

原文連結:https://blog.csdn.net/exodus3/article/details/92762215 + https://blog.csdn.net/daponi/article/details/100739206+https://blog.csdn.net/qq_32683235/article/details/113878052