当前位置: 首页 > news >正文

案例-02.部门管理-查询

一.查询部门-需求

二.查询部门-思路

API接口文档

 

 三.代码实现

1.controller层:负责与前端进行交互,接收前端所发来的请求

注:Slf4j用于记录日志使用,可以省略private static Logger log = LoggerFactory.getLogger(DeptController.class);这行代码从而直接调用log对象。

注:@RequestMapping(value = "/depts",method = RequestMethod.GET)   指定请求方式为GET
但是这种请求方式过于麻烦,因此使用@GetMapping()注解,其含义也是请求方式为Get

package com.gjw.controller;/*** 部门管理Controller*/import com.gjw.anno.Log;
import com.gjw.pojo.Dept;
import com.gjw.pojo.Result;
import com.gjw.service.DeptService;
import com.gjw.service.impl.DeptServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@Slf4j // 记录日志使用
@RestControllerpublic class DeptController {@Autowiredprivate DeptService deptService;//    @RequestMapping(value = "/depts",method = RequestMethod.GET)   指定请求方式为GET@GetMapping("/depts")   // 指定请求方式为GETpublic Result list(){log.info("查询全部部门数据");// 调用service层查询全部部门数据List<Dept> deptList = deptService.list();return Result.success(deptList);}}

设置Controller层接收前端发来的Get请求方式,url请求地址为/depts的请求后,controller层负责调用service层,由service层进行逻辑处理。因此通过依赖注入@Autowired来注入Service层的对象deptService。最后返回给前端的是一个统一响应结果Result。Result中封装的数据是查询出来的全部部门数据,封装在一个list集合当中。

2.service层:用来进行逻辑处理,并连接dao层,将从Dao层获得到的数据返回给controller层

service层接口:

package com.gjw.service;import com.gjw.pojo.Dept;import java.util.List;public interface DeptService {List<Dept> list();
}

service层实现类:

package com.gjw.service.impl;import com.gjw.mapper.DeptLogMapper;
import com.gjw.mapper.DeptMapper;
import com.gjw.mapper.EmpMapper;
import com.gjw.pojo.Dept;
import com.gjw.pojo.DeptLog;
import com.gjw.service.DeptLogService;
import com.gjw.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import java.time.LocalDateTime;
import java.util.List;@Service
public class DeptServiceImpl implements DeptService {@Autowiredprivate DeptMapper deptMapper;@Overridepublic List<Dept> list() {return deptMapper.list();}}

service层中的list方法使用注入的deptMapper对象调用list方法来进行数据的获取。

3.Dao层:连接数据库进行数据的获取并返回给service层

package com.gjw.mapper;import com.gjw.anno.Log;
import com.gjw.pojo.Dept;
import org.apache.ibatis.annotations.*;import java.util.List;/*** 部门管理*/
@Mapper
public interface DeptMapper {/*** 查询全部部门数据* @return*/@Select("select * from dept")List<Dept> list();}

使用list方法查询全部的部门数据,并以List<Dept>集合的方式由service层返回到controller层。并在controller层通过统一响应方式Result响应给前端

http://www.lryc.cn/news/537406.html

相关文章:

  • src和href区别
  • Java每日精进·45天挑战·Day19
  • 区块链的交易管理和共识机制
  • 最新国内 ChatGPT Plus/Pro 获取教程
  • Apollo 9.0 速度动态规划决策算法 – path time heuristic optimizer
  • Apache Iceberg 与 Apache Hudi:数据湖领域的双雄对决
  • 【LeetCode Hot100 普通数组】最大子数组和、合并区间、旋转数组、除自身以外数组的乘积、缺失的第一个正整数
  • 共享存储-一步一步部署ceph分布式文件系统
  • 19.Python实战:实现对博客文章的点赞系统
  • 【stm32】定时器输出PWM波形(hal库)
  • 当Ollama遇上划词翻译:我的Windows本地AI服务搭建日记
  • Linux上Elasticsearch 集群部署指南
  • 字节Trae使用感想(后端)
  • 国产编辑器EverEdit - 二进制模式下观察Window/Linux/MacOs换行符差异
  • 文心一言4月起全面免费,6月底开源新模型:AI竞争进入新阶段?
  • 解锁机器学习算法 | 线性回归:机器学习的基石
  • 如何使用Three.js制作3D月球与星空效果
  • SQL语句语法
  • github上文件过大无法推送问题
  • 微信小程序的请求函数封装(ts版本,uniapp开发)
  • Visual Studio Code支持WSL,直接修改linux/ubuntu中的文件
  • openAI最新o1模型 推理能力上表现出色 准确性方面提升 API如何接入?
  • GC 基础入门
  • Go语言协程Goroutine高级用法(一)
  • DeepSeek处理自有业务的案例:让AI给你写一份小众编辑器(EverEdit)的语法着色文件
  • 【鸿蒙HarmonyOS Next实战开发】lottie动画库
  • PAT乙级真题 — 1084 外观数列(java)
  • 从 ClickHouse 到 Apache Doris:在网易云音乐日增万亿日志数据场景下的落地
  • STM32——HAL库开发笔记19(串口中断接收实验)(参考来源:b站铁头山羊)
  • 清影2.0(AI视频生成)技术浅析(二):自然语言处理