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;
    }
}
相关推荐
豆本-豆豆奶3 分钟前
23个Python在自然语言处理中的应用实例
开发语言·python·自然语言处理·编程语音
曳渔9 分钟前
Java-数据结构-二叉树-习题(三)  ̄へ ̄
java·开发语言·数据结构·算法·链表
shark-chili20 分钟前
数据结构与算法-Trie树添加与搜索
java·数据结构·算法·leetcode
白乐天_n22 分钟前
FRIDA-JSAPI:Java使用
java·jsapi·frida
你可以自己看25 分钟前
python中函数式编程与高阶函数,装饰器与生成器,异常处理与日志记录以及项目实战
服务器·开发语言·python
无奇不有 不置可否32 分钟前
JVM基础篇学习笔记
java·jvm
gopher951143 分钟前
go语言 数组和切片
开发语言·golang
ymchuangke43 分钟前
线性规划------ + 案例 + Python源码求解(见文中)
开发语言·python
gopher95111 小时前
go语言Map详解
开发语言·golang
Python私教1 小时前
Go语言现代web开发15 Mutex 互斥锁
开发语言·前端·golang