当前位置: 首页 > news >正文

微服务之间调用外键“翻译”的方法概述

写在前面的话:减少strean流操作,减少多层嵌套for循环。使用普通for循环和map的方式进行转换,

第一步查询数据

List<Student> findList = studentDao.findList(findMap);

第二步准备遍历和赋值

if(CollectionUtil.isNotEmpty(findList)){
            // 第一次遍历,取出所有待翻译的字段,避免重复使用steam流取值
            Set<String> courseSet = new HashSet<>(16);
            Set<String> schooldSet = new HashSet<>(16);
            Set<String> nativePlaceSet = new HashSet<>(16);
            for (Student student : findList) {
                // 课程id
                String courseId = student.getSourceId();
                if(StringUtil.isNotEmpty(courseId)){
                    courseSet.add(courseId);
                }
                // 学校id
                String schooldId = student.getSchoold();
                if(StringUtil.isNotEmpty(schooldId)){
                    schooldSet.add(schooldId);
                }
                // 籍贯id
                String nativePlaceId = student.getNativePlace();
                if(StringUtil.isNotEmpty(nativePlaceId)){
                    nativePlaceSet.add(nativePlaceId);
                }
            }
            // 查询课程信息、学校信息、籍贯信息,并转换成map
            Map<String,Object> findMap = new HashMap<>(16);
            findMap.put("courseSet",courseSet);
            findMap.put("schooldSet",schooldSet);
            findMap.put("nativePlaceSet",nativePlaceSet);
            List<Course> courseList = courseDao.findList(findMap);
            List<Schoold> schooldList = schooldDao.findList(findMap);
            List<NativePlace> nativePlaceList = nativePlaceDao.findList(findMap);
            // 转换成map
            Map<String,Course> courseMap = CollectionUtil.isEmpty(courseList) ? new HashMap<>(0) :
                    courseList.stream().collect(Collectors.toMap(Course::getId, course -> course));
            Map<String,Schoold> schooldMap = CollectionUtil.isEmpty(schooldList) ? new HashMap<>(0) :
                    courseList.stream().collect(Collectors.toMap(Schoold::getId, schoold -> schoold));
            Map<String,NativePlace> nativePlaceMap = CollectionUtil.isEmpty(nativePlaceList) ? new HashMap<>(0) :
                    courseList.stream().collect(Collectors.toMap(NativePlace::getId, nativePlace -> nativePlace));
            // 第二次遍历,填充翻译后的值
            for (Student student : findList) {
                // 课程id-->课程名称
                String courseId = student.getSourceId();
                if(StringUtil.isNotEmpty(courseId)){
                    student.setSourceName(courseMap.get(courseId).getName());
                }
                // 学校id-->学校名称
                String schooldId = student.getSchoold();
                if(StringUtil.isNotEmpty(schooldId)){
                    student.setSchooldName(schooldMap.get(schooldId).getName());
                }
                // 籍贯id-->籍贯名称
                String nativePlaceId = student.getNativePlace();
                if(StringUtil.isNotEmpty(nativePlaceId)){
                    student.setNativePlaceName(nativePlaceMap.get(nativePlaceId).getName());
                }
            }
        }

结语:仅遍历2次,减少了stream流取值。用转map的方式减少了多层for循环。

相关文章:

  • 47常用控件_QWidget的toolTip属性
  • 使用Conda创建环境存在问题
  • 力扣448.找到数组中所有消失的元素
  • 从繁琐到高效,2025年AI PPT工具选秒出PPT
  • Java性能调优2025:从JVM到Kubernetes的全链路优化策略
  • SpringWebFlux WebClient:非阻塞HTTP请求客户端
  • 1.认识C语言
  • Helm核心概念与常见操作介绍
  • 设计模式 --- 原型模式
  • 跨省发布抖音作品,IP属地会随之变化吗?
  • C++day9
  • 电阻的额定功率与额定电压详解
  • 【什么是动态链接?这里的动态是什么意思?链接了什么?】
  • 【人工智能】如何通过精准提示工程实现完美的珠宝首饰展示
  • 企业使用Excel开展数据分析限制和建议完整版
  • 基于大模型的非阵发性室性心动过速风险预测与诊疗方案研究报告
  • Android中Jetpack设计理念、核心组件 和 实际价值
  • PGA 简介
  • 单元测试原则之——不要过度模拟
  • 刮板机断链保护装置的运行机制
  • 天津哪里有做网站的/企业seo排名哪家好
  • 中国seo网站/故事式软文范例500字
  • 空壳网站清理通知/百度推广账号登录
  • 个人网站的名字/怎样做线上销售
  • 江门网站建设费用/出售网站平台
  • 游戏开发 网站开发/sem竞价是什么