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

需求:

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

1,自定义注解

java 复制代码
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,定义公共业务处理接口

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

    R transferReturnData(Object request);

}

3,编写AOP核心实现

java 复制代码
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);
            return operationMethod.invoke(operationInstance,returnObj);
        }catch (Exception e){
            log.error("类[{}]的方法[{}]执行失败,报错:{}",annotation.operation().getName(),annotation.method(),e.getMessage());
            return returnObj;
        }
    }

}

4,编写业务处理实现

java 复制代码
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


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

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

java 复制代码
    @MapperReturnData(operation = TechnologyGetDataTypeList.class)
    List<String> getDataTypeList(List<String> columnList);
相关推荐
wapicn992 分钟前
查看手机在线状态,保障设备安全运行
java·网络·数据库·python·php
浪淘沙jkp20 分钟前
智慧水务项目(八)基于Django 5.1 版本PyScada详细安装实战
后端·python·django·pyscada
wenbin_java22 分钟前
设计模式之备忘录模式:对象状态的可逆时光机
java·设计模式·备忘录模式
南雨北斗28 分钟前
3.laravel使用create-project 直接创建项目和使用全局安装程序创建项目的区别
后端
fengwuJ29 分钟前
Idea忽略已提交文件
java·ide·git·intellij-idea·ignore
斑驳的岁月43 分钟前
MacOs java环境配置+maven环境配置踩坑实录
java·macos·maven
瀚海澜生1 小时前
链表系列进阶攻略(三):拆解 LRU 与分割链表,突破链表解题瓶颈
后端·算法
严文文-Chris1 小时前
方法区、堆、虚拟机栈、寄存器分别存储哪些内容?为什么存储这些内容?
java·开发语言
qq_485015211 小时前
Java网络编程干货
java·网络·php
bobz9651 小时前
supervisord 的使用
后端