先上代码
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;
}
上述对数据库的操作都是在一张表上面取的
思路
- 根据查询的字段再回表查询
- 为了防止查询的数据重复,用到了groupby()函数
全部都是自己手写的,虽然多层嵌套,方法笨笨的,但是好有成就感!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。