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

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

第一步查询数据

java 复制代码
List<Student> findList = studentDao.findList(findMap);

第二步准备遍历和赋值

java 复制代码
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循环。

使用阿里的"通益千问",指定java17将代码优化如下:

java 复制代码
List<Student> findList = studentDao.findList(findMap);
if (CollectionUtil.isNotEmpty(findList)) {
    // 单次遍历收集所有ID(性能优化核心)
    Set<String> courseSet = new HashSet<>(16);
    Set<String> schoolSet = new HashSet<>(16);
    Set<String> nativePlaceSet = new HashSet<>(16);
    for (Student student : findList) {
        addIfNotEmpty(courseSet, student.getSourceId());
        addIfNotEmpty(schoolSet, student.getSchoold());
        addIfNotEmpty(nativePlaceSet, student.getNativePlace());
    }

    // 构建查询参数
    Map<String, Object> queryMap = new HashMap<>(16);
    queryMap.put("courseSet", courseSet);
    queryMap.put("schoolSet", schoolSet); // 修正拼写错误
    queryMap.put("nativePlaceSet", nativePlaceSet);

    // 查询关联数据
    List<Course> courseList = courseDao.findList(queryMap);
    List<School> schoolList = schoolDao.findList(queryMap); // 修正类名
    List<NativePlace> nativePlaceList = nativePlaceDao.findList(queryMap);

    // 转换为Map(使用辅助方法简化)
    Map<String, Course> courseMap = toMap(courseList, Course::getId);
    Map<String, School> schoolMap = toMap(schoolList, School::getId);
    Map<String, NativePlace> nativePlaceMap = toMap(nativePlaceList, NativePlace::getId);

    // 填充学生信息(使用Optional处理空值)
    for (Student student : findList) {
        // 课程ID → 课程名称
        String courseId = student.getSourceId();
        student.setSourceName(
            courseMap.getOrDefault(courseId, new Course()).getName()
        );

        // 学校ID → 学校名称
        String schoolId = student.getSchoold();
        student.setSchooldName(
            schoolMap.getOrDefault(schoolId, new School()).getName()
        );

        // 籍贯ID → 籍贯名称
        String nativePlaceId = student.getNativePlace();
        student.setNativePlaceName(
            nativePlaceMap.getOrDefault(nativePlaceId, new NativePlace()).getName()
        );
    }
}

// 辅助方法:简化字段添加逻辑
private void addIfNotEmpty(Set<String> set, String value) {
    if (StringUtil.isNotEmpty(value)) {
        set.add(value);
    }
}

// 辅助方法:将列表转换为Map(避免重复代码)
private <T> Map<String, T> toMap(List<T> list, Function<T, String> keyExtractor) {
    return list.stream()
        .collect(Collectors.toMap(keyExtractor, Function.identity(), 
            (existing, replacement) -> existing));
}
相关推荐
winfield8219 小时前
关于工程实践的面试问题
微服务·面试
元Y亨H13 小时前
【深度解析】Seata 分布式事务:核心作用、原理与实战配置指南
spring cloud·微服务
pengkai火火火15 小时前
基于springmvc拓展机制的高性能日志审计框架的设计与实现
spring boot·安全·微服务·架构
拾忆,想起15 小时前
设计模式:软件开发的可复用武功秘籍
开发语言·python·算法·微服务·设计模式·性能优化·服务发现
DKunYu16 小时前
5.优雅实现远程调用-OpenFeign
spring cloud·微服务
Coder_Boy_17 小时前
前端和后端软件系统联调经典问题汇总
java·前端·驱动开发·微服务·状态模式
音符犹如代码18 小时前
ZooKeeper 实战指南:从入门到场景解析
分布式·微服务·zookeeper·云原生·中间件·架构
Misnearch19 小时前
Mock服务是什么?
java·后端·微服务·mock
爱学习的小康19 小时前
angular MicroApp微服务改造
前端·微服务·angular.js