1.数据库表
- 代码
2.1
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/** 字典树
*/
@Data
@Builder
@NoArgsConstructor
//@AllArgsConstructor
public class DictDataVo {
private int id;
private int idP;
private String dictLabel;
private String dictValue;
private String dictType;
private List childList;
public DictDataVo(int id, int idP, String dictLabel, String dictValue, String dictType){
this.id = id;
this.idP = idP;
this.dictLabel = dictLabel;
this.dictValue = dictValue;
}
public DictDataVo(int id, int idP, String dictLabel, String dictValue, String dictType, List<DictDataVo> childList){
this.id = id;
this.idP = idP;
this.dictLabel = dictLabel;
this.dictValue = dictValue;
this.childList = childList;
}
}
2.2 实现
/****
* 字典树
* @param dictType
* @return
*/
@Override
public List<DictDataVo> selectDictDataByTypeTree(String dictType) {
//DictDataVo dictDataVo = DictDataVo.builder().dictType("fz_yj_jcbz").build();
DictDataVo dictDataVo = new DictDataVo();
dictDataVo.setDictType("fz_yj_jcbz");
List<DictDataVo> dictDataVoList = dictDataMapper.selectDictTypeTreeList(dictDataVo);
//父节点
List<DictDataVo> collect = dictDataVoList.stream().filter(e -> e.getIdP()==0).map(
(e) -> {
e.setChildList(getChildrens(e, dictDataVoList));
return e;
}
).collect(Collectors.toList());
return collect;
}
/***
* 子节点
* @param root
* @param all
* @return
*/
public List<DictDataVo> getChildrens(DictDataVo root, List<DictDataVo> all) {
List<DictDataVo> childen = all.stream().filter(e -> {
return Objects.equals(e.getIdP(), root.getId());
}).map(
(e) -> {
e.setChildList(getChildrens(e, all));
return e;
}
).collect(Collectors.toList());
return childen;
}