Spring Boot 字段唯一校验
旭东怪 人气:01 Maven依赖
<!-- apache 工具包 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency>
2 实现代码
2.1 UniqueCheck
package com.xudongbase.uniquecheck.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 在指定的字段用此注解 * 用来唯一校验 * * @author xudongmaster */ @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface UniqueCheck { /** * 字段的中文名 * * @return */ String cnName(); /** * 提示 * * @return */ String tip() default "存在重复"; }
2.2 UniqueCheckDetail
package com.xudongbase.uniquecheck; import lombok.Builder; import lombok.Data; /** * 唯一校验明细信息 * * @author xudongmaster */ @Data @Builder public class UniqueCheckDetail { /** * 字段中文名 */ private String fileCnName; /** * 提示 */ private String tip; }
2.3 UniqueCheckArgs
package com.xudongbase.uniquecheck.args; /** * 唯一校验参数类 * * @author xudongmaster */ public interface UniqueCheckArgs { }
2.4 UniqueCheckService
package com.xudongbase.uniquecheck; import com.xudongbase.uniquecheck.args.UniqueCheckArgs; import java.util.List; /** * 唯一校验类 * * @author xudongmaster */ public interface UniqueCheckService { /** * 唯一校验 * * @param args * @return */ List<UniqueCheckDetail> uniqueCheck(UniqueCheckArgs args); }
2.5 AbstractUniqueCheckService
package com.xudongbase.uniquecheck; import cn.hutool.core.collection.CollUtil; import com.google.common.collect.Lists; import com.xudongbase.uniquecheck.annotation.UniqueCheck; import com.xudongbase.uniquecheck.args.UniqueCheckArgs; import org.apache.commons.lang3.reflect.FieldUtils; import java.lang.reflect.Field; import java.util.List; /** * 抽象唯一校验类 * * @author xudongmaster */ public abstract class AbstractUniqueCheckService<T> implements UniqueCheckService { /** * 唯一校验 * * @param args * @return */ @Override public List<UniqueCheckDetail> uniqueCheck(UniqueCheckArgs args) { //获取用来校验的数据 List<T> dataList = getCheckData(args); //进行唯一校验 return implementsUniqueCheck(dataList, args); } /** * 获取用来校验的数据 * * @param args 传参 * @return */ abstract List<T> getCheckData(UniqueCheckArgs args); /** * 需要实现的唯一校验 * * @param dataList 用来校验的数据 * @param args 传参 * @return */ abstract List<UniqueCheckDetail> implementsUniqueCheck(List<T> dataList, UniqueCheckArgs args); /** * 通用唯一校验方法 * * @param dataList 用来校验的数据 * @param needUniqueTarget 需要校验的对象 * @return */ public static <T> List<UniqueCheckDetail> commonUniqueCheck(List<T> dataList, T needUniqueTarget) { List<UniqueCheckDetail> uniqueCheckDetailList = Lists.newLinkedList(); if (CollUtil.isEmpty(dataList) || needUniqueTarget == null) { return uniqueCheckDetailList; } Class<?> clazz = needUniqueTarget.getClass(); //获取需要进行唯一校验的字段 List<Field> fieldList = FieldUtils.getFieldsListWithAnnotation(clazz, UniqueCheck.class); if (CollUtil.isEmpty(fieldList)) { return uniqueCheckDetailList; } List<Object> checkFieldValueList; Object needUniqueFieldValue; Object checkFieldValue; UniqueCheckDetail uniqueCheckDetail; UniqueCheck uniqueCheck; for (Field field : fieldList) { field.setAccessible(true); checkFieldValueList = Lists.newLinkedList(); needUniqueFieldValue = null; //获取需要校验的字段值 try { needUniqueFieldValue = field.get(needUniqueTarget); } catch (Exception e) { } if (needUniqueFieldValue == null) { continue; } Object filterNeedUniqueFieldValue = needUniqueFieldValue; //获取用来校验的对象的字段值 for (T data : dataList) { checkFieldValue = null; try { checkFieldValue = field.get(data); } catch (Exception e) { } if (checkFieldValue == null) { continue; } checkFieldValueList.add(checkFieldValue); } if (CollUtil.isEmpty(checkFieldValueList)) { continue; } //存在重复的字段值 if (checkFieldValueList.stream().filter(x -> x.equals(filterNeedUniqueFieldValue)).count() > 0) { uniqueCheck = field.getAnnotation(UniqueCheck.class); uniqueCheckDetail = UniqueCheckDetail.builder() .fileCnName(uniqueCheck.cnName()) .tip(uniqueCheck.cnName() + uniqueCheck.tip()).build(); uniqueCheckDetailList.add(uniqueCheckDetail); } } return uniqueCheckDetailList; } }
3 调试代码
3.1 UserCheckArgs
package com.xudongbase.uniquecheck.args; import com.xudongbase.uniquecheck.vo.UserUniqueCheckVo; import lombok.Data; /** * 用户唯一校验参数类 * * @author xudongmaster */ @Data public class UserCheckArgs implements UniqueCheckArgs { /** * 需要进行唯一校验的用户信息 */ private UserUniqueCheckVo needUniqueTarget; }
3.2 UserUniqueCheckVo
package com.xudongbase.uniquecheck.vo; import com.xudongbase.uniquecheck.annotation.UniqueCheck; import lombok.Data; /** * 用户唯一校验vo * * @author xudongmaster */ @Data public class UserUniqueCheckVo { @UniqueCheck(cnName = "用户id") private Long id; /** * 用户名 */ @UniqueCheck(cnName = "用户名") private String username; }
3.3 UserUniqueCheckService
package com.xudongbase.uniquecheck; import com.google.common.collect.Lists; import com.xudongbase.uniquecheck.args.UniqueCheckArgs; import com.xudongbase.uniquecheck.args.UserCheckArgs; import com.xudongbase.uniquecheck.vo.UserUniqueCheckVo; import org.springframework.stereotype.Service; import java.util.List; /** * 用户唯一校验类 * * @author xudongmaster */ @Service("UserUniqueCheckService") public class UserUniqueCheckService extends AbstractUniqueCheckService<UserUniqueCheckVo> { /** * 获取用来校验的数据 * * @param args 传参 * @return */ @Override List<UserUniqueCheckVo> getCheckData(UniqueCheckArgs args) { List<UserUniqueCheckVo> userUniqueCheckVoList=Lists.newLinkedList(); UserUniqueCheckVo userUniqueCheckVo=new UserUniqueCheckVo(); userUniqueCheckVo.setId(1L); userUniqueCheckVo.setUsername("xudongmaster"); userUniqueCheckVoList.add(userUniqueCheckVo); return userUniqueCheckVoList; } /** * 需要实现的唯一校验 * * @param dataList 用来校验的数据 * @param args 传参 * @return */ @Override List<UniqueCheckDetail> implementsUniqueCheck(List dataList, UniqueCheckArgs args) { UserCheckArgs userCheckArgs = (UserCheckArgs) args; UserUniqueCheckVo needUniqueTarget = userCheckArgs.getNeedUniqueTarget(); return commonUniqueCheck(dataList, needUniqueTarget); } }
3.4 UniqueCheckEnum
package com.xudongbase.uniquecheck.enums; import lombok.AllArgsConstructor; import lombok.Getter; /** * 唯一校验实例枚举类 * * @author Xudongmaster */ @Getter @AllArgsConstructor public enum UniqueCheckEnum { USER("UserUniqueCheckService", "用户唯一校验"); private final String value; private final String name; }
3.5 单元测试代码
package com.xudongbase.uniquecheck; import com.alibaba.fastjson.JSONObject; import com.google.gson.JsonObject; import com.xudongbase.uniquecheck.args.UserCheckArgs; import com.xudongbase.uniquecheck.enums.UniqueCheckEnum; import com.xudongbase.uniquecheck.vo.UserUniqueCheckVo; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; import java.util.Map; @SpringBootTest @Slf4j public class UniqueCheckTest { @Autowired private Map<String, UniqueCheckService> uniqueCheckServiceMap; /** * 测试用户唯一校验 */ @Test public void testUserUniqueCheck() { UserCheckArgs userCheckArgs = new UserCheckArgs(); UserUniqueCheckVo userUniqueCheckVo = new UserUniqueCheckVo(); userUniqueCheckVo.setId(2L); userUniqueCheckVo.setUsername("xudongmaster"); userCheckArgs.setNeedUniqueTarget(userUniqueCheckVo); List<UniqueCheckDetail> uniqueCheckDetailList = uniqueCheckServiceMap.get(UniqueCheckEnum.USER.getValue()).uniqueCheck(userCheckArgs); log.info(JSONObject.toJSONString(uniqueCheckDetailList)); } }
4 调试结果
注:
觉得这篇博客写的不错的可以前往Gitee/GitCode点个Star,源码请查看Gitee/GitCode的xudongbase项目unique_check分支。
加载全部内容