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


前言


一、业务场景?

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

二、实现步骤

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> {
    
}

四、参考博客

https://blog.csdn.net/weixin_44912855/article/details/122720171
https://juejin.cn/post/6844903950768930830#heading-14

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Java中,我们可以通过自定义注解来修改字段。首先,我们需要定义一个注解。可以使用@interface关键字来定义注解。例如,假设我们要定义一个名为@CustomAnnotation的注解,该注解用于修改字段。 ```java import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface CustomAnnotation { String value() default ""; } ``` 在注解内部,使用了@Retention和@Target元注解来指定注解的保留策略和注解的作用目标。在此例中,我们设置了@Retention(RetentionPolicy.RUNTIME),表示该注解在运行时可见。@Target(ElementType.FIELD)表示该注解可以用于字段上。 接下来,我们可以将@CustomAnnotation应用到字段上,并使用反射机制获取字段并修改其。 ```java import java.lang.reflect.Field; public class DemoClass { @CustomAnnotation("Hello World") private String value; public void setValueUsingAnnotation() throws IllegalAccessException { Field[] fields = this.getClass().getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(CustomAnnotation.class)) { CustomAnnotation annotation = field.getAnnotation(CustomAnnotation.class); field.setAccessible(true); field.set(this, annotation.value()); } } } public static void main(String[] args) throws IllegalAccessException { DemoClass demo = new DemoClass(); System.out.println("Before modification: " + demo.value); demo.setValueUsingAnnotation(); System.out.println("After modification: " + demo.value); } } ``` 在上述示例中,我们定义了一个名为DemoClass的类,并在其中声明了一个私有字段value。我们将@CustomAnnotation应用到该字段上,并在注解中设置了一个字符串。 在setValueUsingAnnotation方法中,使用反射机制获取类的所有字段。然后,检查每个字段是否应用了@CustomAnnotation注解。如果有,则使用Field类的setAccessible方法来设置字段可访问,并使用Field类的set方法将注解中的赋给字段。 在main方法中,我们创建了DemoClass的实例,并输出修改前后的字段。 通过运行上述代码,我们可以看到输出结果中,字段在应用自定义注解后发生了变化。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值