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

简介: 本文是SpringBoot3的精要总结,涵盖自动配置、起步依赖、嵌入式服务器等核心特性,详解项目搭建、YAML配置、Web开发、MyBatis整合、热部署、定时任务、Actuator监控及原生镜像等高级功能,并附思维导图助快速复习。

📝 引言
SpringBoot3作为Spring框架的最新版本,带来了许多令人兴奋的新特性和改进
本文是基于之前的内容来写的
这篇文章是SpringBoot3的总结,适合想快速复习的读者
最后的思维导图总结,能够让你眼前一亮
个人主页:艺杯羹
系列专栏: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手动搭建

<!-- 父工程 -->
<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>

2.2 项目结构解析

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

3. 核心功能详解⚙️

3.1 YAML配置文件

3.1.1 基本语法

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

3.1.2 配置读取方式

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

3.2 Web开发整合

3.2.1 Servlet注册

// 方式一:注解方式
@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");
  }
}

3.2.2 Thymeleaf模板引擎

<!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>

3.3 MyBatis整合

// 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

4. 高级特性应用🚀

4.1 热部署

添加DevTools依赖:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <optional>true</optional>
</dependency>

4.2 定时任务

@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);
  }
}

4.3 内容协商

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

5. 监控与部署📊

5.1 Actuator监控

添加依赖:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

配置:

management:
  endpoints:
    web:
      exposure:
        include: '*'

访问端点:

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

5.2 Spring Boot Admin

5.2.1 服务端配置

<dependency>
  <groupId>de.codecentric</groupId>
  <artifactId>spring-boot-admin-starter-server</artifactId>
  <version>3.1.3</version>
</dependency>

5.2.2 客户端配置

<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

5.3 多环境部署

# application-dev.yml
server:
  port: 8080
# application-test.yml
server:
  port: 8081
# application-prod.yml
server:
  port: 80

运行时指定环境:

java -jar app.jar --spring.profiles.active=prod

6. 原理分析🔍

6.1 起步依赖原理

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

6.2 自动配置原理

@SpringBootApplication注解等同于:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan

7. SpringBoot3新特性🌟

7.1 ProblemDetails

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

spring.mvc.problemdetails.enabled=true

7.2 原生镜像

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

<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>

8. 实用工具🛠️

8.1 Lombok

简化POJO代码:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
  private Integer id;
  private String username;
  private String password;
}

8.2 MyBatisPlus

增强MyBatis功能:

@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();
}

9. 思维导图总结💎


tips:如果需要Markdown格式的文件,可以来联系我哦!
制作不易,希望大家支持!😊

目录
相关文章
|
Java 机器人 Maven
【Java用法】微服务之间的相互调用方式之一,通过FeignClient客户端调用其他微服务的方法包含熔断器(Hystrix)
【Java用法】微服务之间的相互调用方式之一,通过FeignClient客户端调用其他微服务的方法包含熔断器(Hystrix)
727 0
|
2月前
|
前端开发 Java 数据库连接
Spring Boot 详细简介!
Spring Boot 是什么?能干啥?
299 0
Spring Boot 详细简介!
|
4月前
|
SQL Java 关系型数据库
【Spring全家桶】Spring Cloud 2023.0.x:分布式事务:Seata 四大模式(AT/TCC/SAGA/XA)、适用场景(附《思维导图》+《面试高频考点清单》)
本文系统梳理Spring Cloud 2023.0.x(Leyton)与Seata分布式事务的深度集成,涵盖AT/TCC/SAGA/XA四大模式原理、多维对比、场景选型及高可用实践,助力微服务数据一致性落地。
【Spring全家桶】Spring Cloud 2023.0.x:分布式事务:Seata 四大模式(AT/TCC/SAGA/XA)、适用场景(附《思维导图》+《面试高频考点清单》)
|
2月前
|
缓存 前端开发 JavaScript
Thymeleaf模板引擎:让Spring Boot页面开发更简单高效
Thymeleaf是现代化Java服务端模板引擎,支持HTML等多格式;天然兼容静态预览与动态渲染,实现前后端高效协作;深度集成Spring Boot,以`th:*`属性优雅绑定数据,语法简洁、学习成本低。
153 1
Thymeleaf模板引擎:让Spring Boot页面开发更简单高效
|
29天前
|
设计模式 算法 安全
Java 设计模式全景拆解:从单例工厂到策略责任链的三大类型实战指南
本文穿透设计模式本质,以单例、工厂、装饰器、代理、策略、责任链六大高频模式为线索,结合双检锁单例多线程崩溃的真实排错案例,剖析根因(指令重排序)与修复闭环。强调模式应由真实痛点驱动,而非炫技背诵。
134 2
|
2月前
|
存储 人工智能 安全
2026年阿里云服务器特惠汇总:从38元轻量级到九代高性能实例,覆盖个人应用到企业级算力需求
本文系统梳理了阿里云2026年活动中的多款高性价比云服务器产品线,覆盖从入门到企业级的全场景需求。文章逐一剖析了轻量应用服务器(38元/年起)、经济型e实例(99元/年起)、通用算力型u1/u2i实例以及第九代高性能c9i/g9i/r9i旗舰实例的核心配置、活动价格、技术优势与适用场景,并结合个人博客、中小企业官网、大数据平台、大型电商等实际案例,为个人开发者、初创团队及企业用户提供了从选型、购买到部署管理的全链路参考指南,旨在帮助不同预算与业务需求的用户做出精准决策。
2026年阿里云服务器特惠汇总:从38元轻量级到九代高性能实例,覆盖个人应用到企业级算力需求
|
2月前
|
Linux 开发工具 git
Git入门基础:从概念到安装的完整指南
Git是由Linus Torvalds于2005年创建的开源分布式版本控制系统,以快、稳、强著称,支持离线操作、高效分支管理与完整历史追溯,现为全球软件开发事实标准。
197 0
Git入门基础:从概念到安装的完整指南
|
10月前
|
JSON 前端开发 Java
Spring Boot 封装统一返回的数据结构
本课讲解如何设计统一的JSON响应结构,通过泛型类`JsonResult&lt;T&gt;`封装返回数据,规范包含code、msg、data的标准格式,提升前后端协作效率与API可维护性。

热门文章

最新文章