目录
一、左侧树形菜单数据分析
由图可见pid为-1为父级菜单,父级菜单的id与子菜单的pid相对应
去layUI官网找到菜单,复制源码
二、获取数据库表中的所有菜单树
【实体类 Permission.java】
package com.chenchen.entity;
//数据库中t_oa_permission
public class Permission {
private long id;
private String name;//菜单名
private String description;//描述
private String url;//点击菜单跳转页面
private long pid;//父级菜单ID
private int ismenu;//菜单/按钮
private long displayno;//显示的顺序
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public long getPid() {
return pid;
}
public void setPid(long pid) {
this.pid = pid;
}
public int getIsmenu() {
return ismenu;
}
public void setIsmenu(int ismenu) {
this.ismenu = ismenu;
}
public long getDisplayno() {
return displayno;
}
public void setDisplayno(long displayno) {
this.displayno = displayno;
}
public Permission() {
// TODO Auto-generated constructor stub
}
public Permission(long id, String name, String description, String url, long pid, int ismenu, long displayno) {
this.id = id;
this.name = name;
this.description = description;
this.url = url;
this.pid = pid;
this.ismenu = ismenu;
this.displayno = displayno;
}
@Override
public String toString() {
return "Permission [id=" + id + ", name=" + name + ", description=" + description + ", url=" + url + ", pid="
+ pid + ", ismenu=" + ismenu + ", displayno=" + displayno + "]";
}
}
【PermissionDao.java】
package com.chenchen.dao;
import java.util.ArrayList;
import java.util.List;
import com.chenchen.entity.Permission;
import com.chenchen.util.BaseDao;
import com.chenchen.util.BuildTree;
import com.chenchen.util.PageBean;
import com.chenchen.util.TreeVo;
public class PermissionDao extends BaseDao<Permission>{
//查询t_oa_permission表中的数据
public List<Permission> list(Permission permission,PageBean pageBean) throws Exception{
String sql="select * from t_oa_permission";
return super.executeQuery(sql, Permission.class, pageBean);
}
public List<TreeVo<Permission>> menus(Permission permission,PageBean pageBean) throws Exception{
List<TreeVo<Permission>> trees=new ArrayList<TreeVo<Permission>>();
// 从数据库中拿到的菜单数据,此时数据是平级的,不具备父子关系
List<Permission> list=this.list(permission, pageBean);
for (Permission p : list) {
TreeVo<Permission> vo=new TreeVo<>();
vo.setId(p.getId()+"");
vo.setText(p.getName());//节点名称
vo.setParentId(p.getPid()+"");
trees.add(vo);
}
return Bu