首先要清楚的是下边1,2,3; 且空格是有长度的:
1 @NotNull:不能为null,但可以为empty,没有Size的约束
2 @NotEmpty :不能为null,且Size>0
3 @NotBlank:只用于String,不能为null且trim()之后size>0
即三者都得不为null (没有参数传过来即为null ), @notNull 可以为空白或空格(长度不限); @NotEmpty 长度要大于0 (可以为空格,因为空格也有长度); @NotBlank 去掉空格之后要大于0;
这你就要说了, 那我想要自己定义的规则怎么办( 例:某个参数 不能为null,去掉空格之后长度大于4), 那就要使用正则表达式来实现:
@Pattern(regexp="^\\w{4}$",message="账号最少为四位") //正则表达式很强大,任何需求都可以自己定义,前提是要试着用她
@Entity@Table(name="b_user")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int uid;
//@NotEmpty(message="账号不能为null,且不能不填(但可以为空格)")
@NotBlank(message="账号不能为null,且不能不填(不可以全为空格)")
//@Size(min=4,message="账号最少为四位")
@Pattern(regexp="^\\w{4}$",message="账号最少为四位")
private String uname;
private String upass;
private String email;
}