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();
}

最终将集合数据返回给DeptService,DeptService又返回给DeptController。DeptController拿到数据再返回给前端。

相关推荐
潍坊老登27 分钟前
什么?我的官网最近流量异常增加?
后端
CoderYanger1 小时前
Java EE 进阶:12.Spring AOP(第二弹)
java·前端·数据库·spring boot·spring·java-ee·mybatis
利威尔xu1 小时前
2026-11-24-Go限流器实战
后端·golang
CoderYanger2 小时前
Java EE 进阶:12.Spring AOP(第一弹)
java·开发语言·spring boot·后端·spring·java-ee·交互
H H.3 小时前
Java笔记:String字符串
java·开发语言·经验分享·笔记·其他
Rain的Java大神之路3 小时前
Spring AI 还是 LangChain4j?2026 年 Java 接入大模型,我把这笔「架构赌注」的账算清楚了
java·后端·ai编程
用户112936112193 小时前
注释掉的接口为什么还能访问?没有 Git 也能证明改动有没有进产物
java
Csvn3 小时前
Nginx 高可用网关:VIP 切换、proxy_protocol 与 SNI 分流实战
后端·nginx
Gopher_HBo3 小时前
跟着一次导出,走完 runtime/pprof:从一张工单到一份文件
后端
sharemylove4 小时前
Java 异常处理 学习笔记
java·开发语言·经验分享·笔记·学习