一、自动填充封装

1、在service-base模块中添加

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

  1. @Component
  2. public class MyMetaObjectHandler implements MetaObjectHandler {
  3. @Override
  4. public void insertFill(MetaObject metaObject) {
  5. this.setFieldValByName("gmtCreate", new Date(), metaObject);
  6. this.setFieldValByName("gmtModified", new Date(), metaObject);
  7. }
  8. @Override
  9. public void updateFill(MetaObject metaObject) {
  10. this.setFieldValByName("gmtModified", new Date(), metaObject);
  11. }
  12. }

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

image.png
二、controller方法定义

1、新增

  1. @ApiOperation(value = "新增讲师")
  2. @PostMapping
  3. public R save(
  4. @ApiParam(name = "teacher", value = "讲师对象", required = true)
  5. @RequestBody Teacher teacher){
  6. teacherService.save(teacher);
  7. return R.ok();
  8. }

2、根据id查询

  1. @ApiOperation(value = "根据ID查询讲师")
  2. @GetMapping("{id}")
  3. public R getById(
  4. @ApiParam(name = "id", value = "讲师ID", required = true)
  5. @PathVariable String id){
  6. Teacher teacher = teacherService.getById(id);
  7. return R.ok().data("item", teacher);
  8. }

3、根据id修改

  1. @ApiOperation(value = "根据ID修改讲师")
  2. @PutMapping("{id}")
  3. public R updateById(
  4. @ApiParam(name = "id", value = "讲师ID", required = true)
  5. @PathVariable String id,
  6. @ApiParam(name = "teacher", value = "讲师对象", required = true)
  7. @RequestBody Teacher teacher){
  8. teacher.setId(id);
  9. teacherService.updateById(teacher);
  10. return R.ok();
  11. }