乐趣区

关于java:从数据库获取数据转成json格式java后端

先上代码

private JSONArray getJsonArray(){QueryWrapper<KltSoftExamineEntity> kltSoftExamineQueryWrapper=new QueryWrapper<>();
        kltSoftExamineQueryWrapper.select("platform").groupBy("platform");
        List<KltSoftExamineEntity> platForms=kltSoftExamineMapper.selectList(kltSoftExamineQueryWrapper);
        JSONArray jsonArray=new JSONArray();
        JSONObject jsonPlatObject =new JSONObject();
        platForms.forEach(platForm->{jsonPlatObject.put("plateformCode",platForm.getPlatform());//1
            jsonPlatObject.put("plateformName","platformName");//2
            // 获取 serverList
            JSONArray jsonServerListArray=new JSONArray();
            QueryWrapper<KltSoftExamineEntity> kltSoftExamineQueryWrapper1=new QueryWrapper<>();
            kltSoftExamineQueryWrapper1.select("server_ip").groupBy("server_ip").eq("platform",platForm.getPlatform());
            List<KltSoftExamineEntity> serverIps=kltSoftExamineMapper.selectList(kltSoftExamineQueryWrapper1);
            JSONObject jsonServerObject =new JSONObject();
            AtomicInteger i= new AtomicInteger();
            serverIps.forEach(serverIp->{jsonServerObject.put("serverName","licens-server"+i);//2.1
                i.getAndIncrement();
                jsonServerObject.put("serverIp",serverIp.getServerIp());//2.2

                // 获取 softList
                JSONArray jsonSoftListArray=new JSONArray();
                QueryWrapper<KltSoftExamineEntity> kltSoftExamineQueryWrapper2=new QueryWrapper<>();
                kltSoftExamineQueryWrapper2.select("soft_name").groupBy("soft_name").eq("server_ip",serverIp.getServerIp());
                List<KltSoftExamineEntity> softNames=kltSoftExamineMapper.selectList(kltSoftExamineQueryWrapper2);
                JSONObject jsonSoftObject =new JSONObject();
                softNames.forEach(softName->{jsonSoftObject.put("softName",softName.getSoftName());//3.1

                    // 获取 featureList
                    JSONArray jsonFeatureListArray=new JSONArray();
                    QueryWrapper<KltSoftExamineEntity> kltSoftExamineQueryWrapper3=new QueryWrapper<>();
                    kltSoftExamineQueryWrapper3.select("feature_name").groupBy("feature_name").eq("soft_name",softName.getSoftName());
                    List<KltSoftExamineEntity> featureNames=kltSoftExamineMapper.selectList(kltSoftExamineQueryWrapper3);
                    featureNames.forEach(featureName->{JSONObject jsonFeatureObject =new JSONObject();
                        jsonFeatureObject.put("featureName",featureName.getFeatureName());//4.1
                        jsonFeatureListArray.add(jsonFeatureObject);
                    });
                    jsonSoftObject.put("featureList",jsonFeatureListArray);//3.2
                    jsonSoftListArray.add(jsonSoftObject);
                });
                jsonServerObject.put("softList",jsonSoftListArray);//2.3
                jsonServerListArray.add(jsonServerObject);

            });
            //serverList 是 JSONArray
            jsonPlatObject.put("serverList",jsonServerListArray);//3
            jsonArray.add(jsonPlatObject);
        });
        return jsonArray;
    }

上述对数据库的操作都是在一张表下面取的

思路

  1. 依据查问的字段再回表查问
  2. 为了避免查问的数据反复,用到了 groupby() 函数

全部都是本人手写的,尽管多层嵌套,办法笨笨的,然而好有成就感!

退出移动版