springboot --- (java) @GetMapping、@PostMapping和@RequestMapping的区别

@GetMapping、@PostMapping和@RequestMapping的区别

摘要

Spring4.3中引进了**{@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@PatchMapping}**,来帮助简化常用的HTTP方法的映射,并更好地表达被注解方法的语义。

三者区别

@GetMapping

用于将HTTP GET请求映射到特定处理程序方法的注释。具体来说,@GetMapping是一个作为快捷方式的组合注释
@RequestMapping(method = RequestMethod.GET)。

@PostMapping

用于将HTTP POST请求映射到特定处理程序方法的注释。具体来说,@PostMapping是一个作为快捷方式的组合注释@RequestMapping(method = RequestMethod.POST)。

@RequestMapping:

一般情况下都是用@RequestMapping(method=RequestMethod.),因为@RequestMapping可以直接替代以上两个注解,但是以上两个注解并不能替代@RequestMapping,@RequestMapping相当于以上两个注解的父类!

类似的组合注解还有:
@PutMapping、@DeleteMapping、@PatchMapping

总结下来就是@PostMapping和@GetMapping都可以用@RequestMapping代替,如果读者怕在映射的时候出错,可以统一写@RequestMapping,当然这样写的话也有弊端,笼统的全用@RequestMapping, 不便于其他人对代码的阅读和理解!还是建议区分开来写!养成良好的代码习惯!

可以使用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、付费专栏及课程。

余额充值