create table sys_moudle(
id varchar(32),--每四个为一个层级 0001|00010001|000100010001 0002
levels int, --表示是第几层
name varchar(32),--菜单的名字
createdate date,--创建日期
modifydate date,--更改日期
adduser varchar(32),--创建用户
isshow char(1) check isshow in('0','1'),--该页面是否显示
url varchar(300),--页面路径相对于WEB-INFO
indexes int);
select * from sys_moudle order by id;
alter table sys_moudle add haschild char(1) check haschild in('0','1');--是否是叶子节点 0是叶子
alter table sys_moudle add parentid varchar(32); --父节点id
alter table sys_moudle add samecoluuid varchar(32);--同一列标识
-- drop table sys_moudle;
-- delete from sys_moudle where id is null;
INSERT INTO `sys_moudle` VALUES ('0001', 1, 'Home', '2016-4-11 00:00:00', '2016-4-11 00:00:00', 'ctl', '1', NULL, 100, '1', NULL, '0001');
INSERT INTO `sys_moudle` VALUES ('00010001', 2, 'HomeSub1', '2016-4-11 00:00:00', '2016-4-11 00:00:00', 'ctl', '1', NULL, 100101, '1', '0001', '0001');
INSERT INTO `sys_moudle` VALUES ('000100010001', 3, 'HomeSub1Sub1', '2016-4-11 00:00:00', '2016-4-11 00:00:00', 'ctl', '1', 'jsp/homesub1sub1.jsp', 100101101, '0', '00010001', '0001');
INSERT INTO `sys_moudle` VALUES ('00010002', 2, 'HomeSub2', '2016-4-11 00:00:00', '2016-4-11 00:00:00', 'ctl', '1', NULL, 100101, '1', '0001', '0001');
INSERT INTO `sys_moudle` VALUES ('000100020001', 3, 'HomeSub2Sub1', '2016-4-11 00:00:00', '2016-4-11 00:00:00', 'ctl', '1', 'jsp/homesub2sub1.jsp', 100101, '0', '00010002', '0001');
INSERT INTO `sys_moudle` VALUES ('000100020002', 3, 'HomeSub2Sub2', '2016-4-11 00:00:00', '2016-4-11 00:00:00', 'ctl', '1', 'jsp/homesub2sub2.jsp', 100101, '0', '00010002', '0001');
INSERT INTO `sys_moudle` VALUES ('0002', 1, 'Item2', '2016-4-12 00:00:00', '2016-4-12 00:00:00', 'ctl', '1', NULL, 200, '1', NULL, '0002');
INSERT INTO `sys_moudle` VALUES ('00020001', 2, 'Item2Sub1', '2016-4-12 00:00:00', '2016-4-12 00:00:00', 'ctl', '1', '', 200, '1', '0002', '0002');
INSERT INTO `sys_moudle` VALUES ('000200010001', 3, 'Item2Sub1Sub1', '2016-4-12 00:00:00', '2016-4-12 00:00:00', 'ctl', '1', 'jsp/item2sub1sub1.jsp', 200, '1', '00020001', '0002');
INSERT INTO `sys_moudle` VALUES ('0003', 1, 'Item3', '2016-4-12 00:00:00', '2016-4-12 00:00:00', 'ctl', '1', NULL, 300, '0', NULL, '0003');
package com.ctl.sys.po;
import java.util.Map;
/**@author guolin
* @date 2016-4-17上午10:00:18
* @package_name com.ctl.sys.po
* @project_name Aa权限
* @version version.1.0
*/
public class HtmlMenu {
private StringBuilder bufLevelOne=new StringBuilder();
private StringBuilder bufLevelTwo=new StringBuilder();
private StringBuilder bufLevelThird=new StringBuilder();
/**
* @param sysMoudle
* @param map map中存放的是该节点的所有的子节点
*/
public void setBufLevelOne(SysMoudle sysMoudle,Map map){
// if(bufLevelOne.length()==0){
// bufLevelOne.append("");
// }
if("0".equals(sysMoudle.getHaschild())){//如果没有子节点
bufLevelOne.append("- ").append(sysMoudle.getName()).append("
");
}else{//有子节点
bufLevelOne.append("- ").append(sysMoudle.getName()).append("").append(map.get(sysMoudle.getId()).getBufLevel2()).append("
");
}
}
// public StringBuilder getBufLevelOne(){
// return bufLevelOne.append("
");
// }
public StringBuilder getBufLevel1() {
return bufLevelOne;
}
// 节点二按parentsid生成,生成后存储到map中 map
//
//
// - Sub Item 2
//
//
// - Sub Item 2.1
// - Sub Item 2.2
//
//
//
// - Sub Item 3
//
//
public StringBuilder getBufLevelTwo() {
return bufLevelTwo.append("
List list=sysService.getAllSysMoudle();
Iterator iterator=list.iterator();
Map level1Map=new HashMap();
Map> level2Map=new HashMap>();
Map> level3Map=new HashMap>();
Map menu1Map=new HashMap();
Map menu2Map=new HashMap();
Map menu3Map=new HashMap();
while(iterator.hasNext()){
SysMoudle sysMoudle=iterator.next();
String id=sysMoudle.getId();
String parentId=sysMoudle.getParentid();
int level=sysMoudle.getLevels();
if(level==1){//一级菜单
level1Map.put(id, sysMoudle);
}
if(level==2){//所有的二级菜单
List listLeve2=(List) level2Map.get(parentId);
if(listLeve2==null){
listLeve2=new ArrayList();
listLeve2.add(sysMoudle);
level2Map.put(parentId, listLeve2);
}else{
listLeve2.add(sysMoudle);
}
}
if(level==3){//所有的三级菜单
List listLeve3=(List) level3Map.get(parentId);
if(listLeve3==null){
listLeve3=new ArrayList();
listLeve3.add(sysMoudle);
level3Map.put(parentId, listLeve3);
}else{
listLeve3.add(sysMoudle);
}
}
}
//处理所有的三级菜单生成html
Set level3Set=level3Map.keySet();
Iterator iterLevel3=level3Set.iterator();
while(iterLevel3.hasNext()){
String parentId=iterLevel3.next();
List listLevel3=level3Map.get(parentId);//相同父节点的三级菜单
HtmlMenu htmlMenu=null;
for(int i=0;i