实体类
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class TroubleshootingLabel {
/**
*
*/
private Integer id;
/**
* 名称
*/
private String title;
/**
* 父级id
*/
private Integer superId;
/**
* 几级分类
*/
private Integer type;
省略set get .......
}
树形结构
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName TroubleshootingLabelTree
* @Author qiuyahui
* @Data 2019/4/26 0026 17:10
* @Version 1.0
**/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TroubleshootingLabelTree {
private Integer id;
/**
* 名称
*/
private String title;
/**
* 父级id
*/
private Integer superId;
/**
* 几级分类
*/
private Integer type;
private List<TroubleshootingLabelTree> child = new ArrayList<>();
/**
* 类转换,基本属性复制
* @param
* @return
*/
public static TroubleshootingLabelTree covert(TroubleshootingLabel troubleshootingLabel) {
TroubleshootingLabelTree treeTargetDto = new TroubleshootingLabelTree();
treeTargetDto.id = troubleshootingLabel.getId();
treeTargetDto.title = troubleshootingLabel.getTitle();
treeTargetDto.type = troubleshootingLabel.getType();
treeTargetDto.superId=troubleshootingLabel.getSuperId();
return treeTargetDto;
}
省略set get .......
}
ServiceImpl
import com.zzu.modules.security.dao.TroubleshootingLabelMapper;
import com.zzu.modules.security.entity.TroubleshootingLabel;
import com.zzu.modules.security.entity.TroubleshootingLabelTree;
import com.zzu.modules.security.service.TroubleshootingLabelService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
/**
* @ClassName TroubleshootingLabelServiceImpl
* @Author qiuyahui
* @Data 2019/4/23 0023 14:44
* @Version 1.0
**/
@Service
public class TroubleshootingLabelServiceImpl implements TroubleshootingLabelService {
@Resource
private TroubleshootingLabelMapper troubleshootingLabelMapper;
@Override
public TroubleshootingLabelTree selectByPrimaryKey() {
ArrayList<TroubleshootingLabel> list=troubleshootingLabelMapper.selectByPrimaryKey();
TroubleshootingLabelTree tree = TroubleshootingLabelServiceImpl.getTree(list);
return tree;
}
public static TroubleshootingLabelTree getTree(ArrayList<TroubleshootingLabel> list){
HashMap<Integer, TroubleshootingLabelTree> map = new HashMap<>();
//新建根节点
TroubleshootingLabelTree rootNode = new TroubleshootingLabelTree();
map.put(0, rootNode);
for (TroubleshootingLabel treeSource : list) {
TroubleshootingLabelTree childNode = TroubleshootingLabelTree.covert(treeSource);
map.put(treeSource.getId(), childNode);
//父节点id
Integer pId = treeSource.getSuperId();
//父节点
TroubleshootingLabelTree parentNode = map.get(pId);
//给父节点的child属性赋当前节点
parentNode.getChild().add(childNode);
}
return map.get(0);
}
}
自己开发的出门必备小程序可以扫码体验交流交流