ssm中java实现树状结构_java ssm使用递归写树形结构
實體類:
private String dspId;
private String parentId;? //父類id
private String dataName;
private Integer sortingNumber;
private String remarks;
private Date createTime;
private Date updateTime;
private Date deleteTime;
private Integer dataFlag;
private Integer anId;
private Integer isParent;//是否有父類
private List chlidSpecies;
dao層和接口使用mybatis逆向生成,這里就不住解釋.
service層:
public List findDataspecies() {
// TODO Auto-generated method stub
//查詢所有的結果
DataSpeciesExample example = new DataSpeciesExample();
Criteria criteria = example.createCriteria();
criteria.andDataFlagEqualTo(0);
List dataSpecies = dataspeciesMapper.selectByExample(example);
//創建最后的結果? 想最后結果填充數據
List dataSpeciesList= new ArrayList<>();
for (DataSpecies species : dataSpecies) {
if(StringUtils.isBlank(species.getParentId()) ){
//一級菜單沒有ParentId
dataSpeciesList.add(species);
}
}
//為一級菜單設計子菜單? 使用getchild遞歸
for (DataSpecies daSpecies : dataSpeciesList) {
daSpecies.setChlidSpecies(getChild(daSpecies.getDspId(),dataSpecies));//這里getchild方法是遞歸
}
return dataSpeciesList;
}
//遞歸方法:
public List getChild(String id,List rootDataSpecies){
//創建子菜單
List speciesList = new ArrayList<>();
for (DataSpecies dataSpecies : rootDataSpecies) {
//遍歷蓑鲉節點 ,將父類id與傳過來的id進行對比
if(StringUtils.isNotBlank(dataSpecies.getParentId())){
if(dataSpecies.getParentId().equals(id)){
speciesList.add(dataSpecies);
}
}
}
//把子菜單循環遍歷
for (DataSpecies childSpecies : speciesList) {
if(childSpecies.getIsParent()==1){
childSpecies.setChlidSpecies(getChild(childSpecies.getDspId(),rootDataSpecies));
}
}
if(speciesList.size()==0){
return null;
}
return speciesList;
}
web層直接調用service 的方法就行,這里不做說明.
總結
以上是生活随笔為你收集整理的ssm中java实现树状结构_java ssm使用递归写树形结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java Hashtable rehas
- 下一篇: 如何在Java中使ArrayList只读