java 组织机构树的拼装--递归算法

递归算法的应用-树形结构数据的拼装

树对象:

package com.qkkj.hardwaremgmt.database.resultmap;



import java.util.List;


import com.qkkj.hardwaremgmt.database.beans.SysMenu;


/**
 * 
 * @Title: SysMenuResultMap.java  
 * @Package com.qkkj.usrmgmt.database.resultmap  
 * @Description: TODO(用于显示主菜单树)  
 * @author wangfudong 
 * @date 2018年5月3日 下午5:30
 * @version V1.0
 */
public class SysMenuTreeResultMap extends SysMenu {

private static final long serialVersionUID = -4114812291607390605L;

//主键
private String menuId;

//父级菜单id
private String parentMenuId;

//排序
private Integer menuSort;

//菜单名称
    private String menuName;
    
    //菜单路径
    private String menuUrl;

//子菜单list
private List children;


public String getMenuName() {
return menuName;
}


public void setMenuName(String menuName) {
this.menuName = menuName;
}


public String getMenuUrl() {
return menuUrl;
}


public void setMenuUrl(String menuUrl) {
this.menuUrl = menuUrl;
}


public List getChildren() {
return children;
}


public void setChildren(List children) {
this.children = children;
}


public String getParentMenuId() {
return parentMenuId;
}


public void setParentMenuId(String parentMenuId) {
this.parentMenuId = parentMenuId;
}


public Integer getMenuSort() {
return menuSort;
}


public void setMenuSort(Integer menuSort) {
this.menuSort = menuSort;
}


public String getMenuId() {
return menuId;
}


public void setMenuId(String menuId) {
this.menuId = menuId;
}

}

递归拼装树:

package com.qkkj.hardwaremgmt.database.service.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qkkj.hardwaremgmt.database.beans.SysUser;
import com.qkkj.hardwaremgmt.database.mapper.SysMenuMapper;
import com.qkkj.hardwaremgmt.database.mapper.SysUserMapper;
import com.qkkj.hardwaremgmt.database.mapper.SysUserRoleMapper;
import com.qkkj.hardwaremgmt.database.resultmap.SysMenuTreeResultMap;
import com.qkkj.hardwaremgmt.database.service.IMenuService;
import com.qkkj.hardwaremgmt.framework.config.SysConstants;


@Service("menuService")
public class MenuServiceImpl implements IMenuService {
 
@Autowired
private SysUserRoleMapper sysUserRoleMapper;

@Autowired
private SysMenuMapper sysMenuMapper;

@Autowired
private SysUserMapper sysUserMapper;

@Autowired
private EhCacheManager ehCacheManager;


/**
* 根据用户权限集合、
* @author wangfd
* @param userId 用户表主键
*/
@Override
public List selectMenuTree(Map param) {

// 根据权限集合、应用id查询一级菜单目录
param.put("parentMenuId", "0");
param.put("userState", 0);
List resultMenuTree = sysMenuMapper.selectMenuTreeByPermissionIds(param);


// 节点为空直接返回、
if (null == resultMenuTree || resultMenuTree.size() == 0) {
return new ArrayList();
}


// 根据一级菜单目录、根据权限集合、应用id 层层查找子集放入到children属性中
resultMenuTree = setMenuChildren(param, resultMenuTree);


return resultMenuTree;
}

/**
* 根据用户id查询用户所有菜单权限返回list集合
* @author wangfd
* @param param 
*/
@Override
public List selectPermissionIds(Map param) {
return sysMenuMapper.selectPermissionIds(param);
}

/**
* 根据一级菜单目录、用户id、应用id 层层查找子集放入到children属性中(内调方法)
* @author wangfd
* @param userId 用户表主键、根节点主页菜单集合
*/
public List setMenuChildren(Map param,List resultMenuTree){

for (int i = 0; i < resultMenuTree.size(); i++) {
SysMenuTreeResultMap item = resultMenuTree.get(i);
String parentMenuId = item.getMenuId();
param.put("parentMenuId", parentMenuId);
List resultMenuChildren = sysMenuMapper.selectMenuTreeByPermissionIds(param);
if (null != resultMenuChildren && resultMenuChildren.size() > 0) {
// 递归
resultMenuChildren = setMenuChildren(param, resultMenuChildren);
// 设置子集
item.setChildren(resultMenuChildren);
resultMenuTree.set(i, item);
}
}


return resultMenuTree;
}
   
}

你可能感兴趣的:(java 组织机构树的拼装--递归算法)