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;
    }
}
相关推荐
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS体育馆管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
Miketutu2 小时前
Spring MVC消息转换器
java·spring
乔冠宇2 小时前
Java手写简单Merkle树
java·区块链·merkle树
LUCIAZZZ3 小时前
简单的SQL语句的快速复习
java·数据库·sql
来恩10033 小时前
C# 类与对象详解
开发语言·c#
komo莫莫da3 小时前
寒假刷题Day19
java·开发语言
小小虫码3 小时前
项目中用的网关Gateway及SpringCloud
spring·spring cloud·gateway
ElseWhereR3 小时前
C++ 写一个简单的加减法计算器
开发语言·c++·算法
EchoToMe4 小时前
电信传输基本理论/5G网络层次架构——超三万字详解:适用期末考试/考研/工作
网络·5g·架构
计算机-秋大田4 小时前
基于微信小程序的电子竞技信息交流平台设计与实现(LW+源码+讲解)
spring boot·后端·微信小程序·小程序·课程设计