欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 艺术 > javaweb黑马的 部分查询和修改

javaweb黑马的 部分查询和修改

2025/4/16 20:12:57 来源:https://blog.csdn.net/qq_63432403/article/details/142829244  浏览:    关键词: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 {@Autowiredprivate 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 {@Autowiredprivate 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);
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词