1、双表合并查询
/*两表合并*/
public void te() {
/*全选*/
List<ModelEntity> modelEntities = modelDao.selectList(null);
List<ModelControlVo> modelControlVos = new ArrayList<>();
/*将查询得到的数据赋值给modelControlVos*/
modelControlVos = modelEntities.stream().map(modelEntity -> {
ModelControlVo modelControlVo = new ModelControlVo();
modelControlVo.setModelId(modelEntity.getModelId());
modelControlVo.setModelName(modelEntity.getModelName());
modelControlVo.setModelType(modelEntity.getModelType());
return modelControlVo;
}).collect(Collectors.toList());
/*获得所有的modelId并去重*/
Set<String> modelIdSet = modelEntities.stream().map(modelEntity -> {
return modelEntity.getModelId();
}).collect(toSet());
/*判断取出来的模型表有数据*/
if (modelIdSet.size() > 0) {
/*条件查询得到分数表的数据*/
LambdaQueryWrapper<JssChainModelScore> wrapper = Wrappers.lambdaQuery();
wrapper.in(JssChainModelScore::getModelId, modelIdSet);
/*如果只要某一个属性,可以select*/
/*wrapper.in(JssChainModelScore::getModelId, modelIdSet).select(JssChainModelScore::getModelId);*/
List<JssChainModelScore> jssChainModelScores = jssChainModelScoreDao.selectList(wrapper);
/*取出它的模型名和它本身作为map*/
Map<String, JssChainModelScore> jssChainModelScoreMap = jssChainModelScores.stream().collect(Collectors.toMap(JssChainModelScore::getModelId, p -> p));
/*遍历赋值*/
for (ModelControlVo modelControlVo : modelControlVos) {
JssChainModelScore jssChainModelScore = jssChainModelScoreMap.get(modelControlVo.getModelId());
if (jssChainModelScore!=null){
modelControlVo.setSynthesisAVGScore(jssChainModelScore.getSynthesisScore());
}
}
}
2、双表合并查询-分页
在查询合并后,加入分页
int pageSize = 2;
int pageNo=3;
int total = modelControlVos.size();
int pafeSum = (total+pageSize-1)/pageSize;
List<ModelControlVo> subList = modelControlVos.stream().skip((pageNo-1)*pageSize).limit(pageSize).collect(Collectors.toList());
3、一对一查询合并
/*单表合并*/
@Test
public void getOneMerge(){
int Id = 1;
LambdaQueryWrapper<CollectEntity> wrapper = Wrappers.lambdaQuery();
wrapper.eq(CollectEntity::getId,Id);
// 先查询用户信息
CollectEntity collectEntity = collectDao.selectOne(wrapper);
DetailEntity detailEntity = new DetailEntity();
detailEntity.setCollectId(collectEntity.getId());
System.out.println(detailEntity.toString());
}
标签:modelControlVos,双表,getModelId,查询,modelControlVo,wrapper,modelEntity,MP
From: https://www.cnblogs.com/brickMovingWorker/p/16968653.html