目录结构图:
实体结构:
private Long dirId;
private Long dirParentId;
需求背景:
我需要点击父级目录时查询所有该目录的数据+所有该目录下的所有子目录的数据(子目录有无穷多层)
调用方:
List<ZmgrDataColumnDir> zmgrDataColumnDirList = Lists.newArrayList();
List<ZmgrDataColumnDir> dirIds = dataColumnService.selectByParentDirId(zmgrDataColumnDirList, dirId);
List<Long> dirIdList = dirIds.stream().map(ZmgrDataColumnDir::getColumnDirId).collect(Collectors.toList());
递归——获取所有该父目录下的所有目录:
/**
* 递归——获取所有该父目录下的所有目录
* @param dirId
* @return
*/
public List<ZmgrDataColumnDir> selectByParentDirId(List<ZmgrDataColumnDir> zmgrDataColumnDirList, Long dirId) {
ZmgrDataColumnDir zmgrDataColumnDir = new ZmgrDataColumnDir();
zmgrDataColumnDir.setColumnDirId(dirId);
zmgrDataColumnDirList.add(zmgrDataColumnDir);
ZmgrDataColumnDirExample zmgrDataColumnDirExample = new ZmgrDataColumnDirExample();
zmgrDataColumnDirExample.createCriteria().andDirParentIdEqualTo(dirId);
List<ZmgrDataColumnDir> dirParentIdList = zmgrDataColumnDirMapper.selectByExample(zmgrDataColumnDirExample);
for(ZmgrDataColumnDir type : dirParentIdList) {
// 递归
selectByParentDirId(zmgrDataColumnDirList, type.getColumnDirId());
}
return zmgrDataColumnDirList;
}
测试:
(1)表目录 说明:父目录id是3101,他包括他的的所有子目录是3101、3103、3104、2105四个
(2) 代码debug
good !