千禧愿 發表於 2026-1-13 10:06:28

Spring Boot 项目开发全流程实战示例总结

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>Spring Boot 项目开发全流程实战指南:从 0 到 1 实现 CURD(含面试题)</li><ul class="second_class_ul"><li>📖 一、 引言:Spring Boot 开发在做什么?</li><li>🏗️ 二、 项目结构与开发顺序</li><li>💻 三、 代码实战:实现用户增删改查</li><ul class="third_class_ul"><li>3.1 第一步:Entity (食材准备)</li><li>3.2 第二步:Mapper/Repository (仓库采购)</li><li>3.3 第三步:Service (大厨烹饪)</li><li>3.4 第四步:Controller (服务员接客)</li></ul><li>🧐 四、 核心概念名词解释(小白必看)</li><ul class="third_class_ul"><li>1. IoC (控制反转) 与 DI (依赖注入)</li><li>2. Spring Bean</li><li>3. JSON</li></ul><li>🙋&zwj;♂️ 五、 高频面试题 QA</li><ul class="third_class_ul"></ul><li>🎯 六、 总结</li><ul class="third_class_ul"></ul></ul></ul></div><p class="maodian"></p><h2>Spring Boot 项目开发全流程实战指南:从 0 到 1 实现 CURD(含面试题)</h2>
<blockquote><p>📅 <strong>发布时间</strong>:2026-01-11<br />🏷️ <strong>标签</strong>:Java, Spring Boot, 教程, 后端开发, RESTful<br />💡 <strong>摘要</strong>:本文专为 Java 初学者量身打造,手把手带你理解 Spring Boot 项目的标准开发流程。从 Entity 实体类定义,到 Controller 接口开放,全链路打通。包含超详细的代码注释、核心概念图解以及高频面试题。</p></blockquote>
<p class="maodian"></p><h3>📖 一、 引言:Spring Boot 开发在做什么?</h3>
<p>很多新手在刚接触 Spring Boot 时,会被各种层级搞晕:<code>Controller</code>, <code>Service</code>, <code>Mapper</code>, <code>Entity</code>&hellip; 到底先写谁?谁调谁?</p>
<p>其实,<strong>Web 开发的核心流程</strong>就像餐厅的点餐流程:</p>
<ol><li><strong>Controller (服务员)</strong>:直接面对客人(前端),拿着菜单(接口文档),记录客人的需求(接收请求参数),把菜端给客人(返回响应数据)。</li><li><strong>Service (大厨)</strong>:负责核心烹饪(业务逻辑)。比如把肉切好、炒熟。如果发现菜没了(异常),就告诉服务员。</li><li><strong>Mapper/Repository (采购员)</strong>:只负责去仓库(数据库)拿原材料(增删改查数据),不负责烹饪。</li><li><strong>Entity (食材)</strong>:就是在各个环节传递的数据对象(比如红烧肉)。</li></ol>
<p>今天我们就来演示如何制作一道 <strong>&ldquo;用户管理&rdquo;</strong> 的菜。</p>
<p class="maodian"></p><h3>🏗️ 二、 项目结构与开发顺序</h3>
<p>一个标准的 Spring Boot 项目,通常包含以下层级(建议按此顺序编写):</p>
<ol><li><strong>📄 Entity (实体层)</strong>:定义数据库表结构对应的 Java 类。</li><li><strong>💾 Mapper/DAO (持久层)</strong>:操作数据库的接口(MyBatis 或 JPA)。</li><li><strong>🧠 Service (业务层)</strong>:编写复杂的业务逻辑。</li><li><strong>🌐 Controller (控制层)</strong>:对外暴露 URL 接口。</li></ol>
<p class="maodian"></p><h3>💻 三、 代码实战:实现用户增删改查</h3>
<p>我们将实现一个简单的用户管理功能。</p>
<p class="maodian"></p><h4>3.1 第一步:Entity (食材准备)</h4>
<p>实体类是数据的载体,直接对应数据库中的表。我们分两步来写:</p>
<p><strong>1. 类定义与主键配置</strong><br />首先定义类的主体结构,并配置好主键生成策略。</p>
<div class="jb51code"><pre class="brush:java;">package com.example.demo.entity;
import lombok.Data; // Lombok 插件,自动生成 getter/setter/toString
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
/**
* 用户实体类
* 对应数据库表:t_user
*/
@Data // Lombok 注解:自动生成 Getter, Setter, ToString, HashCode 等方法,省去手动编写
@Entity // JPA 注解:声明这是一个实体类,与数据库表映射
@Table(name = "t_user") // 指定对应的数据库表名为 t_user
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
   * 主键 ID
   * @Id: 标识该属性为主键
   * @GeneratedValue: 指定主键生成策略,IDENTITY 表示使用数据库自增 (Auto Increment)
   */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    // ... 下面继续添加其他字段</pre></div>
