关于无限递归的后台写法(java部分)

 案例以获取组织架构为例。组织下会存在下级组织,可无限阶层。思路很简单,首先获取所有组织结构,然后进行分组递归即可。只需要查询一次数据库。

  • 表结构

CREATE TABLE `t_sys_org` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '数据库自增',
  `parent_id` bigint(20) DEFAULT NULL COMMENT '数据库自增',
  `name` varchar(128) NOT NULL,
  `descs` varchar(255) DEFAULT NULL,
  `sn` smallint(6) DEFAULT NULL,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`),
  KEY `parent_id` (`parent_id`),
  CONSTRAINT `t_sys_org_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `t_sys_org` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;
  • 准备工作部分

 1.表对应的实体类
  
public class TSysOrg implements Serializable {
    private Long id;

    private Long parentId;

    private String name;

    private String descs;

    private Short sn;

    private Date createTime;

    private Date updateTime;
  
    private static final long serialVersionUID = 1L;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescs() {
        return descs;
    }

    public void setDescs(String descs) {
        this.descs = descs;
    }

    public Short getSn() {
        return sn;
    }

    public void setSn(Short sn) {
        this.sn = sn;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

}

 2.返回结果bo类

 

public class RspSysOrgTreesBO extends TSysOrg {


    //下级组织列表
    private List childOrgs;

    public List getChildOrgs() {
        return childOrgs;
    }

    public void setChildOrgs(List childOrgs) {
        this.childOrgs = childOrgs;
    }


}
  • 主体代码部分

 1.controller:
  
List orgs = this.sysOrgServiceImpl.getLeftTrees();
2.service:
入口方法:
public List getLeftTrees() throws Exception {
        //获取所有的记录
        List orgs = this.listAllSysOrg();
        if (null != orgs && orgs.size() > 0) {
            List allOrgs = new ArrayList();
            for (TSysOrg org : orgs) {
                RspSysOrgTreesBO bo = new RspSysOrgTreesBO();
                bo.setId(org.getId());
                bo.setName(org.getName());
                bo.setCreateTime(org.getCreateTime());
                bo.setDescs(org.getDescs());
                bo.setSn(org.getSn());
                bo.setParentId(org.getParentId());
                allOrgs.add(bo);
            }
            return this.getTreeOrgs(allOrgs);
        }
        return null;
    }
 获取所有组织架构数据

public List listAllSysOrg() throws Exception {
        return tSysOrgMapper.listAllOrgs();
    }

封装组织架构树状结构
public List getTreeOrgs(List allOrgs) throws Exception {
        List listParentRecord = new ArrayList();
        List listNotParentRecord = new ArrayList();
        // 第一步:遍历allOrgs找出所有的根节点和非根节点
        if (allOrgs != null && allOrgs.size() > 0) {
            for (RspSysOrgTreesBO org : allOrgs) {
                if (null == org.getParentId()) {
                    listParentRecord.add(org);
                } else {
                    listNotParentRecord.add(org);
                }
            }
        }
        // 第二步: 递归获取所有子节点
        if (listParentRecord.size() > 0) {
            for (RspSysOrgTreesBO record : listParentRecord) {
                // 添加所有子级
                record.setChildOrgs(this.getChildOrgs(listNotParentRecord, record.getId()));
            }
        }
        return listParentRecord;
    }

 递归查询子组织
public List getChildOrgs(List childList, Long parentId) throws Exception {
        List listParentOrgs = new ArrayList();
        List listNotParentOrgs = new ArrayList();
        // 遍历childList,找出所有的根节点和非根节点
        if (childList != null && childList.size() > 0) {
            for (RspSysOrgTreesBO record : childList) {
                // 对比找出父节点
                if (record.getParentId() == parentId) {
                    listParentOrgs.add(record);
                } else {
                    listNotParentOrgs.add(record);
                }

            }
        }
        // 查询子节点
        if (listParentOrgs.size() > 0) {
            for (RspSysOrgTreesBO record : listParentOrgs) {
                // 递归查询子节点
                record.setChildOrgs(getChildOrgs(listNotParentOrgs, record.getId()));
            }
        }
        return listParentOrgs;
    }

Mapper.xml文件:






你可能感兴趣的:(java常用技术)