Spring Boot 入门¶
一句话概述:Spring Boot 是 Java 的企业级 Web 框架,自动配置、内嵌服务器、开箱即用,是全球企业后端开发的标准选择,几乎所有 Fortune 500 公司都在使用。
核心知识点¶
| 概念 | 白话解释 |
|---|---|
| Starter | 起步依赖 = 一键引入某个功能的所有相关包 |
| Auto-configuration | 自动配置 = Spring Boot 根据依赖自动设置好一切 |
| Bean | Bean = Spring 管理的对象实例 |
| IoC/DI | 控制反转/依赖注入 = 对象由框架创建和管理 |
| Controller | 控制器 = 处理 HTTP 请求 |
| Service | 服务层 = 业务逻辑 |
| Repository | 仓储层 = 数据库操作 |
| application.yml | 配置文件 = 应用的所有配置 |
安装配置¶
# 使用 Spring Initializr 生成项目(最推荐)
# 访问 https://start.spring.io/ 选择依赖后下载
# 或用命令行(需安装 Spring Boot CLI)
brew install springboot # macOS
spring init --dependencies=web,data-jpa,postgresql myapp # 生成项目
<!-- pom.xml 核心依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.0</version> <!-- Spring Boot 3.3 -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <!-- Web 功能 -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <!-- JPA 数据库 -->
</dependency>
</dependencies>
基本使用¶
// 主入口类
@SpringBootApplication // 标记为 Spring Boot 应用
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args); // 启动应用
}
}
// 实体类
@Entity
@Table(name = "samples")
public class Sample {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String species;
// getter/setter...
}
// Repository(数据访问层)
public interface SampleRepository extends JpaRepository<Sample, Long> {
List<Sample> findBySpecies(String species); // 自动根据方法名生成 SQL
}
// Service(业务层)
@Service
public class SampleService {
@Autowired
private SampleRepository repository; // 自动注入
public List<Sample> findAll() { return repository.findAll(); }
public Sample create(Sample sample) { return repository.save(sample); }
}
// Controller(控制器)
@RestController
@RequestMapping("/api/samples")
public class SampleController {
@Autowired
private SampleService service;
@GetMapping
public List<Sample> list() { return service.findAll(); }
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Sample create(@RequestBody @Valid Sample sample) {
return service.create(sample);
}
@GetMapping("/{id}")
public Sample get(@PathVariable Long id) {
return service.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
}
# application.yml
server:
port: 8080
spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb
username: user
password: pass
jpa:
hibernate:
ddl-auto: update # 自动更新表结构
show-sql: true # 显示 SQL 语句
常见报错¶
| 报错信息 | 原因 | 解决方法 |
|---|---|---|
Failed to start bean | Bean 初始化失败 | 检查配置和依赖 |
Table not found | 数据库表不存在 | 设置 ddl-auto: update |
Port already in use | 端口被占 | 改 server.port 或杀进程 |
No qualifying bean | 找不到 Bean | 检查 @Component/@Service 注解 |
速查表¶
# === 常用注解 ===
@SpringBootApplication # 主入口
@RestController # REST 控制器
@RequestMapping("/api") # 路由前缀
@GetMapping @PostMapping @PutMapping @DeleteMapping
@PathVariable @RequestParam @RequestBody
@Service @Repository @Component @Autowired
@Valid # 参数验证
@Entity @Table @Id @Column # JPA 实体
# === 启动命令 ===
./mvnw spring-boot:run # Maven 启动
./gradlew bootRun # Gradle 启动
java -jar app.jar # JAR 包启动
# === 常用 Starter ===
spring-boot-starter-web # Web
spring-boot-starter-data-jpa # JPA 数据库
spring-boot-starter-security # 安全认证
spring-boot-starter-test # 测试
spring-boot-starter-actuator # 监控端点
参考:Spring Boot 文档 | 更新于 2026 年