数据库
/*
Navicat Premium Data Transfer
Source Server : 开发环境10.20.131.115-deployop
Source Server Type : MySQL
Source Server Version : 50622
Source Host : 10.20.131.115:5089
Source Schema : smt
Target Server Type : MySQL
Target Server Version : 50622
File Encoding : 65001
Date: 16/05/2018 19:49:52
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for smt_module_menu
-- ----------------------------
DROP TABLE IF EXISTS `smt_module_menu`;
CREATE TABLE `smt_module_menu` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`p_id` bigint(20) NOT NULL,
`name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Compact;
-- ----------------------------
-- Records of smt_module_menu
-- ----------------------------
INSERT INTO `smt_module_menu` VALUES (1, 0, '我的深圳');
INSERT INTO `smt_module_menu` VALUES (2, 0, '我的南通');
INSERT INTO `smt_module_menu` VALUES (3, 0, '宝安通');
INSERT INTO `smt_module_menu` VALUES (4, 1, '首页');
INSERT INTO `smt_module_menu` VALUES (5, 2, '首页');
INSERT INTO `smt_module_menu` VALUES (6, 3, '首页');
INSERT INTO `smt_module_menu` VALUES (7, 4, '常用服务');
INSERT INTO `smt_module_menu` VALUES (8, 7, '公积金');
INSERT INTO `smt_module_menu` VALUES (9, 7, '社保');
INSERT INTO `smt_module_menu` VALUES (10, 7, '住房服务');
SET FOREIGN_KEY_CHECKS = 1;
SmtModuleMenuDTO.java
package com.paic.smt.app.pcapi.configSystem.dto;
import java.util.List;
/**
* Created by HUANGLIAO322 on 2018/5/16.
*/
public class SmtModuleMenuDTO {
private long id;
private long parentId;
private String name;
private List<SmtModuleMenuDTO> childMenus;
public List<SmtModuleMenuDTO> getChildMenus() {
return childMenus;
}
public void setChildMenus(List<SmtModuleMenuDTO> childMenus) {
this.childMenus = childMenus;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getParentId() {
return parentId;
}
public void setParentId(long parentId) {
this.parentId = parentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.paic.smt.app.pcapi.configSystem.biz.service.impl;
import com.alibaba.fastjson.JSON;
import com.paic.smt.app.pcapi.configSystem.biz.dao.ConfigMenuPcDAO;
import com.paic.smt.app.pcapi.configSystem.biz.service.ConfigMenuPcService;
import com.paic.smt.app.pcapi.configSystem.dto.SmtModuleMenuDTO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by HUANGLIAO322 on 2018/5/16.
*/
@Service
public class ConfigMenuPcServiceImpl implements ConfigMenuPcService {
private static Logger logger = Logger.getLogger(ConfigMenuPcServiceImpl.class);
@Autowired
private ConfigMenuPcDAO configMenuPcDAO;
@Override
public List<SmtModuleMenuDTO> getMenu(final long rootId ) {
List<SmtModuleMenuDTO> allList = this.configMenuPcDAO.findList(null); //查询全部数据
List<SmtModuleMenuDTO> menuList = new ArrayList<SmtModuleMenuDTO>();
for(SmtModuleMenuDTO smtModuleMenuDTO: allList){
if(rootId == smtModuleMenuDTO.getParentId()) {
menuList.add(smtModuleMenuDTO);
}
}
logger.info(JSON.toJSONString(menuList));
for (SmtModuleMenuDTO smtModuleMenuDTO : menuList){
smtModuleMenuDTO.setChildMenus(this.getChild(smtModuleMenuDTO.getId(),allList));
}
logger.info(JSON.toJSONString(menuList));
return menuList;
}
private List<SmtModuleMenuDTO> getChild(long id, List<SmtModuleMenuDTO> allList) {
List<SmtModuleMenuDTO> childList = new ArrayList<SmtModuleMenuDTO>();
for (SmtModuleMenuDTO menu : allList) {
if (menu.getParentId()==id) {
childList.add(menu);
}
}
if (childList.size() == 0) {
return null;
}
for (SmtModuleMenuDTO menu : childList) {
menu.setChildMenus(this.getChild(menu.getId(), allList));
}
return childList;
}
}