news 2026/4/23 11:22:13

简单认识JavaWeb的三层架构与分层解耦

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
简单认识JavaWeb的三层架构与分层解耦

1.示例

public class UserController { @RequestMapping("/list") public List<User> list() throws FileNotFoundException { //1.获取数据 InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt"); ArrayList<String> list = IoUtil.readLines(in, StandardCharsets.UTF_8,new ArrayList<>()); //2.解析用户信息,封装为user对象 -> list集合 List<User> userList =list.stream().map(line -> { String[] items = line.split(","); Integer id = Integer.parseInt(items[0]); String username = items[1]; String password = items[2]; String name = items[3]; Integer age = Integer.parseInt(items[4]); LocalDateTime updateTime = LocalDateTime.parse(items[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); return new User(id,username,password,name,age,updateTime); }).toList(); // 3.返回数据 return userList; } }

缺点:功能不单一,复用性差,无法分层解耦,维护难等等

2. JavaWeb三层架构的核心组成(将示例代码分层解耦)

  • 表现层(Presentation Layer)
    • 职责:用户交互与请求处理(如Servlet、Controller)
    • 技术实现:Spring MVC、JSP/Thymeleaf等

示例:

package com.itheima.springbpptweb01.controller; import com.itheima.springbpptweb01.pojo.User; import com.itheima.springbpptweb01.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.io.FileNotFoundException; import java.util.List; @RestController // 包装了Conroller public class UserController { @Autowired private UserService userService; @RequestMapping("/list") public List<User> list() throws FileNotFoundException { // 1.调用service,获取数据 List<User> userList = userService.findAll(); //3.返回数据(josn) return userList; } }
  • 业务逻辑层(Service Layer)
    • 职责:核心业务规则与流程控制
    • 技术实现:Spring Service组件、事务管理

示例:

package com.itheima.springbpptweb01.service; import com.itheima.springbpptweb01.dao.UserDao; import com.itheima.springbpptweb01.dao.UserDaoImpl; import com.itheima.springbpptweb01.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; @Service //@Component public class UserServiceImpl implements UserService{ @Autowired // 用多态创建UserDao private UserDao userDao; @Override public List<User> findAll() { // 1.调用dao层方法获取 数据 List<String> list = userDao.findAll(); //2.解析用户信息,封装为user对象 -> list集合 List<User> userList =list.stream().map(line -> { String[] items = line.split(","); Integer id = Integer.parseInt(items[0]); String username = items[1]; String password = items[2]; String name = items[3]; Integer age = Integer.parseInt(items[4]); LocalDateTime updateTime = LocalDateTime.parse(items[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); return new User(id,username,password,name,age,updateTime); }).toList(); return userList; } }
  • 数据访问层(DAO/Repository Layer)
    • 职责:数据持久化与数据库交互
    • 技术实现:JDBC、MyBatis、JPA/Hibernate

示例:

package com.itheima.springbpptweb01.dao; import cn.hutool.core.io.IoUtil; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @Repository // @Repository("userDao") 这是指定Bean的名字,IOC容器的,通常不用 //@Component public class UserDaoImpl implements UserDao{ @Override public List<String> findAll() { // InputStream in = new FileInputStream(new File("D:\\Java代码放置地\\JAVA_WEB\\web-ai-pj01\\maven-projectDemo1\\springbppt-web-01\\src\\main\\resources\\user.txt")) InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt"); ArrayList<String> list = IoUtil.readLines(in, StandardCharsets.UTF_8,new ArrayList<>()); return list; } }

通过三层架构便可达成高内聚低耦合的编程思想

3. 分层解耦的实现方法

  • 依赖注入(DI)与控制反转(IOC也称呼为Spring容器)
    • 通过Spring框架管理对象生命周期与依赖关系
    • 示例代码:@Autowired注解的使用

(1)Dao层注解(需要加@Repository注解)

(2) Service层注解(Service)

@Service

  • 用于标注业务逻辑层组件
  • 也是@Component的特化版本
  • 表示服务层业务逻辑

(3) Controller层注解

@Controller

  • 用于标注控制层组件
  • 处理HTTP请求
  • 通常与@RequestMapping配合使用

@Autowired 的作用

@Autowired是 Spring 框架提供的一个注解,用于实现依赖注入(Dependency Injection, DI)。它允许 Spring 容器自动将所需的依赖对象注入到目标组件中,无需手动编写代码来创建或查找依赖。

4. 分层架构的优势与挑战

  • 优势
    • 代码复用性高,各层职责单一
    • 便于团队协作与单元测试
    • 系统扩展性增强(如替换DAO实现不影响业务逻辑)
  • 挑战
    • 过度分层可能导致性能损耗
    • 层间调用规范需严格设计(如DTO传递)
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 11:21:57

Java毕设选题推荐:基于JAVA的SpringBoot框架应急物资管理系统基于springboot的救援物资管理系统的设计与实现【附源码、mysql、文档、调试+代码讲解+全bao等】

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2026/4/23 11:21:57

USB转串口Linux驱动编写实战案例解析

从零构建 USB 转串口 Linux 驱动&#xff1a;一次深入内核的实战之旅你有没有遇到过这样的场景&#xff1f;手头有个老旧的 GPS 模块、PLC 控制器或者单片机开发板&#xff0c;只支持 RS232 串口通信。而你的现代笔记本早已砍掉了 COM 口&#xff0c;只剩下几个 USB 接口。这时…

作者头像 李华
网站建设 2026/4/23 11:19:40

NxNandManager深度技术解析:专业级Switch存储管理解决方案

NxNandManager深度技术解析&#xff1a;专业级Switch存储管理解决方案 【免费下载链接】NxNandManager Nintendo Switch NAND management tool : explore, backup, restore, mount, resize, create emunand, etc. (Windows) 项目地址: https://gitcode.com/gh_mirrors/nx/NxN…

作者头像 李华
网站建设 2026/4/19 19:35:37

千兆以太网PHY层PCB布局布线项目应用

千兆以太网PHY层PCB设计实战&#xff1a;从信号完整性到可靠通信的工程之道 在工业控制、边缘计算和智能监控设备中&#xff0c;千兆以太网早已不是“高端配置”&#xff0c;而是系统稳定运行的 基本保障 。但你是否遇到过这样的问题&#xff1a; 板子焊好了&#xff0c;RJ4…

作者头像 李华
网站建设 2026/4/16 14:13:16

SMUDebugTool:5分钟搞定AMD平台硬件调试的终极指南

SMUDebugTool&#xff1a;5分钟搞定AMD平台硬件调试的终极指南 【免费下载链接】SMUDebugTool A dedicated tool to help write/read various parameters of Ryzen-based systems, such as manual overclock, SMU, PCI, CPUID, MSR and Power Table. 项目地址: https://gitco…

作者头像 李华