<p><strong>2. 业务字段定义</strong><br />接下来,在 <code>User</code> 类中补充用户名、密码等业务字段。</p>
<div class="jb51code"><pre class="brush:java;">    /**
   * 用户名
   * @Column: 映射数据库字段属性
   * nullable = false: 数据库这一列不能为空
   * unique = true: 用户名必须唯一
   */
    @Column(nullable = false, unique = true, length = 50)
    private String username;
    /**
   * 密码 (实际开发中不能存明文,要存加密后的哈希值)
   */
    @Column(nullable = false, length = 100)
    private String password;
    /**
   * 邮箱
   */
    @Column(length = 100)
    private String email;
    /**
   * 创建时间
   * 用于记录这一条数据是什么时候插入生成的
   */
    @Column(name = "create_time")
    private Date createTime;
}</pre></div>
<p class="maodian"></p><h4>3.2 第二步:Mapper/Repository (仓库采购)</h4>
<p>这里演示使用 Spring Data JPA,它非常强大,只需继承 <code>JpaRepository</code> 接口,基本的增删改查代码都不用自己写!</p>
<div class="jb51code"><pre class="brush:java;">package com.example.demo.repository;
import com.example.demo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* 用户持久层接口
* 继承 JpaRepository&lt;实体类类型, 主键类型&gt;
*
* 作用:这就是那个"采购员",Spring Boot 自动帮你实现好了去数据库拿数据的方法:
* - save(User user): 保存或更新
* - findById(Long id): 根据ID查询
* - findAll(): 查询所有
* - deleteById(Long id): 根据ID删除
*/
@Repository // 标记这是持久层组件,Spring 会把它放入容器管理
public interface UserRepository extends JpaRepository&lt;User, Long&gt; {
    /**
   * 自定义查询方法
   * JPA 的神奇之处:只要按照规则命名方法,SQL 语句自动生成!
   *
   * 翻译:select * from t_user where username = ?
   */
    User findByUsername(String username);
    /**
   * 翻译:select * from t_user where email = ?
   */
    User findByEmail(String email);
}</pre></div>
<p class="maodian"></p><h4>3.3 第三步:Service (大厨烹饪)</h4>
<p>业务逻辑都在这里。我们将代码拆分为两个部分:基础注入和核心业务逻辑。</p>
<p><strong>1. 依赖注入与类结构</strong><br />首先,我们需要把 <code>UserRepository</code> (采购员) 注入进来,方便后续调用。</p>
<div class="jb51code"><pre class="brush:java;">package com.example.demo.service;
import com.example.demo.entity.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
@Service // 标记这是业务层组件
public class UserService {
    @Autowired // 依赖注入:告诉 Spring,我要用这个采购员(Repository),请帮我送进来
    private UserRepository userRepository;</pre></div>
<p><strong>2. 核心业务:注册逻辑</strong><br />这是最复杂的逻辑,包含参数校验、重名检查和数据补全。</p>
<div class="jb51code"><pre class="brush:java;">    /**
   * 新增用户 (注册)
   * 业务逻辑:
   * 1. 检查用户名是否存在
   * 2. 补全创建时间
   * 3. 保存到数据库
   */
    @Transactional // 开启事务:保证一系列操作要么全成功,要么全失败
    public User register(User userData) {
      // 1. 简单校验
      if (userData.getUsername() == null) {
            throw new RuntimeException("用户名不能为空");
      }
      // 2. 检查是不是重名了
      User existUser = userRepository.findByUsername(userData.getUsername());
      if (existUser != null) {
            throw new RuntimeException("用户名已存在,换一个吧");
      }
      // 3. 补全系统字段 (创建时间)
      userData.setCreateTime(new Date());
      // 4. 调用持久层保存
      return userRepository.save(userData);
    }</pre></div>
<p><strong>3. 其他业务逻辑</strong><br />查询和删除相对简单,直接调用 Repository 的方法即可。</p>
<div class="jb51code"><pre class="brush:java;">    /**
   * 查询所有用户列表
   */
    public List&lt;User&gt; findAllUsers() {
      return userRepository.findAll();
    }
    /**
   * 根据 ID 删除用户
   */
    @Transactional
    public void deleteUser(Long id) {
      if (!userRepository.existsById(id)) {
            throw new RuntimeException("要删除的用户不存在!");
      }
      userRepository.deleteById(id);
    }
}</pre></div>
<p class="maodian"></p><h4>3.4 第四步:Controller (服务员接客)</h4>
<p>这是对外的窗口,通常遵循 RESTful 风格设计。为了清晰,我们按功能模块拆分代码。</p>
<p><strong>1. 控制器初始化</strong><br />定义 API 的基础路径 <code>/api/users</code>,并注入 Service。</p>
<div class="jb51code"><pre class="brush:java;">package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 用户管理控制器
* @RestController = @Controller + @ResponseBody
*/
@RestController // 声明这是一个 REST 风格的控制器
@RequestMapping("/api/users") // 定义统一的基础路径
public class UserController {
    @Autowired
    private UserService userService; // 注入大厨 (Service)</pre></div>
<p><strong>2. 查询接口 (GET)</strong><br />对应 HTTP GET 请求,用于获取资源。</p>
<div class="jb51code"><pre class="brush:java;">    /**
   * 获取用户列表
   * URL: GET /api/users
   */
    @GetMapping
    public List&lt;User&gt; list() {
      return userService.findAllUsers();
    }
</pre></div>
<p><strong>3. 新增接口 (POST)</strong><br />对应 HTTP POST 请求,用于创建资源。注意 <code>@RequestBody</code> 的使用,它负责接收 JSON 数据。</p>
<div class="jb51code"><pre class="brush:java;">    /**
   * 注册新用户
   * URL: POST /api/users
   * @RequestBody: 读取请求体中的 JSON 数据转为 User 对象
   */
    @PostMapping
    public String add(@RequestBody User user) {
      try {
            userService.register(user);
            return "注册成功!";
      } catch (Exception e) {
            return "注册失败: " + e.getMessage();
      }
    }</pre></div>
<p><strong>4. 删除接口 (DELETE)</strong><br />对应 HTTP DELETE 请求。注意 <code>@PathVariable</code> 用于从 URL 中提取 ID。</p>
<div class="jb51code"><pre class="brush:java;">    /**
   * 删除用户
   * URL: DELETE /api/users/{id} (例如 /api/users/5)
   * @PathVariable: 从 URL 路径中提取 {id} 的值
   */
    @DeleteMapping("/{id}")
    public String delete(@PathVariable Long id) {
      try {
            userService.deleteUser(id);
            return "删除成功";
      } catch (Exception e) {
            return "删除失败: " + e.getMessage();
      }
    }
}</pre></div>
<p class="maodian"></p><h3>🧐 四、 核心概念名词解释(小白必看)</h3>
<p class="maodian"></p><h4>1. IoC (控制反转) 与 DI (依赖注入)</h4>
<ul><li><strong>IoC</strong>:以前我们需要对象,必须自己 <code>new UserService()</code>。现在我们把创建对象的权利交给 Spring 容器(管家)。</li><li><strong>DI</strong>:我们需要用对象时,在属性上加个 <code>@Autowired</code>,Spring 就会帮我们把创建好的对象<strong>注入</strong>进来。</li></ul>
<p class="maodian"></p><h4>2. Spring Bean</h4>
<ul><li>被 Spring 容器管理的对象就叫 Bean。</li><li>只要加上 <code>@Controller</code>, <code>@Service</code>, <code>@Repository</code>, <code>@Component</code> 这些注解,类就会变成 Bean。</li></ul>
<p class="maodian"></p><h4>3. JSON</h4>
<ul><li>前后端交互的&quot;普通话&quot;。Controller 返回的 Java 对象会被自动转换成 JSON 字符串格式(比如 <code>{&quot;name&quot;: &quot;张三&quot;, &quot;age&quot;: 18}</code>),前端 JS 能轻松看懂。</li></ul>
<p class="maodian"></p><h3>🙋&zwj;♂️ 五、 高频面试题 QA</h3>
<p>Q1:<code>@RestController</code> 和 <code>@Controller</code> 有什么区别?</p>
<ul><li><strong>A</strong>:</li><li><code>@Controller</code>:通常用于传统的 Web 开发,方法默认返回的是<strong>视图名字</strong>(比如 <code>index.html</code> 的文件名),用于跳转页面。如果要返回 JSON 数据,需要在方法上额外加 <code>@ResponseBody</code>。</li><li><code>@RestController</code>:是 <code>@Controller</code> 和 <code>@ResponseBody</code> 的组合注解。用它标记的类,所有方法默认都返回 <strong>数据 (JSON/XML)</strong>,主要用于前后端分离开发。</li></ul>
<p><strong>Q2:Post 请求和 Get 请求的区别?(RESTful 视角)</strong></p>
<ul><li><strong>A</strong>:</li><li><strong>GET</strong>:用于<strong>获取</strong>资源。它是幂等的(查一次和查很多次结果一样),参数拼接在 URL 后面,不安全且长度有限。</li><li><strong>POST</strong>:用于<strong>新建</strong>资源(比如提交表单)。参数放在 Request Body 中,相对安全且无大小限制。</li></ul>
<p><strong>Q3:Service 层和 Controller 层的区别?代码写在一个层行不行?</strong></p>
<p><strong>A</strong>:</p>
<ul><li>从技术上讲,所有代码写在 Controller 甚至直接写在 JSP 里都能跑,但这不仅难以维护,也没法复用。</li><li><strong>Controller</strong>:负责<strong>接收请求</strong>和<strong>参数校验</strong>。</li><li><strong>Service</strong>:负责<strong>业务逻辑</strong>(比如转账计算、逻辑判断)。这样如果其他 Controller 或者定时任务也需要用到这个逻辑,直接注入 Service 即可,实现了复用。</li></ul>
<p>Q4:<code>@Autowired</code> 和 <code>@Resource</code> 都可以注入,有什么区别?</p>
<p><strong>A</strong>:</p>
<ul><li><code>@Autowired</code>:是 Spring 提供的注解。默认按<strong>类型 (Type)</strong> 装配(只要你是 UserService 类我就注入)。</li><li><code>@Resource</code>:是 JDK (Java标准) 提供的注解。默认按<strong>名称 (Name)</strong> 装配(找 ID 叫 userService 的 bean)。</li></ul>
<p class="maodian"></p><h3>🎯 六、 总结</h3>
<p>Spring Boot 开发其实就是一场流水线作业:</p>
<ol><li><strong>定义实体</strong>:确定数据长什么样。</li><li><strong>写 Repository</strong>:解决怎么存取数据。</li><li><strong>写 Service</strong>:处理复杂的业务规则。</li><li><strong>写 Controller</strong>:把功能暴露出去给别人用。</li></ol>
<p>到此这篇关于Spring Boot 项目开发全流程实战指南的文章就介绍到这了,更多相关Spring Boot 项目开发内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>如何配置cursor进行Java&nbsp;springboot项目开发</li><li>VSCode 配置 Spring Boot 项目开发环境的全过程</li><li>SpringBoot项目开发中常用的依赖</li><li>SpringBoot项目开发常用技术整合</li><li>解析SpringBoot项目开发之Gzip压缩过程</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: Spring Boot 项目开发全流程实战示例总结