自定义注解实现字段检验重复值

479 阅读3分钟

「时光不负,创作不停,本文正在参加2022年中总结征文大赛

一、业务场景?

对于一些数据库表字段是唯一索引的,需要在数据插入前做数据重复性检验,并友好返回给前端。例如用户名不能重复,工号不能重复等。对于这些重复判断项目中很多地方都需要用到,想法是直接抽离处理,统一管理,减少重复代码。按照以下步骤配置,字段值重复检验一个注解即可实现。以下是实现步骤,如有疑问,欢迎大家提问和指点。

二、实现步骤

1.引入依赖

代码如下(示例):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
    <version>2.3.11.RELEASE</version>
</dependency>

2.新增自定义注解(注解嵌套)

@FieldRepeatValidators

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = FieldRepeatValidatorHandler.class)
public @interface FieldRepeatValidators {

    /**
     * 数据表名
     * @return
     */
    String tableName();

    /**
     * 数据表id字段名
     * @return
     */
    String idField() default "id";
    
     /**
     * id字段的属性名称
     * @return
     */
    String idProperty() default "id";
    

    FieldRepeatValidator[] fieldRepeatValidators();

    String message() default "";
    
    Class<?>[] groups() default { };
    
    Class<? extends Payload>[] payload() default { };
}

@FieldRepeatValidator

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.TYPE})
public @interface FieldRepeatValidator {

    /**
     * 注解属性 - 对应校验字段,表字段
     * @return
     */
    String field();

    /**
     * 默认错误提示信息
     * @return
     */
    String message() default "字段内容重复!";

    /**
     *
     * @return 实体类属性
     */
    String property();

}

3. 自定义注解类处理器

public class FieldRepeatValidatorHandler implements ConstraintValidator<FieldRepeatValidators, Object>, ApplicationContextAware {

    private FieldRepeatValidators fieldRepeatValidators;

    static ApplicationContext application;

    @Override
    @SuppressWarnings("all")
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        application = applicationContext;
    }

    @Override
    public void initialize(FieldRepeatValidators fieldRepeatValidators) {
        this.fieldRepeatValidators = fieldRepeatValidators;
    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        return fieldRepeat(o,fieldRepeatValidators);
    }

    public static boolean fieldRepeat(Object object, FieldRepeatValidators fieldRepeatValidators) {
        JSONObject jsonObject = JSONUtil.parseObj(object);
        String table = fieldRepeatValidators.tableName();
        boolean isUpdate;
        String idField = fieldRepeatValidators.idField();
        String idProperty = fieldRepeatValidators.idProperty();
        isUpdate = jsonObject.containsKey(idProperty);
        Object id = jsonObject.get(idProperty);
        HashMap<String, Object> errors = new HashMap<>();
        for(FieldRepeatValidator fieldRepeatValidator : fieldRepeatValidators.fieldRepeatValidators()){
            String field = fieldRepeatValidator.field();
            String property = fieldRepeatValidator.property();
            String message = fieldRepeatValidator.message();
            Object o = jsonObject.get(property);
            if(Objects.isNull(o)){
                continue;
            }
    
            String sql = StrUtil.format("select count(1) 'count' from {} where {} = '{}'",table,field,o);
            if(isUpdate){
                sql = StrUtil.format(sql + " and {} = {}",idField,id);
            }
            JdbcTemplate jdbcTemplate = application.getBean("jdbcTemplate", JdbcTemplate.class);
             Map<String, Object> map = jdbcTemplate.queryForMap(sql);
            if(!map.get("count").equals(0L)){
                errors.put(property,message);
            }
        }
        if(errors.size() != 0){
            throw new FieldRepeatException(errors);
        }
        return true;
    }
}

4. 自定义异常类

@Data
public class FieldRepeatException extends ConstraintDeclarationException {

    /**
     *
     * @param cause
     */
    private HashMap<String,Object> errors;
    
    public FieldRepeatException(HashMap<String,Object> errors) {
        this.errors = errors;
    }
}

5. 全局异常捕获类

@RestControllerAdvice()
@Slf4j
public class GlobalControllerExceptionHandler {
    
    @ExceptionHandler(FieldRepeatException.class)
    private R fieldRepeatExceptionHandler(FieldRepeatException e) {
        return R.error("40001","数据检验失败").put("error",e.getErrors()));
    }
}

6. 实体类上的注解

@Data
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "User对象", description = "用户表")
@TableName("user")
@FieldRepeatValidators(tableName = "user",idField = "id",fieldRepeatValidators = {
        @FieldRepeatValidator(field = "user_name", property = "userName",message = "用户名重复!"),
        @FieldRepeatValidator(field = "mobile", property = "mobile",message = "电话号码重复!")
})
public class User extends Model<User> {
    
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
      /**
       * 用户名
       */
    @ApiModelProperty(value = "用户名")
    private String userName;
    
      /**
       * 电话
       */
    @ApiModelProperty(value = "电话")
    private String mobile;
    
}

7. Controller层

@PostMapping("/save")
@ApiOperation(value = "新增", notes = "传入user")
public R save(@RequestBody @Validated User user) {
    // 增加@Validated注解
   return R.data(userService.save(user));
}

结果显示

{
    "code":40001,
    "message":"数据检验失败",
    "errors":{
        "userName":"用户名重复!",
        "mobile":"电话号码重复!"    
    }
}

三、踩坑日记

问题: 检验完成,抛出自定义异常会出现: unexpected exception during isValid call

原因: 自定义异常由于继承的是RunTimeException,导致在检验处理类执行isValid()方法时没有正常返回值,而抛出了javax.validation.ValidationException。导致自定义异常无法被全局异常处理类捕获。

解决方法: 自定义异常需要继承ConstraintDeclarationException

public class FieldRepeatException extends ConstraintDeclarationException{}

问题: javax.validation.ConstraintDefinitionException: HV000074:containsbut does not contain a message parameter

原因: 自定义检验字段注解缺少message参数

解决方法: 添加message参数等

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = FieldRepeatValidatorHandler.class)
public @interface FieldRepeatValidators {
    
    String message() default "";
    Class<?>[] groups() default { };
    Class<? extends Payload>[] payload() default { };

}

问题: 自定义检验字段注解添加在字段上,只能拿到该字段的当前值。这样的话就不能获取到其他字段的值,其中最重要的是id字段的值。id字段值的作用是判断此次请求是新增还是修改,如果是新增,则只需要判断数据库中是否含有该字段值的记录。如果是修改,则需要判断除了自身id记录外是否还含有该字段值的记录。

解决方法: 自定义检验字段注解在类上,则可以获取到全部字段的值。再通过注解逐一对有需要做重复检验的字段进行检验,返回检验结果。

@FieldRepeatValidators(tableName = "user",idField = "id",fieldRepeatValidators = {
        @FieldRepeatValidator(field = "user_name", property = "userName",message = "用户名重复!"),
        @FieldRepeatValidator(field = "mobile", property = "mobile",message = "电话号码重复!")
})
public class User extends Model<User> {
    
}

四、参考博客

blog.csdn.net/weixin_4491… juejin.cn/post/684490…