springboot查询全部部门流程

前端发送请求后,会请求DeptController的方法list()

java 复制代码
package com.intelligent_learning_aid_system.controller;


import com.intelligent_learning_aid_system.pojo.Dept;
import com.intelligent_learning_aid_system.pojo.Result;
import com.intelligent_learning_aid_system.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@Slf4j
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    //    @RequestMapping(value = "/depts", method = RequestMethod.GET) // 指定请求参数为 GET
    @GetMapping("/depts") // 等同于上面的写法
    public Result list() {
//        System.out.println("查询全部部门数据");
        log.info("查询全部部门数据");

        // 调用service查询部门数据
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }



}

list()中调用DeptService获取数据。

DeptService中调用DeptMapper接口中的方法来查询全部的部门信息。

java 复制代码
package com.intelligent_learning_aid_system.service;


import com.intelligent_learning_aid_system.pojo.Dept;

import java.util.List;

/**
 * 部门管理
 */
public interface DeptService {

    /**
     * 查询全部部门
     * @return
     */
    List<Dept> list();
}
java 复制代码
package com.intelligent_learning_aid_system.service.impl;


import com.intelligent_learning_aid_system.mapper.DeptMapper;
import com.intelligent_learning_aid_system.pojo.Dept;
import com.intelligent_learning_aid_system.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    /**
     * 查询全部部门
     */
    public List<Dept> list() {
        return deptMapper.list();
    }
}

DeptMapper接口会往数据库发送SQL语句,查询全部的部门,并且把查询的信息封装到List<Dept>集合中。

java 复制代码
package com.intelligent_learning_aid_system.mapper;

import com.intelligent_learning_aid_system.pojo.Dept;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
 * 部门管理
 */
@Mapper
public interface DeptMapper {

    /**
     * 查询全部部门
     * @return
     */
    @Select("select * from dept")
    List<Dept> list();
}

最终将集合数据返回给DeptServiceDeptService又返回给DeptControllerDeptController拿到数据再返回给前端。

相关推荐
Escalating_xu3 小时前
【C++ string 上篇】从字符串基础到容量管理、迭代器与经典算法题
java·c++·算法
曹牧4 小时前
Java:Java 数组转 List
java·开发语言·windows
下辈子不要当码农4 小时前
Day7-Linux软件编程(进程与线程(3))
java·开发语言
AI多Agent协作实战派4 小时前
AI多Agent协作系统实战(五十二):我删了定时器,进程还在偷偷干活
后端
星火10244 小时前
【从 0 到 1 动手造 Agent】02、确定性铁笼 LangGraph
人工智能·后端·agent
唐青枫4 小时前
别把 Debug 和 Release 混为一谈:Zig Build Mode 全面解析
后端
A_nanda4 小时前
拆解ASP.NET Core 底层源码!从零手写
后端·asp.net
钱栈up4 小时前
别忽略INFO日志:我用AI编码助手1小时修完3个接口的隐藏Bug
后端·json·trae
星火10244 小时前
【从 0 到 1 动手造 Agent】03、给 Agent 装上操作系统——MemGPT/Letta 内存分层与自我演化
人工智能·后端·agent
万物智能4 小时前
OpenHarmony源码树解剖—【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
前端·后端