Spring AOP实现Mapper层查询返回重新赋值

需求:

        针对查询返回的数据,在数据库层处理可能会影响到性能,在考虑性能及维护方便的情况下,采用AOP实现

1,自定义注解


import java.lang.annotation.*;

/**
 * 针对 mapper层返回值 按照一定规则进行特殊处理后返回
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MapperReturnData {
    /**
     * 指定执行规则的方法,默认方法为:transferReturnData
     * @return
     */
    String method() default "transferReturnData";
    Class<? extends MapperReturnDataInterface> operation();
}

2,定义公共业务处理接口

/**
 * 不同的业务场景 其 针对返回值 解析处理规则不同,须根据自身情况实现该接口
 * @param <T>
 */
public interface MapperReturnDataInterface {

    void transferReturnData(Object request);

}

3,编写AOP核心实现


import com.taia.yms.aop.reponse.inter.MapperReturnDataInterface;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;

/**
 * 针对 mapper层返参进行特殊处理
 */
@Component
@Aspect
public class MapperReturnDataAspect {
    private static final Logger log = LoggerFactory.getLogger(MapperReturnDataAspect.class);
    //定义pointcut签名
    @Pointcut("execution(* com.taia.yms.mapper.*.*(..)) && @annotation(com.taia.yms.aop.reponse.MapperReturnData)")
    private void pointCut() {
        //方法为空,仅做签名
    }

    //对切点方法进行前置增强,就是在调用切点方法前进行做一些必要的操作,这就成为增强
    @Around("pointCut()")
    public Object getRes(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取被拦截的方法签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取被拦截的方法
        Method method = signature.getMethod();
        // 获取返回值
        Object returnObj = joinPoint.proceed();
        MapperReturnData annotation = method.getAnnotation(MapperReturnData.class);
        // 查找并获取注解
        try{
            // 读取注解的属性
            Class<? extends MapperReturnDataInterface> operation = annotation.operation();
            MapperReturnDataInterface operationInstance = operation.getDeclaredConstructor().newInstance();
            String methoded = annotation.method();
            Method operationMethod = operation.getDeclaredMethod(methoded, Object.class);
            operationMethod.invoke(operationInstance,returnObj);
        }catch (Exception e){
            log.error("类[{}]的方法[{}]执行失败,报错:{}",annotation.operation().getName(),annotation.method(),e.getMessage());
        }
        return returnObj;
    }

}

4,编写业务处理实现


import com.taia.yms.aop.reponse.inter.MapperReturnDataInterface;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.stream.Collectors;


public class TechnologyGetDataTypeList implements MapperReturnDataInterface {
    @Override
    public void transferReturnData(Object request) {
        List<String> list = (List<String>) request;
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        List<String> results = list.stream().map(v -> v.substring(v.indexOf("_") + 1))
                .collect(Collectors.toList());
        list.clear();
        list.addAll(results);
    }
}

5,在mapper指定接口方法上使用

    @MapperReturnData(operation = TechnologyGetDataTypeList.class)
    List<String> getDataTypeList(List<String> columnList);

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

远方的、远方的、、、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值