关于前后端交互的注解
@RestController
如果在类上加上@RestController注解,该类中所有SpringMVCurl接口映射都是返回json格式
@RestController是SpringMVC提供的
相当于该类使用@Controller注解,并在类中的每个方法上都加上了@ResponseBody注解
@ResponseBody
@responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后(一般是将java对象转为json格式的数据),写入到response对象的body区,通常用来返回JSON数据。
@RequestMapping
@RequestMapping来映射URL
注解 @RequestMapping 可以用在类定义处和方法定义处。
类定义处:规定初步的请求映射,相对于web应用的根目录;
方法定义处:进一步细分请求映射,相对于类定义处的URL。如果类定义处没有使用该注解,则方法标记的URL相对于根目录而言;
@RestController
@RequestMapping("v1/student")
public class StudentController {
@PostMapping("/list/{currentPage}/{pageSize}")
public ResultJson index(@PathVariable Integer currentPage, @PathVariable Integer pageSize,@RequestBody Student student) {
QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
if (StringUtils.isNotBlank(student.getName())) {
queryWrapper.like("name", student.getName());
}
if (student.getCreateTimeBegin() != null && student.getCreateTimeEnd() != null ){
queryWrapper.between("create_time", student.getCreateTimeBegin(), student.getCreateTimeEnd());
}
queryWrapper.orderByDesc("create_time");
queryWrapper.eq("delete_flag",0);
if (student.getClassesId() !=null) {
queryWrapper.like("classes_id", student.getClassesId());
}
IPage<StudentVo> pageList = studentService.pageList(new Page<>(currentPage, pageSize), queryWrapper);
return ResultJson.ok(pageList);
}
@GetMapping("/findById/{id}")
public ResultJson findStudentById(@PathVariable Long id) {
Student student = studentService.getById(id);
if(student != null){
return ResultJson.ok(student);
}
return ResultJson.failure(ResultCode.BAD_REQUEST);
}
}
如index方法的请求路径就为
http://localhost:8080/v1/student/index
@PostMapping
类似的还有
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
相当于@RequestMapping(value = “/hello”,method = {RequestMethod.POST})
指定了请求的方式
@PathVariable
接收请求路径中占位符的值
@PathVariable("xxx")
通过 @PathVariable 可以将URL中占位符参数{xxx}绑定到处理器类的方法形参中@PathVariable(“xxx“)
@PostMapping("/list/{currentPage}/{pageSize}")
请求路径:http://localhost:8080/v1/student/index/1/10

@RequestBody
使用@RequestBody注解可以在后端接收前端请求体中传来的json字符串中数据
在后台可以通过一个对象来接收这些数据
GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。
如请求下面的方法
@PostMapping("/list/{currentPage}/{pageSize}")
public ResultJson index(@PathVariable Integer currentPage, @PathVariable Integer pageSize,@RequestBody Student student) {
System.out.println(student.getClassesId()+" "+student.getId()+" "+student.getName());
return ResultJson.ok(student);
}
student类
public class Student {
private Long id;
private String name;
private Long classesId;
}
在postman中发送请求

结果

@RequestParam
用于将指定的请求参数赋值给方法中的形参。
使用该注解从前端传递数据相当于在url后面通过?key1=value1&key2=value2传递参数
示例如下
@PostMapping("/delete")
public ResultJson delete(@RequestParam("ids") Long[] ids,@RequestParam("name") String name){
for (Long id : ids) {
System.out.println(id);
}
System.out.println(name);
return ResultJson.ok();
}
使用postman请求上面的方法

运行结果为

MybatisPlus相关注解
@TableName
映射数据库的表名
@TableName(value = "student")
public class Student {
private Integer id;
private String name;
private Integer age;
}
@TableId
设置主键映射,value 映射主键字段名
type 设置主键类型,主键的生成策略

INPUT 如果开发者没有手动赋值,则数据库通过自增的方式给主键赋值,如果开发者手动赋值,则存入该值。
AUTO 默认就是数据库自增,开发者无需赋值。
ASSIGN_ID MP 自动赋值,雪花算法。
ASSIGN_UUID 主键的数据类型必须是 String,自动生成 UUID 进行赋值
@TableId(type = IdType.AUTO)
private Long id;
@TableField
映射非主键字段,value 映射字段名
exist 表示是否为数据库字段 false,如果实体类中的成员变量在数据库中没有对应的字段,则可以使用 exist
@TableField(exist=false)
select 表示是否查询该字段
fill 表示是否自动填充,将对象存入数据库的时候,由 MyBatis Plus 自动给某些字段赋值,如在create_time、update_time等字段中
@TableField(fill = FieldFill.INSERT_UPDATE)//更新时填充
private LocalDateTime updateTime;
@TableField(fill = FieldFill.INSERT)//插入数据时填充
private LocalDateTime createTime;
@Param
mybatis注解,用于传递参数

其他
@SpringBootApplication
是启动注解,同时也具备扫描功能。在类上加上@SpringBootApplication注解
其扫包的范围为当前启动类同包,及其子包
@Scheduled
@Scheduled注解是Spring Boot提供的用于定时任务控制的注解,主要用于控制任务在某个指定时间执行,或者每隔一段时间执行。注意需要配合@EnableScheduling使用,在启动类上加上@EnableScheduling注解
通过配合cron表达式可以指定该注解下的方法在什么时间执行
cron是@Scheduled的一个参数,是一个字符串,以5个空格隔开,只允许6个域(注意不是7个,7个直接会报错),分别表示秒、分、时、日、月、周。

cron通配符

示例
@Scheduled(cron = “0 * * * 1 SAT”) //每年的1月的所有周六的所有0秒时间执行
@Scheduled(cron = “0 0 0 1 Jan ?”) //每年的1月的1日的0时0分0秒执行
本文详细介绍了SpringBoot中与前后端交互相关的注解,如@RestController、@ResponseBody、@RequestMapping等,并讲解了MybatisPlus的常用注解,如@TableName、@TableId、@TableField等。此外,还提及了@SpringBootApplication的启动作用和@Scheduled的定时任务配置。
1095

被折叠的 条评论
为什么被折叠?



