mybatisplus简单生成树结构记录

1.Controller:

/**
* 区域树生成
* @return
*/
@GetMapping("/tree-list")
private Result regionTree() {
String stationCode = getStation().getStationTelecode();
List<StationRegionVo> tree = stationRegionListService.regionTree(stationCode);
return Result.ok(tree);
}

2.Service:

List<StationRegionVo> regionTree(String stationCode);

 

3.ServiceImpl:

@Override
public List<StationRegionVo> regionTree(String stationCode) {

// 1 查询全部数据
List<StationInfRelTree> stationInfRelTrees = stationRegionMapper.selectList(null);

// 2 查询所有一级节点
LambdaQueryWrapper<StationInfRelTree> wrapper = new LambdaQueryWrapper<StationInfRelTree>().eq(StationInfRelTree::getParentId, 1).eq(StationInfRelTree::getStationCode, stationCode);
List<StationInfRelTree> roots = stationRegionMapper.selectList(wrapper);

// 3 查询子节点
if (roots != null && !roots.isEmpty()) {
List<StationRegionVo> stationRegionRoots = new ArrayList<>();
roots.forEach(s -> {
StationRegionVo stationRegionVo = new StationRegionVo();
BeanUtils.copyProperties(s, stationRegionVo);
stationRegionRoots.add(stationRegionVo);
});
stationRegionRoots.forEach(r -> {
findChilds(r, stationInfRelTrees);
});
return stationRegionRoots;
}
return null;
}

// 添加子节点
private void findChilds (StationRegionVo regionVo, List<StationInfRelTree> all) {
List<StationRegionVo> children = new ArrayList<>();
all.forEach(s -> {
if (Objects.equals(s.getParentId(), regionVo.getRelaTreeId())) {
StationRegionVo stationRegionVo = new StationRegionVo();
BeanUtils.copyProperties(s, stationRegionVo);
children.add(stationRegionVo);
findChilds(stationRegionVo, all);
}
regionVo.setChildren(children);
});
}

4.mapper:

public interface StationRegionMapper extends BaseMapper<StationInfRelTree> {
}

5.Entity:

@Data
@TableName(value = "XXX")
public class StationInfRelTree implements Serializable {

private static final long serialVersionUID = 1L;

@TableId(value = "id")
private String id;

@TableField("rela_tree_id")
private String relaTreeId;

@TableField("parent_id")
private String parentId;
}

5.Vo:

@Data
public class StationRegionVo implements Serializable {

private String id;

private String relaTreeId;

private String parentId;

...

private List<StationRegionVo> children;
}
posted @   sensen~||^_^|||&  阅读(81)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
历史上的今天:
2023-05-21 excel给一列数据统一拼接某字符
点击右上角即可分享
微信分享提示