@PathVariable
//请求路径:http://196.0.0.1/getUser/{name}
//请求路径:http://196.0.0.1/getUser/tom//方法参数名称和需要绑定的url中变量名称一致时
@RequestMapping("/getUser/{name}")
public User getUser(@PathVariable String name){return userService.selectUser(name);
}
//方法参数名称和需要绑定的url中变量名称不一致时
@RequestMapping("/getUserById/{name}")
public User getUser(@PathVariable("name") String userName){return userService.selectUser(userName);
}
@RequestParam
//请求路径:http://127.0.0.1/user/?name=tom
@RequestMapping(value="/user")
public String getUserBlog(@RequestParam(value="name") String username) {return name;
}
@RequestBody
//前端传回的json数据时
//请求路径:http://127.0.0.1/user/addUser
@RestController
@RequestMapping("/user")
public class UserController {@PostMapping("/addUser")public void addUser(@RequestBody User user){System.out.println(user);}
}
@ResponseBody
@ResponseBody注解是写在方法上的
@RequestMapping后,返回值通常解析为跳转路径
异步获取 json 数据,加上 @ResponseBody 后,会直接返回 json 数据
@Param
服务于SQL语句中的参数数值,@Param 是mybatis注解
在DAO层函数
public User selectUser(@Param("userName") String name,@Param("userId") String id);
<select id="selectUser" resultMap="User"> select * from user where user_name = #{userName} and user_id=#{userId}
</select>