【JavaWeb基础】三层架构

文章详细介绍了JavaSpring项目中使用三层架构(Controller、Service、Dao)进行代码分层处理的过程。首先定义了各层的职责,然后展示了如何将原本集中在Controller中的数据处理逻辑拆分到Service和Dao层,以此提高代码的复用性和可维护性。最后,文章强调了三层架构的优点,如增强复用性、便于维护和拓展,并推荐了一个分层解耦的学习教程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、知识点整理

三层架构的含义

Controller: 控制层,接收前端发送的请求,对请求进行处理,并响应数据。
Service:业务逻辑层,处理具体的业务逻辑。
Dao(Data Access Object): 数据访问层(持久层),负责数据访问操作,包括数据的增、删、改、查。

二、代码实现

原EmpController.java文件中的代码:

package com.example.controller;

import com.example.pojo.Emp;
import com.example.pojo.Result;
import com.example.service.EmpService;
import com.example.service.impl.EmpServiceA;
import com.example.utils.XmlParserUtils;
import com.fasterxml.jackson.annotation.JsonTypeId;
import jdk.jshell.spi.ExecutionControl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class EmpController {


    @RequestMapping("/listEmp")
    public Result list(){

        //1、加载并解析emp.xml
        String file=this.getClass().getClassLoader().getResource("emp.xml").getFile();
        System.out.println(file);
        List<Emp> empList= XmlParserUtils.parse(file,Emp.class);

        //2、对数据进行转换处理
        empList.stream().forEach(emp->{
            //book 1:十二钗正册,2:十二钗副册,3:十二钗又副册
            String book=emp.getBook();
            if("1".equals(book)){
                emp.setBook("十二钗正册"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林月明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值