Java SpringBoot 實體類數據自動驗證


package demo.dto;

import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotEmpty;
import java.io.Serializable;

public class ProductDto implements Serializable {
    @NotEmpty(message = "姓名 不允許為空")
    @Length(min = 2, max = 10, message = "姓名 長度必須在 {min} - {max} 之間")
    private String userName;

    @NotEmpty(message = "密碼 不允許為空")
    private String password;

    @NotEmpty(message = "真實姓名 不允許為空")
    private String realName;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName == null ? null : userName.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName == null ? null : realName.trim();
    }
    /**
     *
     @NotEmpty,@NotNull和@NotBlank的區別
     1 @NotEmpty :不能為null,且Size>0

     2 @NotNull:不能為null,但可以為empty,沒有Size的約束

     3 @NotBlank:只用於String,不能為null且trim()之后size>0
     *
     @NotNull
     使用該注解的字段的值不能為null,否則驗證無法通過。

     @Null
     修飾的字段在驗證時必須是null,否則驗證無法通過。

     @Size
     如下代碼表示,修飾的字段長度不能超過5或者低於。

     @Size(min = 1, max = 5)
     private String name;
     1
     2
     @Max
     如下代碼表示,該字段的最大值為19,否則無法通過驗證。
     @Max(value = 19)
     private Integer age;
     1
     2
     @Min
     同理,被該注解修飾的字段的最小值,不能低於某個值。

     @AssertFalse
     該字段值為false時,驗證才能通過。

     @AssertTrue
     該字段值為true時,驗證才能通過。

     @DecimalMax
     驗證小數的最大值。

     @DecimalMax(value = "12.35")
     private double money;
     1
     2
     @DecimalMin
     驗證小數的最小值。

     @Digits
     驗證數字的整數位和小數位的位數是否超過指定的長度。

     @Digits(integer = 2, fraction = 2)
     private double money;
     1
     2
     @Future
     驗證日期是否在當前時間之后,否則無法通過校驗。
     @Future
     private Date date;
     1
     2
     @Past
     驗證日期是否在當前時間之前,否則無法通過校驗。

     @Pattern
     用於驗證字段是否與給定的正則相匹配。

     @Pattern(regexp = "[abc]")
     private String name;
     */

}

  

package demo.entity;

import java.io.Serializable;

public class Product implements Serializable {
    private Integer id;

    private String userName;

    private String password;

    private String realName;

    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 == null ? null : userName.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName == null ? null : realName.trim();
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", username='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", realname='" + realName + '\'' +
                '}';
    }
}
   //添加數據
    @RequestMapping("/addproduct")
    public Object addproduct(@Valid ProductDto model, BindingResult result) {
        int errorCount = result.getErrorCount();
        MessagePack messagePack = new MessagePack();
        // 驗證字段是否符合規則
        if (result.hasErrors()) {
            throw new RuntimeException(result.getFieldError().getDefaultMessage());
        } else {
            Product product = new Product();
            BeanUtils.copyProperties(model, product);
            // 操作數據
            int i = Convert.toInt(productService.addProduct(product));
            // 判斷操作成功與否
            if (i > 0) {
                messagePack.setCode(0);
                messagePack.setMessage("新增商品成功");
                messagePack.setObject(null);
                messagePack.setStatus("OK");
            } else {
                messagePack.setCode(-1);
                messagePack.setMessage("新增商品失敗");
                messagePack.setObject(null);
                messagePack.setStatus("error");
            }
        }
        return messagePack;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM