SpringBoot3全栈开发实战:从入门到精通的完整指南

1. SpringBoot3概述🎯

1.1 SpringBoot3的特点与优势

SpringBoot3在SpringBoot2的基础上进行了重大改进,主要特点包括:

  • 自动配置:自动提供最优配置,可修改默认值
  • 起步依赖:将功能所需坐标打包,简化依赖管理
  • 嵌入式服务器:内置Tomcat,无需部署War文件
  • 非功能特性:提供安全指标、健康监测等企业级功能

1.2 SpringBoot3的系统要求

工具 版本要求
IDEA 2021.2.1+
Java 17+
Maven 3.5+
Tomcat 10.0+
Servlet 5.0+
GraalVM Community 22.3+
Native Build Tools 0.9.19+

2. SpringBoot3入门指南🛠️

2.1 项目搭建方式

2.1.1 官网搭建

访问 [start.spring.io] 生成项目,选择合适版本和依赖。

2.1.2 IDEA脚手架搭建

在IDEA中选择"Spring Initializr",配置项目信息和依赖。

2.1.3 Maven **手动搭建

xml 复制代码
<!-- 父工程 -->
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>3.1.2</version>
</parent>
<!-- 起步依赖 -->
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>
<!-- 打包插件 -->
<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

AI写代码xml
12345678910111213141516171819202122

2.2 项目结构解析

csharp 复制代码
src.main.java       # Java代码
  └── 启动类        # 项目入口
src.main.resources  # 配置和资源文件
  ├── static       # 静态资源(css, js, img)
  ├── templates    # 模板文件(Thymeleaf)
  └── application.yml # 配置文件
src.test.java      # 测试代码
pom.xml            # Maven配置

AI写代码markdown
12345678

3. 核心功能详解⚙️

3.1 YAML配置文件

3.1.1 基本语法

yaml 复制代码
# 简单数据
email: yibeigen@sxt.com
# 对象数据
my1:
  email: yibeigen@sxt.com
  password: yibeigen
# 集合数据
city1:
  - beijing
  - shanghai
  - tianjin

AI写代码yaml
1234567891011

3.1.2 配置读取方式

kotlin 复制代码
// @Value方式
@Value("${email}")
private String email;
// @ConfigurationProperties方式
@ConfigurationProperties(prefix = "user")
public class UserConfig {
  private int id;
  private String username;
  // getters and setters
}

AI写代码java
运行
12345678910

3.2 Web开发整合

3.2.1 Servlet注册

scala 复制代码
// 方式一:注解方式
@WebServlet("/first")
public class FirstServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response) {
    System.out.println("First Servlet");
  }
}
// 方式二:配置类方式
@Configuration
public class ServletConfig {
  @Bean
  public ServletRegistrationBean getServletRegistrationBean() {
    return new ServletRegistrationBean(new SecondServlet(), "/second");
  }
}

AI写代码java
运行
123456789101112131415

3.2.2 Thymeleaf模板引擎

xml 复制代码
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <title>Thymeleaf示例</title>
</head>
<body>
  <h2 th:text="${msg}">默认值</h2>
  
  <!-- 条件判断 -->
  <div th:if="${sex} == '男'">性别:男</div>
  <div th:if="${sex} == '女'">性别:女</div>
  
  <!-- 循环遍历 -->
  <table>
    <tr th:each="user : ${users}">
      <td th:text="${user.name}"></td>
      <td th:text="${user.age}"></td>
    </tr>
  </table>
</body>
</html>

AI写代码html
123456789101112131415161718192021

3.3 MyBatis整合

ini 复制代码
// Mapper接口
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
  @Select("select * from student where id = #{id}")
  Student findById(int id);
}
// 配置文件
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql:///student?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.itbaizhan.springbootmybatis.pojo

AI写代码java
运行
12345678910111213

4. 高级特性应用🚀

4.1 热部署

添加DevTools依赖:

xml 复制代码
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <optional>true</optional>
</dependency>

AI写代码xml
12345

4.2 定时任务

typescript 复制代码
@Component
public class MyTask {
  @Scheduled(cron="0 0 2 * * ?") // 每天凌晨2点执行
  public void task1() {
    System.out.println("定时任务执行: " + new Date());
  }
}
@SpringBootApplication
@EnableScheduling
public class DemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

AI写代码java
运行
1234567891011121314

4.3 内容协商

less 复制代码
@Controller
public class ConsultController {
  @Autowired
  private StudentMapper studentMapper;
  
  @RequestMapping("/student/findById")
  @ResponseBody
  public Student findById(Integer id) {
    return studentMapper.findById(id);
  }
}

AI写代码java
运行
1234567891011

5. 监控与部署📊

5.1 Actuator监控

添加依赖:

xml 复制代码
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

AI写代码xml
1234

配置:

yaml 复制代码
management:
  endpoints:
    web:
      exposure:
        include: '*'

AI写代码yaml
12345

访问端点:

  • /actuator/health - 健康检查
  • /actuator/metrics - 系统指标
  • /actuator/loggers - 日志管理

5.2 Spring Boot ** Admin

5.2.1 服务端配置

xml 复制代码
<dependency>
  <groupId>de.codecentric</groupId>
  <artifactId>spring-boot-admin-starter-server</artifactId>
  <version>3.1.3</version>
</dependency>

AI写代码xml
12345

5.2.2 客户端配置

xml 复制代码
<dependency>
  <groupId>de.codecentric</groupId>
  <artifactId>spring-boot-admin-starter-client</artifactId>
  <version>3.1.3</version>
</dependency>
spring.boot.admin.client.url=http://localhost:9090

AI写代码xml
123456

5.3 多环境部署

yaml 复制代码
# application-dev.yml
server:
  port: 8080
# application-test.yml
server:
  port: 8081
# application-prod.yml
server:
  port: 80

AI写代码yaml
123456789

运行时指定环境:

ini 复制代码
java -jar app.jar --spring.profiles.active=prod

AI写代码bash
1

6. 原理分析🔍

6.1 起步依赖原理

SpringBoot的起步依赖通过spring-boot-starter-parent实现版本管理和依赖传递。

6.2 自动配置原理

@SpringBootApplication注解等同于:

less 复制代码
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan

AI写代码java
运行
123

7. SpringBoot3新特性🌟

7.1 ProblemDetails

处理异常的新方式,符合RFC 7807规范:

ini 复制代码
spring.mvc.problemdetails.enabled=true

AI写代码java
运行
1

7.2 原生镜像

使用GraalVM生成原生可执行文件:

xml 复制代码
<profiles>
  <profile>
    <id>native</id>
    <properties>
      <repackage.classifier>exec</repackage.classifier>
      <native-buildtools.version>0.9.19</native-buildtools.version>
    </properties>
    <build>
      <plugins>
        <plugin>
          <groupId>org.graalvm.buildtools</groupId>
          <artifactId>native-maven-plugin</artifactId>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

AI写代码xml
1234567891011121314151617

8. 实用工具🛠️

8.1 Lombok

简化POJO代码:

less 复制代码
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
  private Integer id;
  private String username;
  private String password;
}

AI写代码java
运行
12345678

8.2 MyBatisPlus

增强MyBatis功能:

scala 复制代码
@TableName("tb_student")
public class Student extends Model<Student> {
  @TableId(value = "sid", type = IdType.AUTO)
  private Integer id;
  @TableField("sname")
  private String name;
  
  // CRUD操作
  student.insert();
  student.updateById();
  student.selectById();
}

AI写代码java
运行
相关推荐
Java水解2 小时前
Java 中间件:Dubbo 服务降级(Mock 机制)
java·后端
千寻girling2 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
南风9992 小时前
Claude code安装使用保姆级教程
后端
爱泡脚的鸡腿2 小时前
Node.js 拓展
前端·后端
蚂蚁背大象3 小时前
Rust 所有权系统是为了解决什么问题
后端·rust
子玖4 小时前
go实现通过ip解析城市
后端·go
Java不加班4 小时前
Java 后端定时任务实现方案与工程化指南
后端
心在飞扬5 小时前
RAG 进阶检索学习笔记
后端