第一步 创建配置类,配置LocalValidatorFactoryBean

package com.atguigu.Validator2;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

@Configuration//配置类
@ComponentScan("com.atguigu.Validator2")//扫描规则
public class ValidationConfig {
    @Bean
    public LocalValidatorFactoryBean validator() {
        return new LocalValidatorFactoryBean();
    }

}

创建实体类,使用注解定义校验规则

package com.atguigu.Validator2;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;

public class User {
    @NotNull
    private String name;
    @Max(120)
    @Min(0)
    public  int age;
   /*
   常用注解说明
    @NotNull 限制必须不为null
    @NotEmpty 只作用于字符串类型,字符串不为空,并且长度不为0
    @NotBlank 只作用于字符串类型,字符串不为空,并且trim()后不为空串
    @DecimalMax(value) 限制必须为一个不大于指定值的数字
    @DecimalMin(value) 限制必须为一个不小于指定值的数字
    @Max(value) 限制必须为一个不大于指定值的数字
    @Min(value) 限制必须为一个不小于指定值的数字
    @Pattern(value) 限制必须符合指定的正则表达式
    @Size(max,min) 限制字符长度必须在min到max之间
    @Email 验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式
    */

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

使用两种不同的校验器实现

(1)使用jakarta.validation.Validator校验(注意 2个方法的Validator导包不同)

package com.atguigu.Validator2;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;
@Service
public class MyValidation1 {
    @Autowired
    //原生的 validator
    private Validator validator;
    public boolean validatorByOne(User user){
        Set<ConstraintViolation<User>> validate = validator.validate(user);
            return  validate.isEmpty();

    }
}

(2)使用org.springframework.validation.Validator校验

package com.atguigu.Validator2;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindException;
import org.springframework.validation.Validator;

import java.util.Set;

@Service
public class MyValidation2 {
    @Autowired
    //原生的 validator
    private Validator validator;
    public boolean validatorByTwo(User user){
        BindException bindException=new BindException(user,user.getName());
        validator.validate(user,bindException);
        return  bindException.hasErrors();

    }

}

测试类

 
package com.atguigu.Validator2;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestValidation {
    @Test
    public void TestOne(){
       User user =new User();
       user.setAge(20);
       user.setName("JOJO");
       ApplicationContext context=
                new AnnotationConfigApplicationContext(ValidationConfig.class);
        MyValidation1   validation1 = context.getBean(MyValidation1.class);
        boolean b = validation1.validatorByOne(user);
        System.out.println("validation1="+b);
      //这个方法校验不过不会报错 } @Test
public void TestTwo(){ User user =new User(); user.setAge(-20); user.setName("JOJO"); ApplicationContext context= new AnnotationConfigApplicationContext(ValidationConfig.class); MyValidation2 validation2 = context.getBean(MyValidation2.class); boolean b = validation2.validatorByTwo(user); System.out.println("validation1="+b); //这个方法如果是空会报异常 } }

 

package com.atguigu.Validator2;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestValidation {
@Test
public void TestOne(){
User user =new User();
user.setAge(20);
user.setName("JOJO");
ApplicationContext context=
new AnnotationConfigApplicationContext(ValidationConfig.class);
MyValidation1 validation1 = context.getBean(MyValidation1.class);
boolean b = validation1.validatorByOne(user);
System.out.println("validation1="+b);

}
@Test
public void TestTwo(){
User user =new User();
user.setAge(-20);
user.setName("JOJO");

ApplicationContext context=
new AnnotationConfigApplicationContext(ValidationConfig.class);
MyValidation2 validation2 = context.getBean(MyValidation2.class);
boolean b = validation2.validatorByTwo(user);
System.out.println("validation1="+b);
//这个方法如果是空会报异常
}

}