1.先获取所有的菜单 在内存中获取子菜单
表结构如下(部分)
@ApiOperation("获取当前用户菜单")
@ApiImplicitParams({
})
@PostMapping("getMenuTreeByUserId")
public List<Map<String,Object>> getMenuTreeByUserId(){
Map<String,Object> param=new HashMap<String,Object>();
param.put("userid", UserUtils.getUser().getId());//获取用户id
List<Map<String,Object>> list=service.getMenuTreeByUserId(param);//获取全部的菜单
List<Map<String,Object>> menuTree= new ArrayList<Map<String,Object>>();
//获取顶级菜单
for(Map<String,Object> menu:list){
if(menu.get("parentid")==null || menu.get("parentid").equals("")){
menuTree.add(menu);
}
}
return childList(list,menuTree);
}
public List<Map<String,Object>> childList(List<Map<String,Object>> list,List<Map<String,Object>> menuTree) {
for(int i=0;i<menuTree.size();i++){
List<Map<String,Object>> childList=new ArrayList<Map<String,Object>>();
for(int j=0;j<list.size();j++){
if(menuTree.get(i).get("id")==list.get(j).get("parentid") || menuTree.get(i).get("id").equals(list.get(j).get("parentid"))){
childList.add(list.get(j));
}
}
if(childList.size()>0){
menuTree.get(i).put("childList", childList);
childList(list,childList);//调用本身 是否子菜单下还有子菜单
}
}
return menuTree;
}
select id as "id",target as "target",parent_id as "parentid",name as "name", href as "componentsName",sort as "currentIndex",icon as "icon" from sys_menu where id in
(select menu_id from sys_role_menu t where role_id in(select role_id from sys_user_role t where user_id='${userid}'))
and scope='BJSY-ZHGK' and is_show='1' and flag='1' and type='1' order by sort
1.1(适用于只有二级的菜单)
一、获取当前用户的全部菜单的sql
select
distinct m.*
from
SYS_USER_ROLE ur,
SYS_ROLE r,
SYS_ROLE_MENU rm,
SYS_MENU m
where 1=1
and ur.ROLE_ID = r.ID
and r.ID = rm.ROLE_ID
and rm.MENU_ID = m.ID
and m.FLAG = '1'
and ur.USER_ID = #{userId}
and m.SCOPE = #{scope}
order by m.SORT
二、将菜单转换成树形结构
public static List<Menu> tranferMenuListToTree(List<Menu> menuList){
List<Menu> result=new ArrayList<Menu>();
Map<String,Menu> menuMap=new HashMap<String,Menu>(menuList.size());
for(Menu item:menuList){
item.getChildList().clear();
menuMap.put(item.getId(), item);
}
for(Menu item:menuList){
if(menuMap.get(item.getParentId())!=null&&item.getId()!=item.getParentId()){
menuMap.get(item.getParentId()).addChild(item);
}else{
result.add(item);
}
}
Collections.sort(result, new Comparator<Menu>() {
@Override
public int compare(Menu o1, Menu o2) {
// TODO Auto-generated method stub
return o1.getSort()>o2.getSort()?1:o1.getSort()==o2.getSort()?0:-1;
}
});
System.out.println(JsonMapper.getInstance().toJson(result));
return result;
}
三、实体类
public class Menu implements Serializable{
private static final long serialVersionUID = 1L;
@Id
private String id;
private String parentId; // 父级菜单
private String parentIds; // 所有父级编号
private String name; // 名称
private String href; // 链接
private String target; // 目标( mainFrame、_blank、_self、_parent、_top)
private String icon; // 图标
private int sort; // 排序
private String isShow; // 是否在菜单中显示(1:显示;0:不显示)
private String permission; // 权限标识
private String iconUrl;
private String controlMark;
private String flag;
/**
* to control the system scope,config in ${app.scope} in application.properties
*/
private String scope;
@Transient
private List<Menu> childList=new ArrayList<Menu>();
public Menu(){
super();
this.sort = 30;
}
public Menu(String id, String parentId, String parentIds, String name, String href, Integer sort, String flag,String icon,String iconUrl) {
this.id = id;
this.parentId = parentId;
this.parentIds = parentIds;
this.name = name;
this.href = href;
this.sort = sort;
this.flag = flag;
this.icon=icon;
this.iconUrl=iconUrl;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getParentIds() {
return parentIds;
}
public void setParentIds(String parentIds) {
this.parentIds = parentIds;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
public String getIsShow() {
return isShow;
}
public void setIsShow(String isShow) {
this.isShow = isShow;
}
public String getPermission() {
return permission;
}
public void setPermission(String permission) {
this.permission = permission;
}
public String getIconUrl() {
return iconUrl;
}
public void setIconUrl(String iconUrl) {
this.iconUrl = iconUrl;
}
public String getControlMark() {
return controlMark;
}
public void setControlMark(String controlMark) {
this.controlMark = controlMark;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public List<Menu> getChildList() {
return childList;
}
public void setChildList(List<Menu> childList) {
this.childList = childList;
}
public void addChild(Menu child){
childList.add(child);
}
}
2.获取顶级菜单 然后从数据库获取子菜单
public List<Menu> getList(Object param) {
Map<String,Object> paramMap=(Map<String, Object>) param;
Example example=new Example(Menu.class);
Criteria cr = example.createCriteria();
if(!StringUtils.isBlank((String) paramMap.get("name"))){
cr.andLike("name", (String) paramMap.get("name"));
}
if(!StringUtils.isBlank((String) paramMap.get("flag"))){
cr.andEqualTo("flag", (String) paramMap.get("flag"));
}
cr.andCondition("PARENT_ID IS NULL");
example.setOrderByClause("sort");
List<Menu> selectByExample = getMapper().selectByExample(example);//获取顶级菜单
return mune(selectByExample);
}
public List<Menu> mune(List<Menu> selectByExample){
for(Menu menu:selectByExample) {
List<Menu> childList = getMapper().getChildList(menu.getId());//获取菜单下的子菜单
menu.setChildList(childList);
if(menu.getChildList().size()!=0) {
mune(menu.getChildList());//点用方法本身
}
}
return selectByExample;
}