火狐浏览器关闭自动更新

jquery获取一个div中有多少个子div

  返回  

java构建菜单权限树形结构给前端

2021/8/20 13:56:45 浏览:

1.建个菜单权限表

CREATE TABLE `system_permission`  (
  `id` bigint(19) NOT NULL COMMENT 'id',
  `permission_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '菜单或按钮名称',
  `parent_id` bigint(20) NULL DEFAULT 0 COMMENT '父ID',
  `path` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '路由地址',
  `is_frame` tinyint(2) NULL DEFAULT 0 COMMENT '是否为外链(1是 0否)',
  `permission_type` tinyint(2) NULL DEFAULT NULL COMMENT '菜单类型(0目录 1菜单 2按钮)',
  `permission_status` tinyint(2) NULL DEFAULT 1 COMMENT '菜单状态(1正常 0停用)',
  `perms` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '权限标识',
  `remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '备注',
  `sort` int(4) NULL DEFAULT 0 COMMENT '显示顺序',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  `create_by` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `update_by` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `del_flag` tinyint(2) NULL DEFAULT 0,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '菜单权限表' ROW_FORMAT = Dynamic;

2.用代码递归

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 * @author chy
 * @since 2021-08-16
 */
@Service
public class SystemPermissionServiceImpl extends BaseServiceImpl<SystemPermissionMapper, SystemPermission> implements SystemPermissionService {

    @Autowired
    private SystemPermissionMapper systemPermissionMapper;

    @Override
    public List<SystemPermissionVO> getSystemPermissionList() {
        List<SystemPermissionVO> systemPermissionVOList =  systemPermissionMapper.selectPermissionList();
        // 构建前端需要的树结构
        systemPermissionVOList = buildMenuTree(systemPermissionVOList);
        return systemPermissionVOList;
    }
    
    /**
     * 构建前端所需要树结构
     *
     * @author chy
     * @date 2021/8/16
     */
    public List<SystemPermissionVO> buildMenuTree(List<SystemPermissionVO> permissionVOList) {
        if (CollectionUtils.isEmpty(permissionVOList)) {
            return null;
        }
        List<SystemPermissionVO> returnList = new ArrayList<>();
        List<Long> tempList = permissionVOList.stream().map(SystemPermissionVO::getId).collect(Collectors.toList());
        for (SystemPermissionVO systemPermissionVO : permissionVOList) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(systemPermissionVO.getParentId())) {
                recursionFn(permissionVOList, systemPermissionVO);
                returnList.add(systemPermissionVO);
            }
        }
        if (returnList.isEmpty()) {
            returnList = permissionVOList;
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @author chy
     * @date 2021/8/16
     */
    private void recursionFn(List<SystemPermissionVO> list, SystemPermissionVO t) {
        // 得到子节点列表
        List<SystemPermissionVO> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SystemPermissionVO tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     *
     * @author chy
     * @date 2021/8/16
     */
    private List<SystemPermissionVO> getChildList(List<SystemPermissionVO> list, SystemPermissionVO t) {
        List<SystemPermissionVO> permissionVOList = new ArrayList<SystemPermissionVO>();
        for (SystemPermissionVO n : list) {
            if (n.getParentId().longValue() == t.getId().longValue()) {
                permissionVOList.add(n);
            }
        }
        return permissionVOList;
    }

    /**
     * 判断是否有子节点
     *
     * @author chy
     * @date 2021/8/16
     */
    private boolean hasChild(List<SystemPermissionVO> list, SystemPermissionVO t) {
        return getChildList(list, t).size() > 0;
    }
}

3.SystemPermissionVO的字段

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * @author chy
 * @date 2021/8/16
 */
@Data
@ApiModel(value = "权限出参")
public class SystemPermissionVO implements Serializable {

    @ApiModelProperty(value = "权限id")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    @ApiModelProperty(value = "菜单或按钮名称")
    private String permissionName;

    @ApiModelProperty(value = "父ID")
    private Long parentId;

    @ApiModelProperty(value = "菜单类型(0目录 1菜单 2按钮)")
    private Integer permissionType;

    @ApiModelProperty(value = "显示顺序")
    private Integer sort;

    @ApiModelProperty(value = "路由地址 前端标识")
    private String path;

    @ApiModelProperty(value = "权限标识")
    private String perms;

    @ApiModelProperty(value = "是否拥有此权限 1有  0没有")
    private Integer isHave;

    @ApiModelProperty(value = "子菜单或按钮")
    private List<SystemPermissionVO> children;



}

联系我们

如果您对我们的服务有兴趣,请及时和我们联系!

服务热线:18288888888
座机:18288888888
传真:
邮箱:888888@qq.com
地址:郑州市文化路红专路93号