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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
2023-05-21 excel给一列数据统一拼接某字符