zoukankan      html  css  js  c++  java
  • 非WEB项目中引入Hibernate Validator

    前言:

    网上一些朋友分享了关于hibernate-validator的使用方法,但是不是缺少关联库信息,就是提供的参考代码中缺少自定类。

    希望我这一篇博客能够让你顺利的跑出预期的结果。

    如果有错,可以给我留言。

    英文好的朋友可以参考官网的getting started。

    http://hibernate.org/validator/documentation/getting-started/

    一、环境

    hibernate-validator库必须运行的JDK版本为1.6及以上。

    二、hibernate-validator库及依赖

    1 classmate-1.3.1.jar
    2 hibernate-validator-5.3.1.Final.jar
    3 javax.el-2.2.4.jar
    4 javax.el-api-2.2.4.jar
    5 jboss-logging-3.3.0.Final.jar
    6 validation-api-1.1.0.Final.jar

    三、假设我们构造了一个Car类

     1 public class Car {
     2 
     3     @NotNull
     4     private String manufacturer;
     5 
     6     @NotNull
     7     @Size(min = 2, max = 14)
     8     private String licensePlate;
     9 
    10     @Min(2)
    11     private int seatCount;
    12 
    13     public Car(String manufacturer, String licencePlate, int seatCount) {
    14         this.manufacturer = manufacturer;
    15         this.licensePlate = licencePlate;
    16         this.seatCount = seatCount;
    17     }
    18 
    19     public String getManufacturer() {
    20         return manufacturer;
    21     }
    22 
    23     public void setManufacturer(String manufacturer) {
    24         this.manufacturer = manufacturer;
    25     }
    26 
    27     public String getLicensePlate() {
    28         return licensePlate;
    29     }
    30 
    31     public void setLicensePlate(String licensePlate) {
    32         this.licensePlate = licensePlate;
    33     }
    34 
    35     public int getSeatCount() {
    36         return seatCount;
    37     }
    38 
    39     public void setSeatCount(int seatCount) {
    40         this.seatCount = seatCount;
    41     }
    42 
    43 }

    四、如何校验呢?我们看看这个测试类

     1 public class CarTest {
     2 
     3     public static void main(String[] args) {
     4         ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
     5         Validator validator = factory.getValidator();
     6         Car car = new Car(null, "苏A999999", 1);
     7 
     8         Set<ConstraintViolation<Car>> constraintViolations = validator.validate(car);
     9         for (ConstraintViolation<Car> constraintViolation : constraintViolations)
    10             System.out.println("错误:" + constraintViolation.getMessage());
    11     }
    12 }

    五、结果如何?

    1 十一月 08, 2016 10:31:00 下午 org.hibernate.validator.internal.util.Version <clinit>
    2 INFO: HV000001: Hibernate Validator 5.3.1.Final
    3 错误:不能为null
    4 错误:最小不能小于2

    六、真是简单易用!虽然这个库重复“发明”了轮子。

    七、你注意到了吗?结果是自动国际化了的!

  • 相关阅读:
    闲来无事研究研究.Net中的异步编程
    Sql Server 因为触发器问题导致数据库更新报错“在触发器执行过程中引发了错误,批处理已中止”的问题处理
    c# 连接Redis报错:WRONGTYPE Operation against a key holding the wrong kind of value:类型搞混弄出的错误
    VS2013 调试时出现“表达式计算器中发生内部错误”的问题解决办法
    WCF优化的几个常规思路
    UWP汉堡菜单
    C#注册系统全局快捷键
    CXF详细介绍
    hadoop默认3个核心配置文件说明
    在虚拟机配置hive
  • 原文地址:https://www.cnblogs.com/yoyotl/p/6045004.html
Copyright © 2011-2022 走看看