本文最后更新于31 天前,其中的信息可能已经过时,如有错误请发送邮件到2327470875@qq.com
🏗 Spring Boot 注解完整手册(小白版)
结构:
① 启动类 & 配置相关
② Bean 管理 & 依赖注入
③ Web 层(Controller 请求相关)
④ Service & DAO 层
⑤ 数据库 & 事务
⑥ 配置文件 & 常用工具
⑦ Lombok 辅助注解
① 启动类 & 配置相关
1. @SpringBootApplication
- 比喻:大门口的“总开关”,告诉 Spring Boot 从这里启动。
- 作用:标记启动类,启动时会:
- 把当前类作为配置类(
@Configuration
)。 - 开启自动配置(
@EnableAutoConfiguration
)。 - 自动扫描同包下的 Bean(
@ComponentScan
)。
- 把当前类作为配置类(
- 代码:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2. @Configuration
- 比喻:人事部文件夹,里面写着“公司要招聘什么员工”。
- 作用:定义配置类,可以写
@Bean
方法。 - 代码:
@Configuration
public class AppConfig {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
3. @Bean
- 比喻:招聘启事 → 招一个对象交给 Spring 管理。
- 作用:方法返回值会交给 Spring 容器,成为 Bean。
- 代码:
@Bean
public Tool tool() {
return new Tool("螺丝刀");
}
4. @ComponentScan
- 比喻:扫地机器人 → 自动扫描某个包下的员工。
- 作用:告诉 Spring 去哪里找带注解的类。
- 代码:
@SpringBootApplication(scanBasePackages = "com.example.demo")
② Bean 管理 & 依赖注入
5. @Component
- 比喻:给一个类贴工牌,Spring 启动时会招进去。
- 作用:最基本的 Bean 注解。
- 代码:
@Component
public class Tool {
public String getName() {
return "扳手";
}
}
6. @Service
- 比喻:经理,负责业务逻辑。
- 作用:业务层类。其实就是
@Component
的变种。 - 代码:
@Service
public class UserService {
public String getName() { return "小明"; }
}
7. @Repository
- 比喻:仓库管理员,负责数据库操作。
- 作用:DAO 层类。
- 代码:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {}
8. @Controller
& @RestController
- 比喻:前台接待员,负责接待客户。
- 作用:
@Controller
→ 返回页面(HTML)。@RestController
→ 返回 JSON(常用于接口开发)。
- 代码:
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/{id}")
public String getUser(@PathVariable int id) {
return "用户ID:" + id;
}
}
9. @Autowired
- 比喻:公司里自动分配 → 不用自己
new
。 - 作用:自动注入 Bean。
- 代码:
@Service
public class WorkService {
@Autowired
private Tool tool;
public void doWork() {
System.out.println("使用工具:" + tool.getName());
}
}
10. @Qualifier
- 比喻:如果有两个同岗位的人 → 指定要哪一个。
- 代码:
@Autowired
@Qualifier("tool2")
private Tool tool;
11. @Primary
- 比喻:默认优先选中某个员工。
- 代码:
@Component
@Primary
public class Tool1 implements Tool {}
③ Web 层(请求映射相关)
12. @RequestMapping
- 比喻:前台小姐姐 → 定义接口入口。
- 代码:
@RequestMapping("/hello")
public String hello() { return "hi"; }
13. @GetMapping
/ @PostMapping
/ @PutMapping
/ @DeleteMapping
- 比喻:不同的门 → GET/POST/PUT/DELETE 请求方式。
- 代码:
@GetMapping("/{id}")
public String getUser(@PathVariable Long id) { ... }
@PostMapping("/add")
public String addUser(@RequestBody User user) { ... }
14. @PathVariable
- 比喻:从 URL 路径里取参数。
- 代码:
/user/1
→ id=1
@GetMapping("/user/{id}")
public String getUser(@PathVariable int id) { ... }
15. @RequestParam
- 比喻:从 URL 参数里取数据。
- 代码:
/user/search?name=小明
@GetMapping("/search")
public String search(@RequestParam String name) { ... }
16. @RequestBody
- 比喻:从请求体(JSON)里取数据。
- 代码:
@PostMapping("/add")
public String addUser(@RequestBody User user) { ... }
④ Service & DAO 层
17. @Transactional
- 比喻:银行转账 → 要么两边都成功,要么都失败。
- 代码:
@Transactional
public void transfer(Long fromId, Long toId, double money) {
accountMapper.decrease(fromId, money);
accountMapper.increase(toId, money);
}
18. @Mapper
- 比喻:告诉 MyBatis:这是 SQL 接口。
- 代码:
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user WHERE id = #{id}")
User selectById(Long id);
}
⑤ 配置文件相关
19. @Value
- 比喻:点菜单 → 从配置文件拿数据。
- 代码:
application.yml:app: name: demo
使用:
@Value("${app.name}")
private String appName;
20. @ConfigurationProperties
- 比喻:一整个配置套餐 → 自动装配成对象。
- 代码:
application.yml:app: name: demo version: 1.0
@Component
@ConfigurationProperties(prefix = "app")
@Data
public class AppConfig {
private String name;
private String version;
}
⑥ 日志 & Lombok 辅助
21. @Slf4j
- 比喻:自动给你一个小本子,可以随时写日志。
- 代码:
@Slf4j
@RestController
public class TestController {
@GetMapping(“/log”)
public String logTest() {
log.info(“日志测试成功!”);
return “ok”;
}
}
22. @Data
- 比喻:秘书帮你写 getter/setter。
- 代码:
@Data
@AllArgsConstructor
public class User {
private Long id;
private String name;
}
🎯 总结(推荐学习顺序)
- 启动类注解:
@SpringBootApplication / @Configuration / @Bean
- Bean 管理:
@Component / @Service / @Repository / @Autowired / @Qualifier
- Web 层:
@RestController / @RequestMapping / @GetMapping / @PostMapping / @PathVariable / @RequestParam / @RequestBody
- 数据库 & 事务:
@Repository / @Mapper / @Transactional
- 配置文件:
@Value / @ConfigurationProperties
- 工具类:Lombok 注解(
@Data / @Slf4j
)