`
bugyun
  • 浏览: 540864 次
社区版块
存档分类
最新评论

java 验证PO注解

    博客分类:
  • Java
 
阅读更多
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;

/**
 * <p>
 *     以下注解参数,只针对PO的校验
 *
 *     参考:http://jinnianshilongnian.iteye.com/blog/1733708
 *
 *      @NotNull : Collection, Map 和 Array 对象不能是 null, 但可以是空集(size = 0)。
 *      @NotEmpty : Collection, Map 和 Array 对象不能是 null 并且相关对象的 size 大于 0。
 *      @NotBlank : 不是 null 且去除两端空白字符后的长度(trimmed length)大于 0。
 *
 *      @Null  被注释的元素必须为null
 *      @NotNull  被注释的元素不能为null
 *      @AssertTrue  被注释的元素必须为true
 *      @AssertFalse  被注释的元素必须为false
 *      @Min(value)  被注释的元素必须是一个数字,其值必须大于等于指定的最小值
 *      @Max(value)  被注释的元素必须是一个数字,其值必须小于等于指定的最大值
 *      @DecimalMin(value)  被注释的元素必须是一个数字,其值必须大于等于指定的最小值
 *      @DecimalMax(value)  被注释的元素必须是一个数字,其值必须小于等于指定的最大值
 *      @Size(max,min)  被注解的元素必须在制定的范围(数据类型:String, Collection, Map and arrays)
 *      @Range(min=, max=) 被注释的元素必须在合适的范围内 (数据:BigDecimal, BigInteger, String, byte, short, int, long and 原始类型的包装类 )
 *      @Digits(integer,fraction)  被注释的元素必须是一个数字,其值必须在可接受的范围内
 *      @Past  被注释的元素必须是一个过去的日期
 *      @Future  被注释的元素必须是一个将来的日期
 *      @Pattern(value) 被注释的元素必须符合指定的正则表达式。
 *      @Email 被注释的元素必须是电子邮件地址
 *      @Length 被注释的字符串的大小必须在指定的范围内
 *      @NotEmpty  被注释的字符串必须非空
 *      @Range  被注释的元素必须在合适的范围内
 *
 *      @Valid 任何非原子类型,指定递归验证关联的对象;如用户对象中有个地址对象属性,如果想在验证用户对象时一起验证地址对象的话,在地址对象上加@Valid注解即可级联验证
 *      eg:
 *          手机号校验:@Pattern(regexp = "^1[3|4|5|7|8][0-9]\\d{4,8}$")
 * </p>
 *
 *
 * @author beyond
 * @since 2019-05-07
 */
@ApiModel("用户模型-po")
@TableName("t_user_base")
public class UserBase implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "用户id", example = "1001")
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @Pattern(regexp = "^[a-zA-Z_]\\w{4,19}$", message = "用户名必须以字母下划线开头,可由字母数字下划线组成")
    @NotEmpty()
    @ApiModelProperty(value = "用户昵称", example = "张三")
    private String userName;

    @Range(max = 200)
    @NotNull()
    @ApiModelProperty(value = "年龄", example = "16")
    private Integer age;

    @Email
    @ApiModelProperty(value = "邮箱|登录帐号", example = "root@gmail.com")
    private String email;

    @Pattern(regexp = "^1[3|4|5|6|7|8][0-9]\\d{4,8}$", message = "手机号码输入不合法")
    @ApiModelProperty(value = "手机号码", example = "15261404040")
    private String mobile;

    @Size(min = 6, max = 8)
    @ApiModelProperty(value = "密码", example = "root")
    private String password;

    @ApiModelProperty(value = "创建时间", example = "2018-06-29 09:17:54")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT")
    private Date createTime;

    @ApiModelProperty(value = "更新时间", example = "2018-06-29 09:17:54")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT")
    private Date updateTime;

    @ApiModelProperty(value = "1|有效,0|无效", example = "0")
    private Integer status;

//    @Valid
//    @NotEmpty(message = "集合不能为空")
//    private List<UserBase> list ;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics