java递归查询树形结构

domain

package com;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.unioncms.domain.interestGroup.TInterestGroupType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@TableName("t_name")
public class TName  implements Serializable {

    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 父id
     */
    @TableField("pid")
    private Long pid;

    /**
     * 名称
     */
    @TableField("name")
    private String name;

    /**
     * 排序
     */
    @TableField("order_num")
    private Integer orderNum;

    /**
     * 是否删除(0:否,1:是)
     */
    @TableField("disabled")
    private Integer disabled;

    /**
     * 创建人id
     */
    @TableField("create_id")
    private Long createId;

    /**
     * 创建人名称
     */
    @TableField("create_name")
    private String createName;

    /**
     * 创建时间
     */
    @TableField("create_time")
    private Date createTime;

    /**
     * 修改人id
     */
    @TableField("update_id")
    private Long updateId;

    /**
     * 修改人名称
     */
    @TableField("update_name")
    private String updateName;

    /**
     * 修改时间
     */
    @TableField("update_time")
    private Date updateTime;

    @TableField(exist = false)
    private List<TName> children;
}

serviceImpl代码,用的是mybatisPlus,直接上代码

public List<TName> getTypeTree() {
        List<TName> list = new ArrayList<>();
        LambdaQueryWrapper<TName> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TName::getDisabled,0).eq(TName::getPid,0);
        List<TName> tNames = tNameMapper.selectList(wrapper);
        if(ObjectUtils.isNotEmpty(tNames)){
            for(TName type : tNames){
                TName tName = new TName();
                BeanUtils.copyProperties(type, tName);
                tName.setChildren(getChildren(type.getId()));
                list.add(tName);
            }
        }
        return list;
    }

    private List<TName> getChildren(Long pid){
        List<TName> list = new ArrayList<>();
        LambdaQueryWrapper<TName> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TName::getDisabled,0).eq(TName::getPid,pid);
        List<TName> tNames = tNameMapper.selectList(wrapper);
        if(ObjectUtils.isNotEmpty(tNames)){
            for(TName type : tNames){
                TName tName = new TName();
                BeanUtils.copyProperties(type, tName);
                tName.setChildren(getChildren(type.getId()));
                list.add(tName);
            }
        }
        return list;
    }

你可能感兴趣的:(java,开发语言,mybatis)