Java从数据库读取页面树形菜单

Posted Mr. Ma

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java从数据库读取页面树形菜单相关的知识,希望对你有一定的参考价值。

从数据库加载菜单及子菜单主要使用递归的方法,具体实现可看代码

首先封装一个菜单pojo

public class Menu {
    // 菜单id
    private String id;
    // 菜单名称
    private String name;
    // 父菜单id
    private String parentId;
    // 菜单url
    private String url;
    // 菜单图标
    private String icon;
    // 菜单顺序
    private int order;
    // 子菜单
    private List<Menu> childMenus;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getParentId() {
        return parentId;
    }

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

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public List<Menu> getChildMenus() {
        return childMenus;
    }

    public void setChildMenus(List<Menu> childMenus) {
        this.childMenus = childMenus;
    }
}

  然后写dao接口

package com.xfma.dao;

import com.xfma.pojo.Menu;

import java.util.List;

public interface MenuMapper {
    /**
     * 查找用户的菜单
     * @return
     */
    public List<Menu> queryMenuList();
}

  接着mybatis配置映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xfma.dao.MenuMapper" >

    <select id="queryMenuList" resultType="com.xfma.pojo.Menu">
        SELECT
            id,`name`,parent_id as parentId,url,icon,`order`
        FROM
            jrbac_menu ORDER BY `order` ASC
    </select>

</mapper>

  接下来是service接口

package com.xfma.service;

import com.xfma.pojo.Menu;

import java.util.List;

public interface IMenuService {
    public List<Menu> listMenu();
}

  service实现类

package com.xfma.service.impl;

import com.xfma.dao.MenuMapper;
import com.xfma.pojo.Menu;
import com.xfma.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuService implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    public List<Menu> listMenu() {
        List<Menu> rootMenus = menuMapper.queryMenuList();
        List<Menu> list = getMenuList(rootMenus);
        return list;
    }

    /**
     * 找出一级菜单和二级菜单
     * @param rootMenus
     * @return
     */
    private List<Menu> getMenuList(List<Menu> rootMenus){
        List<Menu> list = new ArrayList<Menu>();
        // 先找到所有的一级菜单
        for (int i=0;i<rootMenus.size();i++){
            Menu menu = rootMenus.get(i);
            //一级菜单  没有父节点
            if (StringUtils.isEmpty(menu.getParentId())){
                list.add(menu);
            }
        }

        //查找二级菜单
        /**
         * 利用递归找出所有子菜单
         */
        for (Menu menu: list) {
            menu.setChildMenus(getChild(menu.getId(),rootMenus));
        }

        return list;

    }

    /**
     * 找出子菜单
     * @param id
     * @param rootMenu
     * @return
     */
    private List<Menu> getChild(String id, List<Menu> rootMenu) {
        // 子菜单
        List<Menu> childList = new ArrayList<Menu>();

        for (Menu menu : rootMenu) {
            // 遍历所有节点,将父菜单id与传过来的id比较
            if (!StringUtils.isEmpty(menu.getParentId())) {
                if (menu.getParentId().equals(id)) {
                    childList.add(menu);
                }
            }
        }

        // 把子菜单的子菜单再循环一遍
        for (Menu menu : childList) {// 没有url子菜单还有子菜单
            if (StringUtils.isEmpty(menu.getUrl())) {
                // 递归
                menu.setChildMenus(getChild(menu.getId(), rootMenu));
            }
        }

        // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }

        return childList;
    }
}

  返回的结果是已经封装好的各个菜单和子菜单,只需前台遍历就可以了。

下面附上SQL脚本:

DROP TABLE IF EXISTS `jrbac_menu`;
CREATE TABLE `jrbac_menu` (
  `id` varchar(32) NOT NULL COMMENT ‘主键id,uuid32位‘,
  `name` varchar(64) NOT NULL COMMENT ‘登录用户名‘,
  `parent_id` varchar(32) default NULL COMMENT ‘父菜单id‘,
  `url` varchar(64) default NULL COMMENT ‘访问地址‘,
  `icon` varchar(32) default NULL COMMENT ‘菜单图标‘,
  `order` tinyint(4) default ‘0‘ COMMENT ‘菜单顺序‘,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=‘菜单表‘;

-- ----------------------------
-- Records of jrbac_menu
-- ----------------------------
INSERT INTO `jrbac_menu` VALUES (‘1‘, ‘Forms‘, null, ‘forms.html‘, ‘fa fa-edit‘, ‘0‘);
INSERT INTO `jrbac_menu` VALUES (‘2‘, ‘UI Elements‘, null, ‘‘, ‘fa fa-wrench‘, ‘1‘);
INSERT INTO `jrbac_menu` VALUES (‘3‘, ‘Buttons‘, ‘2‘, ‘buttons.html‘, ‘‘, ‘0‘);
INSERT INTO `jrbac_menu` VALUES (‘4‘, ‘Icons‘, ‘2‘, ‘icons.html‘, null, ‘1‘);
INSERT INTO `jrbac_menu` VALUES (‘5‘, ‘Multi-Level Dropdown‘, ‘‘, ‘‘, ‘fa fa-sitemap‘, ‘2‘);
INSERT INTO `jrbac_menu` VALUES (‘6‘, ‘Second Level Item‘, ‘5‘, ‘second.html‘, null, ‘0‘);
INSERT INTO `jrbac_menu` VALUES (‘7‘, ‘Third Level‘, ‘5‘, null, ‘‘, ‘1‘);
INSERT INTO `jrbac_menu` VALUES (‘8‘, ‘Third Level Item‘, ‘7‘, ‘third.html‘, null, ‘0‘);

  

以上是关于Java从数据库读取页面树形菜单的主要内容,如果未能解决你的问题,请参考以下文章

java如何实现二级树形菜单动态显示。要求加载页面时显示一级,点击一级菜单再去数据库查找出二级菜单

javascript实现动态加载树形菜单(tree),

尚筹网项目 五后台 菜单维护

PHP / MySQL 构建树形菜单

EasyUI创建异步树形菜单和动态添加tab页面

JSP实现树形菜单