Skip to content

三层架构注解使用

约 292 字小于 1 分钟

SpringBoot

2025-02-24

1. Controller层

  • 使用@RestController ,结合了 @Controller 和 @ResponseBody

  • 使用@Slf4j,便于响应日志

  • 外部使用@RequestMapping,可以将公共的请求路径合到一起

  • 内部

    • 使用@Autowired,从Spring容器注入Service层的类或其他类

    • Mapping

      • Post请求时使用@PostMapping

      • Get请求时使用@GetMapping

      • Delete请求时使用@DeleteMapping

      • Put请求时使用@PutMapping

    • 参数

      • 路径参数使用@PathVariable

      • 请求参数为封装为对象的json或xml时使用@RequestBody

      • @RequestParam,可以绑定多个参数、设置参数默认值等

      • 日期使用@DateTimeFormat

2. Service层(实现类)

  • 使用@Service,将类传到Spring容器

  • 使用@Autowired从Spring容器注入Mapper层的类或其他类

  • 使用@Override(实现类需要)

3. Mapper层(不使用xml映射时)

  • 使用@Mapper,将类传到Spring容器

  • 增删查改分别使用:@Insert@Delete@Select@Update

4. 其它

  • 使用@Component,将类传到Spring容器
  • @Value注解只能一个一个的进行外部属性的注入。
  • @ConfigurationProperties可以批量的将外部的属性配置注入到bean对象的属性中。
贡献者: xsq, xsq