规范,api,接口
hibernate-validator官网: The Bean Validation reference implementation. - Hibernate Validator
是beanvalidation的最佳实现
bean:属性+setter/getter
一、非web环境下使用校验1.1 校验项目环境搭建
javax.validation
validation-api
2.0.1.Final
org.hibernate.validator
hibernate-validator
6.0.18.Final
org.apache.tomcat.embed
tomcat-embed-el
9.0.29
@Data
public class UserInfo {
// 标记接口,新增组
public interface Add{
}
public interface Update{
}
// 默认的组 : javax.validation.groups.Default
@Null(groups = {Add.class}) // 只适用于新增
@NotNull(groups = Update.class) // 只适用于修改
private Long id;
// @NotNull // 只校验不为null
// @NotEmpty // !=null&&!""
@NotBlank(message = "你的名字不能为空") // !=null&&!""&&!" "
private String name;
@NotNull
// @Min(1) @Max(800) // 闭区间,什么时候生效?!=null
// @Range(min = 1,max = 800) // 闭区间
@Min(value = 18,message = "年龄小于{value}岁,禁止进入")
private Integer age;
@NotBlank
@Email
private String email;
@Pattern(regexp = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$")
private String phone;
// @NotBlank // 看一个约束可以作用在哪些类型上,直接点进去看源码开头的注释就行了
@NotNull
@Past
private LocalDateTime birthDay;
@URL
private String personalPage;
@NotNull
@Valid // 被引用对象加@Valid注解才可以完成级联校验
private Grade grade;
@UserStatus
private Integer status;
}



