1.Maven依赖
<!--导入相关的依赖-->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
2.@valid开启数据效验
使用范围:
@RequestMapping("/save")
// 把前端传入的JOSN数据封装到BrandEntity并对他开启数据效验
public R save(@Valid @RequestBody BrandEntity brand){
brandService.save(brand);
return R.ok();
}
3.对应实体类需要设定效验规则
@Data //开启Set.get.tostring方法
@TableName("pms_brand") //对应数据库实体类
public class BrandEntity implements Serializable {
@TableId //主键ID
private Long brandId;
@NotBlank //开启效验不能为空白
private String name;
@NotEmpty(message = "必须不为null且不为空")
private String logo;
private String descript;