springboot查询全部部门流程分析

 更新时间:2024年10月14日 11:41:19   作者:golemon  
本文分析了在SpringBoot框架中前端如何请求DeptController的list()方法,并通过DeptService到DeptMapper接口查询数据库中的全部部门信息的流程,整个过程涉及前端到后端数据的获取和返回,是SpringBoot应用中常见的数据处理模式

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

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接口中的方法来查询全部的部门信息。

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();
}
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>集合中。

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拿到数据再返回给前端。

到此这篇关于springboot查询全部部门流程的文章就介绍到这了,更多相关springboot查询全部部门内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java Mybatis框架增删查改与核心配置详解流程与用法

    Java Mybatis框架增删查改与核心配置详解流程与用法

    MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO为数据库中的记录
    2021-10-10
  • Spring中的注解@Value("#{}")与@Value("${}")的区别介绍

    Spring中的注解@Value("#{}")与@Value("${}")的区别

    这篇文章主要介绍了Spring中的注解@Value(“#{}“)与@Value(“${}“)的区别到底是什么,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06
  • java8新特性 stream流的方式遍历集合和数组操作

    java8新特性 stream流的方式遍历集合和数组操作

    这篇文章主要介绍了java8新特性 stream流的方式遍历集合和数组操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • SpringBoot测试junit遇到的坑及解决

    SpringBoot测试junit遇到的坑及解决

    这篇文章主要介绍了SpringBoot测试junit遇到的坑及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Java多线程下载文件实现案例详解

    Java多线程下载文件实现案例详解

    这篇文章主要介绍了Java多线程下载文件实现案例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • StringUtils中的isEmpty、isNotEmpty、isBlank和isNotBlank的区别详解

    StringUtils中的isEmpty、isNotEmpty、isBlank和isNotBlank的区别详解

    这篇文章主要介绍了StringUtils中的isEmpty、isNotEmpty、isBlank和isNotBlank的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • 关于Tomcat出现The origin server did not find a current representation for the target resourc...的问题

    关于Tomcat出现The origin server did not find a current represent

    这篇文章主要介绍了关于Tomcat出现The origin server did not find a current representation for the target resourc...的问题,感兴趣的小伙伴们可以参考一下
    2020-08-08
  • 详解Android系统中的root权限获得原理

    详解Android系统中的root权限获得原理

    这篇文章主要介绍了详解Android系统中的Root权限获得原理,安卓基于Linux,所以原理也相当于Linux中的root用户,需要的朋友可以参考下
    2015-08-08
  • Java字符串如何转化date

    Java字符串如何转化date

    Java字符串转换为Date对象,通常需要使用SimpleDateFormat类,该类提供了日期格式化和解析的方法,但需要注意日期格式模式的选择、异常处理和线程安全性
    2025-02-02
  • Java过滤器@WebFilter用法详解

    Java过滤器@WebFilter用法详解

    @WebFilter用于将一个类声明为过滤器,该注解将会在部署时被容器处理,容器将根据具体的属性配置将相应的类部署为过滤器,这篇文章主要给大家介绍了关于Java过滤器@WebFilter用法的相关资料,需要的朋友可以参考下
    2024-01-01

最新评论