news 2026/1/25 4:48:41

简单认识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/1/23 14:13:42

5分钟快速上手SMUDebugTool:AMD电源调试的完整解决方案

5分钟快速上手SMUDebugTool&#xff1a;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://g…

作者头像 李华
网站建设 2026/1/23 17:16:36

5分钟掌握Windows防休眠工具:NoSleep永不锁屏终极指南

5分钟掌握Windows防休眠工具&#xff1a;NoSleep永不锁屏终极指南 【免费下载链接】NoSleep Lightweight Windows utility to prevent screen locking 项目地址: https://gitcode.com/gh_mirrors/nos/NoSleep 你是否遇到过这些令人烦恼的场景&#xff1f;正在为客户演示…

作者头像 李华
网站建设 2026/1/23 16:41:00

PPTTimer:5大核心功能助你精准掌控演示时间

PPTTimer&#xff1a;5大核心功能助你精准掌控演示时间 【免费下载链接】ppttimer 一个简易的 PPT 计时器 项目地址: https://gitcode.com/gh_mirrors/pp/ppttimer 在各类演讲、汇报和教学演示中&#xff0c;时间管理始终是影响成败的关键因素。PPTTimer作为一款专业的演…

作者头像 李华
网站建设 2026/1/23 16:41:06

AMD Ryzen处理器调试利器:5个技巧让你轻松玩转SMUDebugTool

还在为AMD处理器性能调优而苦恼吗&#xff1f;想要深入掌握Ryzen平台的底层参数调节却无从下手&#xff1f;今天我们就来聊聊这款专业的硬件调试工具——SMUDebugTool&#xff0c;看看它是如何帮你打开处理器性能调优的新世界&#xff01; 【免费下载链接】SMUDebugTool A dedi…

作者头像 李华
网站建设 2026/1/20 14:24:46

成本意识在研发管理中如何落实

要在研发管理中有效落实成本意识&#xff0c;核心在于推动一个根本性的思维转变&#xff1a;即从被动的“成本控制”转向主动的“价值投资”。这绝非简单的“省钱”或“砍预算”&#xff0c;而是要求将“投入产出比&#xff08;ROI&#xff09;”的核算&#xff0c;系统性地融入…

作者头像 李华
网站建设 2026/1/22 6:27:15

从 ScriptProcessor 到 AudioWorklet:Electron 桌面端录音实践总结

实践总结开发 Electron 桌面端应用时&#xff0c;我遇到了一个常见但又棘手的问题&#xff1a;录音功能。本文将分享我的实践经历&#xff0c;包括为什么 ScriptProcessor 蓝屏、为什么 AnalyserNode 会导致音频噪声&#xff0c;以及最终使用 AudioWorklet 的完整解决方案。一、…

作者头像 李华