Springboot中@PathVariable,@RequestParam,@RequestBody,HttpServletRequest区别

@PathVariable

//接收请求URL中占位符的值
@GetMapping("/getId/{id}")//{id}写在请求URL中,参数来源于URL中,所以@PathVariable可以多个
//单个入参时id可不写,多个入参需要写
public String pathVariable(@PathVariable("id") Integer id) {

}

@RequestParam

//将URL的请求参数绑定到控制器的方法参数上
@GetMapping("/getId")//例:getId?id=0001,参数来源于URL中,所以@RequestParam可以多个
//单个入参时id可不写,甚至@RequestParam("id")都可不写,多个入参需要写全
public String requestParam(@RequestParam("id") Integer id) {

}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Spring Boot和MyBatis搭建一个web项目,并自定义学生表,并运用注解完成增删改查接口。同时,通过拦截器来打印每个接口的用时。 首先,确保你已经配置好了Spring Boot和MyBatis的环境。然后,创建一个名为Student的实体类,包含学生的信息。 ```java public class Student { private Long id; private String name; private Integer age; // 省略getter和setter方法 } ``` 接下来,创建一个名为StudentMapper的接口,用于定义数据库操作的方法。 ```java @Mapper public interface StudentMapper { @Insert("INSERT INTO student(name, age) VALUES(#{name}, #{age})") @Options(useGeneratedKeys = true, keyProperty = "id") int insert(Student student); @Delete("DELETE FROM student WHERE id = #{id}") int deleteById(Long id); @Update("UPDATE student SET name = #{name}, age = #{age} WHERE id = #{id}") int update(Student student); @Select("SELECT * FROM student WHERE id = #{id}") Student findById(Long id); @Select("SELECT * FROM student") List<Student> findAll(); } ``` 然后,创建一个名为StudentController的控制器类,用于处理请求。 ```java @RestController @RequestMapping("/students") public class StudentController { private final StudentMapper studentMapper; public StudentController(StudentMapper studentMapper) { this.studentMapper = studentMapper; } @PostMapping public Long createStudent(@RequestBody Student student) { studentMapper.insert(student); return student.getId(); } @DeleteMapping("/{id}") public void deleteStudent(@PathVariable Long id) { studentMapper.deleteById(id); } @PutMapping("/{id}") public void updateStudent(@PathVariable Long id, @RequestBody Student student) { student.setId(id); studentMapper.update(student); } @GetMapping("/{id}") public Student getStudent(@PathVariable Long id) { return studentMapper.findById(id); } @GetMapping public List<Student> getAllStudents() { return studentMapper.findAll(); } } ``` 接下来,创建一个名为LoggingInterceptor的拦截器类,用于打印每个接口的用时。 ```java @Component public class LoggingInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { request.setAttribute("startTime", System.currentTimeMillis()); return true; } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { long startTime = (long) request.getAttribute("startTime"); long endTime = System.currentTimeMillis(); long elapsedTime = endTime - startTime; String requestURI = request.getRequestURI(); System.out.println(requestURI + " executed in " + elapsedTime + "ms"); } } ``` 最后,配置拦截器和MyBatis动态标签。 ```java @Configuration public class WebConfig implements WebMvcConfigurer { private final LoggingInterceptor loggingInterceptor; public WebConfig(LoggingInterceptor loggingInterceptor) { this.loggingInterceptor = loggingInterceptor; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(loggingInterceptor); } } @MapperScan("com.example.mapper") public class MyBatisConfig { } ``` 这样,你就完成了使用Spring Boot和MyBatis搭建web项目,并通过注解完成增删改查接口,并通过拦截器打印每个接口的用时。同时,使用MyBatis的动态标签可以完成判断查询和批量插入并获取自增主键id的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值