java递归mysql生成树_java递归生成树结构的数据

@Data

@EqualsAndHashCode(callSuper =true)

@ApiModel(value = "AccountCaptionVo", description = "会计科目")

public class AccountCaptionVo extends BaseModel {

@ApiModelProperty(value ="会计科目名称",name = "captionName")

private String captionName;

@ApiModelProperty(value = "会计科目编码",name = "captionCode")

private String captionCode;

@ApiModelProperty(value = "父类编码",name = "parentId")

private Long parentId;

@ApiModelProperty(value = "系统科目",name = "systematicSubjects")

private String systematicSubjects;

@ApiModelProperty(value = "借贷方向",name = "lendingDirection")

private String lendingDirection;

@ApiModelProperty(value = "子集",name = "children")

private List children;

@ApiModelProperty(value = "科目名称助记码",name = "mnemonicCode")

private String mnemonicCode;

public static List listToTree(List list) {

//用递归找子。

List treeList = new ArrayList();

for (AccountCaptionVo tree : list) {

//根目录的parentId为-1

if (tree.getParentId() == -1 ) {

treeList.add(findChildren(tree, list));

}

}

return treeList;

}

private static AccountCaptionVo findChildren(AccountCaptionVo tree, List list) {

for (AccountCaptionVo node : list) {

if (node.getParentId().longValue() == tree.getId().longValue()) {

if (tree.getChildren() == null) {

tree.setChildren(new ArrayList());

}

tree.getChildren().add(findChildren(node, list));

}

}

return tree;

}

@GetMapping(path="")

@ApiOperation(value="获取所有会计科目",nickname="getAccountCaption")

public Iterable List(){

List listAccountCaption= capAccountRepository.selecttSql();

List listAccountCaptionVos=new ArrayList<>();

listAccountCaption.stream().forEach(x->

{

AccountCaptionVo AccountCaptionVo=new AccountCaptionVo();

try {

BeanUtils.copyProperties(AccountCaptionVo,x);

listAccountCaptionVos.add(AccountCaptionVo);

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

});

return listToTree(listAccountCaptionVos);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值