自学内容网 自学内容网

javaweb黑马的 部分查询和修改

根据所给的api知道,查询和修改的Mapping等。

在这里插入图片描述

对于查询来说,要根据id获得单行数据内容返回。

对于修改来说,根据JSON对学生(Dept类)进行UPDATE的操作。

`

DeptController.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
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

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

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

    /**
     * 删除部门
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        log.info("根据id - {}删除部门数据", id);
        // 调用service删除部门
        return deptService.deleteById(id);
    }

    /**
     * 新增部门
     */
    @PostMapping("")
    public Result add(@RequestBody Dept dept) {
        log.info("新增部门数据 - {}", dept);
        deptService.add(dept);
        return Result.success();
    }

    /**
     * 按id查询部门
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable("id") Integer id) {
        log.info("根据id - {}查询部门数据", id);
        // 调用service查询部门数据
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    /**
     * 修改部门
     */
    @PutMapping("")
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门数据 - {}", dept);
        // 调用service修改部门
        deptService.update(dept);
        return Result.success();
    }
}

DeptService.java

package com.intelligent_learning_aid_system.service;


import com.intelligent_learning_aid_system.pojo.Dept;
import com.intelligent_learning_aid_system.pojo.Result;

import java.util.List;

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

    /**
     * 查询全部部门
     * @return
     */
    List<Dept> list();
    /**
     * 删除部门
     */
    Result deleteById(Integer id);

    /**
     * 新增部门
     */
    void add(Dept dept);

    /**
     * 按id查询部门
     * @return
     */
    Dept getById(Integer id);

    /**
     * 修改部门
     * @param dept
     */
    void update(Dept dept);
}

DeptServiceImpl.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.pojo.Result;
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.time.LocalDateTime;
import java.util.List;


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

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

    /**
     * 删除部门
     */
    public Result deleteById(Integer id) {
        deptMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 新增部门
     */
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.add(dept);
    }

    /**
     * 按id查询部门
     * @return
             */
    public Dept getById(Integer id) {
        Dept dept = deptMapper.getById(id);
         return dept;
    }
    /**
     * 修改部门
     * @param dept
     */
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}

DeptMapper.java

package com.intelligent_learning_aid_system.mapper;

import com.intelligent_learning_aid_system.pojo.Dept;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

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

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

    /**
     * 删除部门
     * @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 add(Dept dept);

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

    /**
     * 修改部门
     * @param dept
     */
    @Update("update dept set name = #{name}, update_time = #{updateTime} where id = #{id}")
    void update(Dept dept);
}


原文地址:https://blog.csdn.net/qq_63432403/article/details/142829244

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!