Spring Boot 入门指南:从零开始构建 Java Web 应用 📅 2026/8/1 8:33:29 1. Spring Boot 简介Spring Boot 是 Spring 框架的一个子项目旨在简化 Spring 应用的初始搭建和开发过程。它通过自动配置、起步依赖和嵌入式服务器等特性让开发者能够快速创建独立运行、生产级别的 Spring 应用。2. Spring Boot 的核心特性自动配置根据项目依赖自动配置 Spring 应用减少大量 XML 配置。起步依赖提供一系列预配置的依赖包简化 Maven/Gradle 配置。嵌入式服务器内置 Tomcat、Jetty 或 Undertow无需部署 WAR 包。生产就绪提供健康检查、指标、外部化配置等生产级功能。无代码生成无需 XML 配置通过注解和约定实现配置。3. 快速开始创建第一个 Spring Boot 应用3.1 环境准备JDK 8 或更高版本Maven 3.3 或 Gradle 4.4IDE推荐 IntelliJ IDEA 或 Eclipse3.2 使用 Spring Initializr 创建项目访问 https://start.spring.io选择以下配置项目类型Maven语言JavaSpring Boot 版本选择最新稳定版依赖Spring Web3.3 项目结构src/main/java ├── com/example/demo │ ├── DemoApplication.java # 主启动类 │ └── controller │ └── HelloController.java # 控制器示例 src/main/resources ├── application.properties # 配置文件 └── static/ # 静态资源 └── templates/ # 模板文件3.4 编写第一个 REST 接口package com.example.demo.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; RestController public class HelloController { GetMapping(/hello) public String hello() { return Hello, Spring Boot!; } }3.5 运行应用# 方式一使用 Maven mvn spring-boot:run 方式二直接运行主类 在 IDE 中运行 DemoApplication.java访问 http://localhost:8080/hello 即可看到返回结果。4. 常用配置与注解4.1 配置文件Spring Boot 支持多种配置文件格式application.properties键值对格式application.ymlYAML 格式层次更清晰# application.yml 示例 server: port: 8081 servlet: context-path: /api spring: datasource: url: jdbc:mysql://localhost:3306/testdb username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver4.2 核心注解SpringBootApplication主启动类注解包含 Configuration、EnableAutoConfiguration、ComponentScanRestController声明 RESTful 控制器RequestMapping/GetMapping/PostMapping请求映射Autowired自动注入依赖Configuration声明配置类Bean声明 Bean 对象5. 数据库集成5.1 集成 MySQL在pom.xml中添加依赖dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId scoperuntime/scope /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-data-jpa/artifactId /dependency5.2 创建实体类package com.example.demo.entity; import javax.persistence.*; Entity Table(name user) public class User { Id GeneratedValue(strategy GenerationType.IDENTITY) private Long id; Column(nullable false) private String username; Column(nullable false) private String email; // 省略 getter/setter 和构造方法 }5.3 创建 Repositorypackage com.example.demo.repository; import com.example.demo.entity.User; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepositoryUser, Long { User findByUsername(String username); }6. 项目打包与部署6.1 打包为可执行 JARmvn clean package生成的 JAR 文件位于target/目录可直接运行java -jar target/demo-0.0.1-SNAPSHOT.jar6.2 打包为 WAR传统部署修改主启动类SpringBootApplication public class DemoApplication extends SpringBootServletInitializer { Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DemoApplication.class); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }修改pom.xml打包方式packagingwar/packaging