首页 > 其他分享 >springboot 常用的注解,解决面试

springboot 常用的注解,解决面试

时间:2022-10-19 20:35:08浏览次数:41  
标签:users userId value PathVariable 面试 注解 public springboot

一: ComponentScan  : 作用扫描

二: MapperScan  : 扫描mapper 

三: @SpringBootApplication 组合注解

四: @EnableAutoConfiguration 开启自动配置的功能

五: @AutoConfigurationPackage

 

springboot 常用的注解,解决面试_数据

 这个注解是自动配置包,主要是使用的@Import来给Spring容器中导入一个组件 ,这里导入的是Registrar.class。

六: @ImportAutoConfiguration 导入配置类,一般做测试的时候使用,正常优先使用@EnableAutoConfiguration 

 七: @Autowired   

@Autowired
ApplicationService applicationService;

八: ​​@RestController​

​@RestController​​​注解是​​@Controller和​​​@​​ResponseBody​​的合集,表示这是个控制器 bean,并且是将函数的返回值直 接填入 HTTP 响应体中,是 REST 风格的控制器。

九:@​​Configuration    一般用来声明配置类​

@Configuration
public class AppConfig {
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}

}

 

选择器注释

1: @Conditional  当指定的条件都满足时,组件才被注册

 

缓冲:

@EnableCaching,开启缓存配置,支持子类代理或者AspectJ增强

 

事务注解:

 

 

处理常见的http 请求的注解

  1:  GET 请求

​@GetMapping("users")​​​ 等价于​​@RequestMapping(value="/users",method=RequestMethod.GET)​

@GetMapping("/users")
public ResponseEntity<List<User>> getAllUsers() {
return userRepository.findAll();
}

2:​​@PostMapping("users")​​​ 等价于​​@RequestMapping(value="/users",method=RequestMethod.POST)​

@PostMapping("/users")
public ResponseEntity<User> createUser(@Valid @RequestBody UserCreateRequest userCreateRequest) {
return userRespository.save(user);
}

3:​​@PutMapping("/users/{userId}")​​​ 等价于​​@RequestMapping(value="/users/{userId}",method=RequestMethod.PUT)​

@PutMapping("/users/{userId}")
public ResponseEntity<User> updateUser(@PathVariable(value = "userId") Long userId,
@Valid @RequestBody UserUpdateRequest userUpdateRequest) {
......
}

4: ​​@DeleteMapping("/users/{userId}")​​​等价于​​@RequestMapping(value="/users/{userId}",method=RequestMethod.DELETE)​

@DeleteMapping("/users/{userId}")
public ResponseEntity deleteUser(@PathVariable(value = "userId") Long userId){
......
}

前后端传值

1:​​@PathVariable​​​ 和 ​​@RequestParam​

​@PathVariable​​​用于获取路径参数,​​@RequestParam​​用于获取查询参数。

 

@GetMapping("/klasses/{klassId}/teachers")
public List<Teacher> getKlassRelatedTeachers(
@PathVariable("klassId") Long klassId,
@RequestParam(value = "type", required = false) String type ) {
...
}

如果我们请求的 url 是:​​/klasses/{123456}/teachers?type=web​

那么我们服务获取到的数据就是:​​klassId=123456,type=web​

 




标签:users,userId,value,PathVariable,面试,注解,public,springboot
From: https://blog.51cto.com/u_10999550/5776562

相关文章

  • Springboot项目的全局异常处理类
    在controller的同级目录exception下新建一个JavaClass文件,命名为GlobalExceptionHandler,内容如下packagecn.smxy.stest2022101601.exception;importorg.springframe......
  • springBoot 总结
        这是标准创建boot工程的方式 注意这里使用的是阿里云的url  https://start.aliyun.com/修改服务器端口  自动提示功能消失解决方案    ......
  • springboot + mybatisplus出现was not registered for synchronization because synch
    原因一:缺少事务注解,底层mybatisplus的接口方法有事务原因二:该服务器被限制访问要连接的数据库原因三:乐观锁失效乐观锁由@version注解标注,有以下使用要求支持的......
  • React HOOK:useReducer 与 useState区别?(面试)
    useReducervsuseState(面试)useReducer和useState都可以用来管理组件的状态,它们之间最大的区别就是:useReducer将状态和状态的变化统一管理在reducer函数里面,这样对......
  • SpringBoot 启动参数及vm参数获取
    @AutowiredprivateApplicationArgumentsapplicationArguments;@AutowiredprivateApplicationContextapplicationContext;main方法通过Appl......
  • 2021年非常全的.NET Core面试题
    1.如何在ASP.NETCore中激活Session功能?          首先要添加session包.其次要在configservice方法里面添加session。然后又在configure方法里面调用usese......
  • 注解系列(持续)
    1、@EqualsAndHashCode(callSuper=false)如果没有继承,仅对一个类加上@Data,不会有告警提示设置@EqualsAndHashCode(callSuper=false);但存在继承,会有告警@Datapublic......
  • JPA入门学习集合springboot(一)
    1、在pom.xml文件中添加相应依赖SpringDatajpa和数据库MySql<!--SpringDataJPA依赖(重要)--><dependency><groupId>org.springframework.boo......
  • React面试八股文(第二期)
    React.forwardRef是什么?它有什么作用?React.forwardRef会创建一个React组件,这个组件能够将其接受的ref属性转发到其组件树下的另一个组件中。这种技术并不常见,但在以下......
  • SpringBoot自定义Banner信息
    SpringBoot自定义Banner信息一、介绍本文主要介绍使用springboot框架时,我们可以自定义我们项目的相关信息,例如启动图、启动时的版本号等。二、自定义banner我们在启......