從數據庫加載菜單及子菜單主要使用遞歸的方法,具體實現可看代碼
首先封裝一個菜單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');
