SpringBoot的常用注解

1.@SpringBootApplication

一般用于主启动类上,用于标识一个SpringBoot应用的入口点。它是一个复合注解,主要包含了@Configuration、@ComponentScan、@EnableAutoConfiguration 三个注解

2.@Configuration

用于标注一个类为配置类

3.@Component

用于标识一个类为Spring组件,表名该类被Spring容器所管理

4.@Service

用于标识一个类为服务层的组件(写业务逻辑处理)

5.@Controller

用于标识一个类是控制器

6.@RequestMapping

通过http请求去映射到其对应的处理方法(controller层方法),@RequestMapping 中的return返回值默认是被解析成跳转路径

7.@ResponseBody

自动将方法的返回值转换为特定格式(Json、xml)的HTTP响应体,返回给客户端。

因为@RequestMapping 默认是将返回值解析为url跳转路径,所以如果想要返回一个具体的值(比如String字符串)或者是一个对象时,就需要在方法前加上@ResponseBody.

这个注解会将java对象转为json格式的数据,会将返回结果直接写到http response body中,而不会再被解析为默认跳转路径。

8.@RestController

是@Controller和@ResponseBody的结合体,当同时使用这两个注解的时候,就可以使用@RestController代替

9.@PathVariable

将HTTP请求路径(url)中的变量绑定到控制器方法的参数上去

    @ApiOperation("删除部门")
    @DeleteMapping("/{id}")
    public RespBean deleteDepartment(@PathVariable("id") Integer id){
        Department department = new Department();
        department.setId(id);
        return departmentService.deleteDepartment(department);
    }
10.@RequestParam

用于获取请求参数的值

@ApiOperation(value = "获取所有员工信息(分页)")
@GetMapping("/")
public RespPageBean getAllPageEmployee(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer num,
            Employee employee,
            LocalDate[] beginDateScope){
    return employeeService.getAllPageEmployee(currentPage,num,employee,beginDateScope);

    }
11.@RequestBody

用于将http请求体中的数据绑定到方法的参数上去,比如前端返回一个对象,将其转换为dao层的实体,将其绑定到控制器的参数上去

    @ApiOperation("添加部门")
    @PostMapping("/")
    public RespBean addDepartment(@RequestBody Department department){
       return departmentService.addDepartment(department);
    }
12.@Autowired

用于自动装配spring中的bean 默认根据类型进行装配

    @Autowired
    private IEmployeeService employeeService;  //员工
    @Autowired
    private INationService nationService;  //民族
    @Autowired
    private IPositionService positionService;  //职位
    @Autowired
    private IJoblevelService joblevelService; //职称
    @Autowired
    private IDepartmentService departmentService;//部门
13.@Bean

将方法返回的对象注册到spring容器中

14.@Value

用于获取配置文件中的属性值

@Component
public class MyComponent {

    @Value("${my.property}")
    private String myProperty;

    // 其他方法
}
15.@Transactional 

启动事务管理功能,用于表示一个方法或者一个类需要使用事务进行操作

在Spring框架中,如果一个方法需要对数据库进行操作,我们可以使用@Transactional注解来确保这个操作在一个事务中进行,从而保证操作的原子性、一致性、隔离性和持久性。

@Service
@Transactional
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public void createUser(User user) {
        userRepository.save(user);
    }
    
    public void updateUser(Long id, User user) {
        User existingUser = userRepository.findById(id);
        
        if (existingUser != null) {
            existingUser.setName(user.getName());
            existingUser.setEmail(user.getEmail());
            userRepository.save(existingUser);
        }
    }
}

16.Lombok框架中注解

@Date、 @AllargsConstructor(生成全参构造)、@NoargsConstructor(自动生成无参构造)

@ToString 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值