AOP:通过查询出来的数据,改变返回给前端包装类的一些属性的值

例子:数据库表没有字段createName(创建人),但是表里边有创建人的createid,我们返回给前端需要有创建人的名字
1.对应的实体类 ( TranslationStrategy.USERNAME为一个枚举类,Translation为自定注解,为了能够在定位到需要改变的是哪一个字段)
@Transient
@Translation(value = TranslationStrategy.USERNAME,name = "createid")
private String createName;

2.对应的TranslationStrategy枚举类、TranslationStrategy为具体处理对应需要改变某些字段赋值的枚举类代码略

3.对应的AOP,对应AOP是为了切带SelectSectionMethod注解的方法,当切到带SelectSectionMethod的方法是会进入到aop这个类,此方法类适用于返回值为:实体类、List<实体类>、List<Map<String,Object>>
@Component
@Aspect
public class ChangeName {
    @Pointcut("@annotation(com.bsoft.healthManagerWeb.aop.annotion.selectmethod.SelectSectionMethod)")
    public void ChangeName() {
    }

    @AfterReturning(value = "ChangeName()", returning = "methodResoult")
    public void implChangeName(JoinPoint joinPoint, Object methodResoult) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        if (null == methodResoult) {
            return;
        }
        if (methodResoult instanceof List) {
            List list = (List) methodResoult;
            if (IterUtil.isEmpty(list)) {
                return;
            }
            boolean isNotMap = true;
            if (list.get(0) instanceof Map) {
                isNotMap = false;
            }
            //list传过来有两种形式,一种是List<entity>,另一种是List<Map<String,Object>>
            if (isNotMap) {
                //
                list.forEach(entityOrMap -> {
                    entityChangeName(entityOrMap);
                });
            } else {
                list.forEach(entityOrMap -> {
                    mapChangeName(joinPoint, (Map) entityOrMap);
                });
            }
        } else {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            Class value1 = method.getAnnotation(SelectSectionMethod.class).value();
            String name = value1.getName();
            if (value1.isInstance(methodResoult)) {
                entityChangeName(methodResoult);
            }
        }
    }

    private void entityChangeName(Object entity) {
        //拿到返回结果
        //1.遍历集合拿到实体类,通过实体类找到对应的映射类
        //2.获取对应映射类的所有属性
        //3.拿到带注解的属性
        //4.遍历带注释的属性
        //得到某一个具体带注释的字段
        //拿到对应字段注解的value 拿到对应字段注解的name值
        //通过字节码class得到对应name对应字段
        //得到的字段进行实体类匹配得到对应的值
        //得到对应的值之后赋值给原有想要修改的值
        Class<?> aClass = entity.getClass();
        Field[] fields = aClass.getDeclaredFields();
        List<Field> fieldList = Stream.of(fields).filter(field -> {
            return field.isAnnotationPresent(Translation.class);
        }).collect(Collectors.toList());
        fieldList.stream().forEach(field -> {
            Translation annotation = field.getAnnotation(Translation.class);
            TranslationStrategy value = annotation.value();
            String name = annotation.name();
            Field declaredField = null;
            Object o = null;
            try {
                declaredField = aClass.getDeclaredField(name);
                declaredField.setAccessible(true);
                o = declaredField.get(entity);

            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }
            String apply = value.getFunction().apply(o);
            field.setAccessible(true);
            try {
                field.set(entity, apply);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });
    }

    //处理返回为List<Map<String,Object>>
    //步骤  获取方法签名,根据方法签名获取到方法,获取方法上注解(注解:SelectSectionMethod.class)的值(想要转化的类)
    //根据想要转化成的类(SelectSectionMethod.class 注解带的字段)获取想要转化的字段
    //获取类的字段
    //过滤出带注解的字段(过滤出带Translation.class的注解)
    //获取带注解字段的name、value
    //通过带注解(Translation.class)的value找到对应map的key((Translation.class)value对应(Map)key)通过key找到对应的具体值
    //通过得到的具体值放到map里 key(Translation.class标识字段)为字段名
    //注:要区分大小写故使用CaseInsensitiveMap 直接强转会出现转换异常
    private void mapChangeName(JoinPoint joinPoint, Map map) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        Class value = method.getAnnotation(SelectSectionMethod.class).value();
        Field[] declaredFields = value.getDeclaredFields();
        List<Field> collect = Stream.of(declaredFields).filter(
                filed -> filed.isAnnotationPresent(Translation.class)).collect(Collectors.toList());
        collect.stream().forEach(filed -> {
            Translation translation = filed.getAnnotation(Translation.class);
            String name = translation.name();
            TranslationStrategy value1 = translation.value();
            CaseInsensitiveMap caseInsensitiveMap = new CaseInsensitiveMap();
            BeanUtil.copyProperties(map, caseInsensitiveMap);
            Object o = caseInsensitiveMap.get(name);
            String apply = value1.getFunction().apply(o);
            map.put(filed.getName(), apply);
        });

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值