java 常用注解记录

  1. @RestController 和 @Controller 的区别?
    首先 @RestController 是由多个注解组成的,如下图:
    java 常用注解记录
    可以看出来@RestController包含了@Controller,所以@RestController可以说是@Controller的升级版,功能比@Controller更丰富。
    比如说@RestController里面还包含了@ResponseBody注解,如果使用的是@Controller注解的话,后台给前台返回数据,就 需要在方法上添加@ResponseBody注解才行,但是如果接口使用的是@RestController注解的话,就不需要再方法上使用@ResponseBody注解了。
  2. @MapperScan 和 @Mapper 的区别?
    ① @Mapper 用在 dao 层,@MapperScan 用在springboot项目的启动类上
    ② 使用 @Mapper 的话,那 dao 层的每个接口上都要使用,使用 @MapperScan 的话,dao 层就不需要再使用 @Mapper 注解了,使用 @MapperScan 后会自动扫描配置的包下面所有的接口。
    ③ @Mapper 作用:添加了 @Mapper 注解之后这个接口在编译时会生成相应的实现类
    java 常用注解记录
    java 常用注解记录