第一步:获取结果集
@Override
public Result getReltionList(String formId) {
if(StringUtils.isBlank(formId)){
return Result.error(ResponseMessage.PARAMERROR);
}
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(FormReltionship::getDeleteIf,true);
wrapper.eq(FormReltionship::getFormId,formId);
List shipList = formReltionshipMapper.selectList(wrapper);
// 以树型结构返回
List treeList = getTree("0",shipList);
return Result.success(treeList);
}
工具类:
public class FormUtils {
public static List getTree(String parentId,List shipList){
List formReltionshipList = new ArrayList<>();
// 获取父节点
List mainNodeList = getMainNode(parentId,shipList);
mainNodeList.stream().forEach(x->{
// 根据父节点下的子节点
x=childShipList(x,shipList);
formReltionshipList.add(x);
});
return formReltionshipList;
}
public static FormReltionship childShipList(FormReltionship ship,List shipList){
List childList = new ArrayList<>();
shipList.stream().forEach(x->{
// 通过parentId 进行比较
if(ship.getId().equals(x.getParentId())){
// 如果为子节点,则判断子节点下是否存在节点
childList.add(childShipList(x,shipList));
}
});
ship.setChildrenList(childList);
return ship;
}
public static List getMainNode(String parentId,List shipList){
List reltionshipList = new ArrayList<>();
shipList.stream().forEach(x->{
if(parentId.equals(x.getParentId())){
reltionshipList.add(x);
}
});
return reltionshipList;
}
}



