当前位置: 首页 > news >正文

Spring Boot 三层架构【清晰易懂】

Spring Boot 的三层架构是一种常见的软件设计模式,它将应用程序分为三个主要部分:控制器层(Controller)、服务层(Service) 和 数据访问层(Repository)。这种分层架构有助于代码的模块化和复用,使应用程序更易于维护和扩展


一、表示层(Controller)

  • 含义:这一层是用户和系统交互的接口,是用户看到并使用的部分。一般由控制器(Controller)组成,负责接收用户请求和返回处理结果
  • 作用:表示层接收用户请求,把请求数据交给业务层处理,最后将业务层的结果返回给用户。它相当于应用的“前台”,负责和用户“对话”。

例子

package com.example.demo.controller;import com.example.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@Autowiredprivate HelloService helloService;@GetMapping("/hello")public String hello() {return helloService.getMessage();}
}

在这个例子中,HelloController 接收用户的 HTTP 请求,并调用 HelloService 来获取数据,然后将结果返回给用户。


二、业务层(Service)

  • 含义:业务层包含了应用的核心逻辑,即系统如何处理数据和实现业务规则。它是整个系统的“大脑”。
  • 作用:业务层接收表示层传来的请求数据,执行具体的业务逻辑,比如计算、验证、状态更新等。处理完之后,它将结果返回给表示层。通过将业务逻辑集中在这一层,系统的核心规则更加明确,也便于后续修改和维护。

例子

package com.example.demo.service;import org.springframework.stereotype.Service;@Service
public class HelloService {public String getMessage() {return "Hello, Spring Boot!";}
}

在这个例子中,HelloService 提供了一个简单的业务逻辑方法 getMessage(),它返回一条消息。


三、数据访问层(Repository)

  • 含义:数据访问层负责和数据库打交道。它提供了读取、存储、更新和删除数据的接口。
  • 作用:数据访问层负责从数据库中取出数据,或者将数据存入数据库。它相当于系统的“数据存储员”,将数据管理的细节与业务逻辑分离开来,使得业务层不需要关注具体的数据操作。

例子: 假设我们有一个 User 实体类和一个 UserRepository 接口:

package com.example.demo.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;private String email;// Getters and Setters
}
package com.example.demo.repository;import com.example.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}

在这个例子中,UserRepository 是一个接口,它继承了 JpaRepository,提供了基本的数据库操作方法。

三层架构的好处

1. 模块清晰:每一层都有单一的职责,代码结构清晰。

2. 便于维护:各层相互独立,修改其中一层时不会影响其他层。

3. 便于测试:可以单独测试业务逻辑、数据访问等,方便调试和排错。

4. 提高代码复用:业务层和数据层的代码可以被多个界面或模块重复利用。

三层架构帮助我们将系统逻辑分隔开来,使得代码更易管理、扩展和维护,是一种构建稳定可靠应用的好方法。


完整的代码示例

假设我们要创建一个简单的用户管理系统,用户可以通过 HTTP 请求添加和获取用户信息。

1. 实体类(User)
package com.example.demo.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;private String email;// Getters and Setters
}
2. 数据访问层(UserRepository)
package com.example.demo.repository;import com.example.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
3. 业务层(UserService)
package com.example.demo.service;import com.example.demo.model.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserService {@Autowiredprivate UserRepository userRepository;public List<User> getAllUsers() {return userRepository.findAll();}public User saveUser(User user) {return userRepository.save(user);}
}
4. 表示层(UserController)
package com.example.demo.controller;import com.example.demo.model.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
@RequestMapping("/users")
public class UserController {@Autowiredprivate UserService userService;@GetMappingpublic List<User> getAllUsers() {return userService.getAllUsers();}@PostMappingpublic User createUser(@RequestBody User user) {return userService.saveUser(user);}
}

总结

Spring Boot 的三层架构将应用程序分为控制器层、服务层和数据访问层,每一层都有明确的职责:

  • 控制器层:处理用户请求,调用服务层。

  • 服务层:处理业务逻辑。

  • 数据访问层:与数据库交互,执行数据操作。

这种分层架构有助于代码的模块化和复用,使应用程序更易于维护和扩展。


http://www.mrgr.cn/news/95892.html

相关文章:

  • npm常用的命令
  • DM9162使用记录
  • 人工智能通识速览
  • 漏洞挖掘---锐明Crocus系统Service.do接口任意文件读取
  • VSCode中使用Markdown以及Mermaid实现流程图和甘特图等效果
  • 深度学习入门之基于MLP的加州房价预测模型
  • 电机倍频曲线的一些奇异特性-原因分析及应用
  • 【Hugging Face 开源库】Diffusers 库 ——扩散模型
  • esp32s3聊天机器人(三)
  • QTcpSocket多线程连接慢问题
  • Tekton系列之实践篇-从触发到完成的完整执行过程
  • 蓝桥杯高频考点——搜索(含C++源码)
  • C++学习:六个月从基础到就业——C++基础语法回顾:指针与引用基础
  • html和css 实现元素顺时针旋转效果(椭圆形旋转轨迹)
  • 【react】在react中async/await一般用来实现什么功能
  • 【Java】Springboot集成itextpdf制作pdf(内附pdf添加表格、背景图、水印,条形码、二维码,页码等功能)
  • 从医疗大模型到综合医疗智能体:算法、架构与路径全流程分析
  • aws S3利用lambda edge实现图片缩放、质量转换等常规图片处理功能
  • Java 线程池全面解析
  • Linux输入系统应用编程