侧边栏壁纸
博主头像
qiql博主等级

水能载舟,亦可赛艇

  • 累计撰写 33 篇文章
  • 累计创建 28 个标签
  • 累计收到 20 条评论

目 录CONTENT

文章目录

【搬运】谷粒学院文档-05:新增和修改讲师接口

qiql
2022-07-20 / 0 评论 / 0 点赞 / 707 阅读 / 254 字

一、自动填充封装

1、在service-base模块中添加

创建包handler,创建自动填充类 MyMetaObjectHandler

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("gmtCreate", new Date(), metaObject);
        this.setFieldValByName("gmtModified", new Date(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("gmtModified", new Date(), metaObject);

    }
}

2、在实体类添加自动填充注解

image-1658278894303

二、controller方法定义

1、新增

@ApiOperation(value = "新增讲师")
@PostMapping
public R save(
        @ApiParam(name = "teacher", value = "讲师对象", required = true)
        @RequestBody Teacher teacher){

     teacherService.save(teacher);
     return R.ok();
}

2、根据id查询

@ApiOperation(value = "根据ID查询讲师")
@GetMapping("{id}")
public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){

        Teacher teacher = teacherService.getById(id);
        return R.ok().data("item", teacher);
}

3、根据id修改

@ApiOperation(value = "根据ID修改讲师")
@PutMapping("{id}")
public R updateById(
    @ApiParam(name = "id", value = "讲师ID", required = true)
    @PathVariable String id,

    @ApiParam(name = "teacher", value = "讲师对象", required = true)
    @RequestBody Teacher teacher){

    teacher.setId(id);
    teacherService.updateById(teacher);
    return R.ok();
}
0

评论区