共计 1549 个字符,预计需要花费 4 分钟才能阅读完成。
多对多, 依据用户 id, 查问用户的多个角色, 同时查问每个角色下对应的菜单, 同时过滤出雷同的菜单
用户 <–> 角色, 角色 <–> 菜单, 多对多的关系
个别状况下, 菜单不过滤, 前端框架主动反复的数据会笼罩
办法一
@RequestMapping("getMenuByUserid")
public List<MenuRoleView> getMenuByUserid(HttpServletRequest request){int userid = (int) request.getSession().getAttribute("userid");
// 依据 userid 查问登录用户的角色信息
UserRoleViewExample userRoleViewExample=new UserRoleViewExample();
Criteria criteria2 = userRoleViewExample.createCriteria();
userRoleViewMapper.selectByExample(userRoleViewExample);
criteria2.andUseridEqualTo(userid);
List<UserRoleView> roleList = userRoleViewMapper.selectByExample(userRoleViewExample);
List<Integer> roleIds=new ArrayList<Integer>();// 角色 id 的汇合
for (UserRoleView userRoleView : roleList) {roleIds.add(userRoleView.getRoleid());
}
// 依据 userid 的汇合,查问
MenuRoleViewExample menuRoleViewExample=new MenuRoleViewExample();
com.bw.entity.MenuRoleViewExample.Criteria criteria = menuRoleViewExample.createCriteria();
criteria.andRoleidIn(roleIds);
List<MenuRoleView> menuList = menuRoleViewMapper.selectByExample(menuRoleViewExample);
return menuList;
}
办法二
@RequestMapping("getMenuByUserid2")
public List<MenuRoleView> getMenuByUserid2(HttpServletRequest request){Integer userid = (Integer) request.getSession().getAttribute("userid");
if(userid==null){return new ArrayList();
}
List<MenuRoleView> menuList = menuRoleViewMapper.getMenuByUserid2(userid);
// 工具类
HashMap<Integer, MenuRoleView> map=new HashMap<Integer, MenuRoleView>();
for (MenuRoleView menuRoleView : menuList) {map.put(menuRoleView.getMenuid(), menuRoleView);
}
Collection<MenuRoleView> values = map.values();
menuList=new ArrayList<MenuRoleView>(values);
return menuList;
}
正文完
发表至: springboot
2023-03-22