Springboot三层架构

Controller层

java 复制代码
package com.wzb.springbootmybatis20240920.ThreeLevelsExercise20240920.controller;

import com.wzb.springbootmybatis20240920.Pojo.Emp;
import com.wzb.springbootmybatis20240920.Pojo.Result;

import com.wzb.springbootmybatis20240920.ThreeLevelsExercise20240920.service.Service;
import com.wzb.springbootmybatis20240920.ThreeLevelsExercise20240920.service.ServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

// 注解
@RestController
public class Controller {
    @Autowired
    ServiceInterface service;


    @RequestMapping("/emp")
    public Result listEmp() {
        List<Emp> empList = service.getList();
        return Result.success(empList);
    }
}

Dao层

java 复制代码
package com.wzb.springbootmybatis20240920.ThreeLevelsExercise20240920.dao;


import com.wzb.springbootmybatis20240920.Pojo.Emp;
import com.wzb.springbootmybatis20240920.ThreeLevelsExercise20240920.utils.XmlParserUtils;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class Dao implements DaoInterface{
    @Override
    public List<Emp> getList() {
        // 加载.xml文件
        String filePath = this.getClass().getClassLoader().getResource("emp.xml").getFile();
        // 利用dom4j解析.xml文件
        // parse方法需要两个参数,需要解析文件的路径和要解析成的对象
        List<Emp> empList = XmlParserUtils.parse(filePath, Emp.class);
        return empList;
    }
}

Service层

java 复制代码
package com.wzb.springbootmybatis20240920.ThreeLevelsExercise20240920.service;


import com.wzb.springbootmybatis20240920.Pojo.Emp;
import com.wzb.springbootmybatis20240920.ThreeLevelsExercise20240920.dao.Dao;
import com.wzb.springbootmybatis20240920.ThreeLevelsExercise20240920.dao.DaoInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.function.Consumer;

@Component
public class Service implements ServiceInterface{
    @Autowired
    private DaoInterface dao;


    @Override
    public List<Emp> getList() {
        List<Emp> empList = dao.getList();
        // 按照业务逻辑处理userList中的数据
        empList.forEach(new Consumer<Emp>() {
            @Override
            public void accept(Emp emp) {
                String gender = emp.getGender();
                if (gender.equals("1")) {
                    emp.setGender("男");
                } else if (gender.equals("2")) {
                    emp.setGender("女");
                } else {
                    emp.setGender("Error");
                }

                String job = emp.getJob();
                switch (job) {
                    case "1" -> emp.setJob("讲师");
                    case "2" -> emp.setJob("班主任");
                    case "3" -> emp.setJob("就业指导");
                    default -> emp.setJob("Other");
                }
            }
        });
        return empList;
    }
}
相关推荐
крон2 小时前
【Auto.js例程】华为备忘录导出到其他手机
开发语言·javascript·智能手机
zh_xuan2 小时前
c++ 单例模式
开发语言·c++·单例模式
coderSong25683 小时前
Java高级 |【实验八】springboot 使用Websocket
java·spring boot·后端·websocket
老胖闲聊3 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1183 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
Mr_Air_Boy3 小时前
SpringBoot使用dynamic配置多数据源时使用@Transactional事务在非primary的数据源上遇到的问题
java·spring boot·后端
曹勖之3 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
豆沙沙包?4 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
打码人的日常分享4 小时前
物联网智慧医院建设方案(PPT)
大数据·物联网·架构·流程图·智慧城市·制造
军训猫猫头4 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#