layui左侧菜单接口java实现:替代init.json
生活随笔
收集整理的這篇文章主要介紹了
layui左侧菜单接口java实现:替代init.json
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先是創建菜單表結構,我寫的寫的字段比較多,實際要實現,不需要這么多字段
? CREATE TABLE `menu` (`id` int(20) NOT NULL AUTO_INCREMENT,`pid` int(20) DEFAULT NULL COMMENT '父菜單ID。一級菜單為0',`title` varchar(255) DEFAULT NULL COMMENT '菜單標題',`icon` varchar(255) DEFAULT NULL COMMENT '圖標',`href` varchar(255) DEFAULT NULL COMMENT '菜單URL',`target` varchar(255) DEFAULT NULL COMMENT '打開層級',`spread` int(2) DEFAULT NULL COMMENT '是否默認展子菜單 0不展開 1展開',`permission` varchar(255) DEFAULT NULL COMMENT '授權標識。多個用逗號分隔,如:user:list,user:create',`type` int(255) DEFAULT NULL COMMENT '類型。0:目錄;1:菜單;2:按鈕;3:導航菜單',`status` int(2) DEFAULT NULL COMMENT '狀態 0:禁用;1:正常',`sort` int(50) DEFAULT NULL COMMENT '排序值 越小越靠前',`ifdelete` int(2) DEFAULT NULL COMMENT '假刪除 0:刪除;1:正常',`create_by` varchar(255) DEFAULT NULL,`create_time` datetime DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;?首先是Mapper.xml文件,執行的sql
<select id="selectAllMenu" resultType="java.util.HashMap">SELECTid,pid,title,icon,href,target,spread,sort FROMmenu WHEREifdelete = 1 ORDER BYsort</select>service層進行數據整理
import com.sws.dao.MenuDao; import com.sws.services.MenuService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.util.ArrayList; import java.util.List; import java.util.Map;/*** @author qushen* @date 2021/7/14 15:31*/ @Service public class MenuServicelmpl implements MenuService {@AutowiredMenuDao menuDao;@Overridepublic List<Map<String,Object>> selectAllMenu() {//獲取全部的菜單List<Map<String,Object>> Menu=menuDao.selectAllMenu();//創建一個list對象,用來存放轉換后的數據List<Map<String, Object>> allMenu=new ArrayList<Map<String, Object>>();for (Map<String, Object> map:Menu) {int spread = Integer.parseInt(map.get("spread").toString());if (spread==1){map.put("spread",true);}else {map.put("spread",false);}allMenu.add(map);}//新建一個List,存放根節點List<Map<String, Object>> rootMenu = new ArrayList<Map<String, Object>>();for (Map<String, Object> nav : allMenu) {String parentId = String.valueOf(nav.get("pid"));if(parentId.equals("0")){//父節點是0的,為根節點。rootMenu.add(nav);}}//為根菜單設置子菜單,getClild是遞歸調用的for (Map<String, Object> nav : rootMenu) {/* 獲取根節點下的所有子節點 使用getChild方法*/String id = String.valueOf(nav.get("id"));List<Map<String, Object>> childList = getChild(id, allMenu);nav.put("child", childList);}return rootMenu;}/*** 獲取子節點* @param id 父節點id* @param allMenu 所有菜單列表* @return 每個根節點下,所有子菜單列表*/private List<Map<String, Object>> getChild(String id,List<Map<String, Object>> allMenu){//子菜單List<Map<String, Object>> childList = new ArrayList<Map<String, Object>>();for (Map<String, Object> nav : allMenu) {// 遍歷所有節點,將所有菜單的父id與傳過來的根節點的id比較//相等說明:為該根節點的子節點。String parentId = String.valueOf(nav.get("pid"));if(id.equals(parentId)){childList.add(nav);}}//遞歸for (Map<String, Object> nav : childList) {String tempId = String.valueOf(nav.get("id"));//nav.setChildren(,getChild(tempId, allMenu));nav.put("child", getChild(tempId, allMenu));}//Collections.sort(childList,order());//排序//如果節點下沒有子節點,返回一個空List(遞歸退出)if(childList.size() == 0){return new ArrayList<Map<String, Object>>();}return childList;}}controller對home和logo進行自定義
@RequestMapping("/selectAllMenu")public Object selectAllMenu(){//獲取全部的菜單List<Map<String, Object>> menuInfo=menuService.selectAllMenu();Map homeInfo = new HashMap();homeInfo.put("title","首頁");homeInfo.put("href","page/welcome-1.html?t=1");Map logoInfo = new HashMap();logoInfo.put("title","LAYUI ADMIN");logoInfo.put("image","images/logo.png");logoInfo.put("href","");Map map = new HashMap();map.put("homeInfo",homeInfo);map.put("logoInfo",logoInfo);map.put("menuInfo",menuInfo);return map;}返回數據格式如下:
{"menuInfo": [{"icon": "fa fa-address-book","pid": 0,"id": 1,"sort": 1,"title": "系統管理","target": "_self","spread": true,"child": [{"icon": "fa fa-cubes","pid": 1,"id": 3,"href": "sys/menu/index","sort": 1,"title": "菜單管理","target": "_self","spread": false,"child": []},{"icon": "fa fa-user","pid": 1,"id": 2,"href": "sys/user/index","sort": 2,"title": "用戶管理","target": "_self","spread": true,"child": []}]}],"homeInfo": {"href": "page/welcome-1.html?t=1","title": "首頁"},"logoInfo": {"image": "images/logo.png","href": "","title": "LAYUI ADMIN"} }總結
以上是生活随笔為你收集整理的layui左侧菜单接口java实现:替代init.json的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 把握商机口号文案30句
- 下一篇: SQL查询数据库完整表结构(mysql)