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

案例-06.部门管理-根据ID查询

一.根据ID查询-接口文档

 

二.根据ID查询-Controller层 

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 // 记录日志使用
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

//    @RequestMapping(value = "/depts",method = RequestMethod.GET)   指定请求方式为GET
    @GetMapping()   // 指定请求方式为GET
    public Result list(){
        log.info("查询全部部门数据");

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

    @Log
    @DeleteMapping("{id}")  // 指定请求方式为DELETE
    public Result delete(@PathVariable Integer id) throws Exception {
        log.info("根据id删除部门:{}",id);

        // 调用service删除部门
        deptService.deleteById(id);
        return Result.success();
    }

    @Log
    @PostMapping()      // 指定请求方式为Post
    public Result add(@RequestBody Dept dept) { //RequestBody注解可以将前端在请求时所传递的json格式的数据封装成一个实体类来接受
        log.info("新增部门:{}",dept);

        // 调用service新增部门
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询部门信息:{}",id);

        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
}

查询回来的部门要封装在部门对象Dept中,因此deptService通过getById方法的返回对象要封装在部门类的实现类对象dept中。 并传递给同意响应结果result中作为Result的data属性值,并将Result统一响应结果返回给前端。

三.根据ID查询-service层

package com.gjw.service;

import com.gjw.pojo.Dept;

import java.util.List;

public interface DeptService {
    List<Dept> list();

    void deleteById(Integer id) throws Exception;

    void add(Dept dept);

    Dept getById(Integer id);
}

在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 {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }

    @Override
    public void deleteById(Integer id) {
        deptMapper.deleteById(id);
    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    @Override
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }
}

通过deptMapper的mapper层的接口方法调用其接口的getById方法,从而封住查询到的部门数据,返回给controller层。 

 四.根据ID查询-mapper层

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


    /**
     * 根据id删除部门数据
     * @param id
     */
    @Delete("delete from dept where id = #{id}")
    void deleteById(Integer id);

    /**
     * 根据部门名称添加部门
     * @param dept
     */
    @Insert("insert into dept(name, create_time, update_time) VALUES (#{name},#{createTime},#{updateTime})")
    void insert(Dept dept);

    /**
     * 根据id查询部门
     * @param id
     */
    @Select("select * from dept where id = #{id}")
    Dept getById(Integer id);
}


http://www.kler.cn/a/549656.html

相关文章:

  • ESP32通过MQTT连接阿里云平台实现消息发布与订阅
  • 9. Docker 当中的复杂安装(MySQL主从复制的安装,Redis 的3主3从的安装配置)的详细说明[附加步骤图]
  • Linux(ubuntu)下载ollama速度慢解决办法
  • 今日写题06work(链表)
  • Golang GORM系列:GORM数据库迁移
  • Apache 服务启动失败的故障诊断
  • Leetcode 221-最大正方形
  • 使用css实现镂空效果
  • 小初高各学科教材,PDF电子版下载
  • 数据库-通用数据接口标准
  • mysql系列8—Innodb的undolog
  • MVC模式和MVVM模式
  • 【漫话机器学习系列】092.模型的一致性(Consistency of a Model)
  • 国产FPGA开发板选择
  • Spring Boot实战:拦截器
  • 2025百度快排技术分析:模拟点击与发包算法的背后原理
  • yarn add electron --dev --platform=win64 报错 certificate has expired 2025年 解决办法
  • 消息队列(MQ)核心知识与应用场景解析
  • oracle使用动态sql将多层级组织展平
  • git仓库拉取最新更改