树的生成(Lambda表达式写法)
1.Controller:
//查询树
@PostMapping("/list")
public List<TbBasetree> list(@RequestBody Map<String,String> param) {
String mark = param.get("mark");
List<TbBasetree> personList = tbBasetreeService.getPersonList(mark);
return personList;
}
2.Service:
List<TbBasetree> getPersonList(String mark);
3.ServiceImpl:
在查询树的基础上添加需求:
1、根据mark查询树,该字段对应数据库的info_mark字段,0表示设备类型,1表示车站类型,且默认查询设备类型。
2、只有基础类被选中。
@Override
public List<TbBasetree> getPersonList(String mark) {
List<TbBasetree> allList = null;
if("0".equals(mark) || mark == null) {
allList = tbBasetreeMapper.getPersonList(mark);
}else if("1".equals(mark)) {
allList = tbBasetreeMapper.getPersonList2(mark);
}
//查询所有的数据
// List<TbBasetree> allList = tbBasetreeMapper.getPersonList(mark);
List<TbBasetree> finalAllList = allList;
return allList.stream().filter(item -> "0".equals(item.getParentId())).map(item -> {
item.setChildren(getChild(item.getId(), finalAllList, ""));
//虚拟字段spread为true时表示默认展开
item.setSpread("true");
//虚拟字段checked为true时表示默认选中
item.setChecked("true");
return item;
}).collect(Collectors.toList());
}
String baseId = "";
private List<TbBasetree> getChild(String id,List<TbBasetree> allList,String checkedPid) {
return allList.stream().filter(item -> id.equals(item.getParentId()
)).map(item -> {
if ("基础类".equals(item.getTitle())){
baseId = item.getId();
item.setChecked("true");
item.setChildren(getChild(item.getId(), allList,checkedPid));
}else if (baseId.equals(item.getParentId()) || (!"".equals(checkedPid) && checkedPid.equals(item.getParentId()))) {
item.setChecked("true");
item.setChildren(getChild(item.getId(), allList,item.getId()));
}else{
item.setChildren(getChild(item.getId(), allList,checkedPid));
}
return item;
}).collect(Collectors.toList());
}
旧版+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@Override
public List<TbBasetree> getTreeState() {
//查询所有的数据
List<TbBasetree> allList = tbBasetreeMapper.getTreeState();
//创建一个list集合,用于存放根节点
List<TbBasetree> parent = new ArrayList<>();
for (TbBasetree person : allList) {
//如果parentId为0,则代表是根节点,存入到集合中 person.setSpread(true); 自动展开
if (person.getParentId().equals("0")) {
//虚拟字段spread为true时表示默认展开
person.setSpread("true");
//虚拟字段checked为true时表示默认选中
person.setChecked("true");
parent.add(person);
}
}
//查找根节点下的子类,因为根节点的id就是子节点的parentId;
for (TbBasetree person : parent) {
List<TbBasetree> children = getChild2(person.getId(), allList);
person.setChildren(children);
}
return parent;
}
//查找子节点的方法
private List<TbBasetree> getChild2(String id, List<TbBasetree> allList) {
//存放子节点的集合
List<TbBasetree> children = new ArrayList<>();
for (TbBasetree person : allList) {
//如果根节点的id等于集合内parentId,说明是根节点的子节点
if (person.getParentId().equals(id)) {
//存入子节点集合
//虚拟字段checked为true时表示默认选中
person.setChecked("true");
children.add(person);
}
}
for (TbBasetree person : children) {
//递归调用,如果子节点存在根节点,则再次调用,往后依次推
List<TbBasetree> personChildren = getChild2(person.getId(), allList);
person.setChildren(personChildren);
}
return children;
}
旧版+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4.Mapper:
5.Mapper.xml:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示