多对多,根据用户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;
    }

张泽
5 声望1 粉丝