栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

aop编程 springboot 修改dao层所有方法的返回值

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

aop编程 springboot 修改dao层所有方法的返回值

文章目录
  • aop 切点表达式
  • 修改dao层所有方法的返回值
    • 问题场景
    • aop解决方案
  • 实现要点
    • 获取函数返回值,判断返回类型为List 或者 Map
    • concurrentModifyException Map的并发修改异常

aop 切点表达式

Spring AOP切点表达式-SpringBoot从入门到熟悉(十五)

修改dao层所有方法的返回值 问题场景
  • 项目数据源需要从Oracle转为mysql,因此要对mybatis mapper文件做修改
  • 但是mybatis 文件里写的sql语句有很多返回的数据库字段是小写英文,在oracle返回结果字段都是大写,而mysql返回字段包含小写英文,因此在使用mysql数据源的情况下,java代码获取的是大写英文字段,就会获取不到指定字段。

mapper文件:
select station_name,bay_name from stationTable;
java代码:
String stationName=stationBayList.get(i).get(“STATION_NAME”).toString(); (切换成mysql数据源会报错,因为返回的key 是 “station_name”)

aop解决方案
  • 切点为将dao层返回类型为 List(Map) 的所有函数
  • 实现将 List(Map) 或者 Map 中的key 全部转为大写
实现要点 获取函数返回值,判断返回类型为List 或者 Map

获取函数返回类型,用 instanceof 关键字判断

            //执行方法  获取方法执行结果
            object=proceedingJoinPoint.proceed();
            if(object instanceof List){
                List objectList=(List)object;
                //是 List类型
                if(objectList.size() > 0 && objectList.get(0) instanceof Map){
                    List> recordList=(ArrayList>)object;
                    recordList=changeKeyToUpperCaseList(recordList);

                    return recordList;
                }


            }else if(object instanceof Map){
                Map record=(Map)object;
                record=changeKeyToUpperCase(record);
                return record;

            }
concurrentModifyException Map的并发修改异常

转为 ConcurrentHashMap

        records=records.stream().map(map -> {
            ConcurrentHashMap concurrentHashMap=new ConcurrentHashMap(map);
            Map newMap=new HashMap<>();
            for(String key:concurrentHashMap.keySet()){
                newMap.put(key.toUpperCase(),concurrentHashMap.get(key));
            }
            return newMap;
        }).collect(Collectors.toList());
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.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;



@Aspect
@Component
public class DaoAspect {


    
    @Pointcut("execution(* com.dfe.e8800p.scheduler.dao.*.*(..))")
    public void daoPoint() {
//        log.info("analyse方法 为切点");

    }




//    // 前置通知:在目标方法执行之前执行
//    @Before(value = "excutePoint()")
//    public void befor(JoinPoint joinPoint) {
//        String name = joinPoint.getSignature().getName();
//        System.out.println(name + "方法, 执行 befor");
//    }
//
//    // 后置通知:在目标方法执行之后执行
//    @After(value = "excutePoint()")
//    public void after(JoinPoint joinPoint) {
//        String name = joinPoint.getSignature().getName();
//        System.out.println(name + "方法, 执行 after");
//    }
//
//    // 返回通知:获取目标方法的返回值
//    @AfterReturning(value = "excutePoint()", returning = "result")
//    public void afterReturning(JoinPoint joinPoint, Object result) {
//        String name = joinPoint.getSignature().getName();
//        System.out.println(name + "方法, 执行 " + "afterReturning" + ", 返回: " + result);
//    }
//
//    // 异常通知:当目标方法出现异常时,会执行该方法
//    @AfterThrowing(value = "excutePoint()", throwing = "e")
//    public void afterThrowing(JoinPoint joinPoint, Exception e) {
//        String name = joinPoint.getSignature().getName();
//        System.out.println(name + "方法, 执行 " + "afterThrowing" + ", 抛出异常: " + e);
//    }
//


    // 环绕通知:可实现任意通知,可调用 proceedingJoinPoint.proceed() 方法使目标方法继续执行

    
    @Around(value = "daoPoint()")
    public Object aroundSystemDeficiency(ProceedingJoinPoint proceedingJoinPoint){


        //类型转换,向下转型,必定成功,因为其内部的实现MethodSignatureImpl实现的就是MethodSignature接口
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        //获取method对象s
        Method method = signature.getMethod();
        //获取方法的返回值的类型
        Class  returnType=   method.getReturnType();

        
        Object object = null;

        try {
            //执行方法  获取方法执行结果
            object=proceedingJoinPoint.proceed();
            if(object instanceof List){
                List objectList=(List)object;
                //是 List类型
                if(objectList.size() > 0 && objectList.get(0) instanceof Map){
                    List> recordList=(ArrayList>)object;
                    recordList=changeKeyToUpperCaseList(recordList);

                    return recordList;
                }


            }else if(object instanceof Map){
                Map record=(Map)object;
                record=changeKeyToUpperCase(record);
                return record;

            }

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }



        return object;


    }

    //将所有key转为大写
    public List> changeKeyToUpperCaseList(List> records){
        records=records.stream().map(map -> {
            ConcurrentHashMap concurrentHashMap=new ConcurrentHashMap(map);
            Map newMap=new HashMap<>();
            for(String key:concurrentHashMap.keySet()){
                newMap.put(key.toUpperCase(),concurrentHashMap.get(key));
            }
            return newMap;
        }).collect(Collectors.toList());


        return records;
    }

    //将所有key转为大写
    public Map changeKeyToUpperCase(Map record){
        ConcurrentHashMap concurrentHashMap=new ConcurrentHashMap(record);
        Map newMap=new HashMap<>();
        for(String key:concurrentHashMap.keySet()){
            newMap.put(key.toUpperCase(),concurrentHashMap.get(key));
        }

        return newMap;
    }


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459974.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号