开发者学堂课程【微服务+全栈在线教育实战项目演练(SpringCloud Alibaba+SpringBoot):后台讲师管理模块-修改讲师】学习笔记,与课程紧密连接,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/667/detail/11292
后台讲师管理模块-修改讲师
讲师修改功能(修改首先要做查询数据回显,回显之后才能修改,所以修改接口(接口主要是返回数据和操作数据)则有以下两部分方法)
1.根据讲师 id 进行查询
@GetMapping( "getTeacher/ {id}" ) → 因为要根据 id 查询,所以 id 通过路径传递
方法:
public R getTeacher(@PathVariable String id) {
EduTeacher eduTeacher = teacherService. getById(id) :
return R. ok(). data( "teacher", eduTeacher) :
}
打开 swagger,找到 /eduservice/teacher/getTeacher/{id} 点击
id: 2
点击try it out!
{
"success": true,
"code": 20000,
"message": "成功" ,
"data": {
"teacher": {
"id": "2",
"name": "string6S",
"intro": "string" ,
"career": "string",
"level": 2,
"avatar": "string",
"sort": 0,
"isDeleted": false,
"gmtCreate": "1999-11-11 00:00:00" ,
"gmthodified": "2020-02-23 21:22:30"
}
2.讲师修改(测试时候, json 数据需要 id 值,没有 id 值不能做修改)
(1)讲师修改功能,修改应该用 putMapping,但是在操作中,传数据都是通过对象或RequestBody,而RequestBody在传递中需要跟 PostMapping 一起用,所以讲师修改也用 PostMapping,为了用 RequestBody,
当然如果不用 RequestBody,也可以用 putMapping
①@PostMapping ( "updateTeacher" )
public R updateTeacher(@RequestBody EduTeacher eduTeacher) { → //修改在页面中,首先数据要回显,而回显有一个表单,最后一提交,数据则需要提交到 real 中,而提交过程还是用对象传递 RequestBody,而对象则是eduTeacher,且在 Teacher 中会有id值.
boolean flag = teacherService.updateById (eduTeacher);
if(flag) {
return R. ok() :
} else {
return R. error() :
}
}
判断:若为 true,则 ok,若为 false,则是 error
- 根据 id 修改
//在 PutMapping 用 RequestBody 会有一个问题,就是它的id值传的会有问题,那么以下用 PutMapping 是怎么做到的,把 {id}翻译值传过来,然后id值手动设置到对象中去,最终再做修改,若用 postMapping,则没必要用 id 值,把数据都传到 teacher 对象中就可以了
@Api0peration(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();
}
(3)打开swagger,找到 /eduservice/teacher/updateTeacher 点击
eduTeacher: {
"avatar": "string",
"career": "string",
"id": "2", → 必须有id值,时间可以自动填充
"intro": "string" ,
"isDeleted": false
"level": 0,
"name": "string1010upup",
"sort": 0,
}
点击try it out!
{
"success": true,
"code": 20000,
"message":.“成功",
"date": { }
}