管理后台菜单查询

Posted gjq1126-web

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了管理后台菜单查询相关的知识,希望对你有一定的参考价值。

本查询用于Mybatis+Spring的B/S架构

1.创建实体类与数据库的字段进行映射

public class PriMenu implements Serializable 
	private Long id;
	private Long pId; // 父节点id
	private String menuName;// 菜单名称
	private String menuDesc;// 描述
	private String menuUrl;// 访问地址
	private Long menuOrder;// 菜单顺序
	private String icon;// 图标地址
	private String priLevel;// 菜单层级
	private String priPath;// 权限菜单层级路径
	private String status; // 状态 0可用 1禁用
	private Long creatorId; // 创建人id
	private List<PriMenu> sonList;

2.需要对数据库查询

1)通过角色ID和去数据库查询到所有的权限父菜单(一般都是要表关联 这就看各自的表设计了)

2)通过角色ID和权限父菜单ID去查询所属的子菜单一起封装到PriMenu类里面

<resultMap id="MenuResultMap" type="com.p2p.pri.domain.PriMenu">
        <result property="id" column="ID" />
        <result property="pId" column="P_ID" />
        <result property="menuName" column="MENU_NAME" />
        <result property="menuDesc" column="MENU_DESC" />
        <result property="menuUrl" column="MENU_URL" />
        <result property="menuOrder" column="MENU_ORDER" />
        <result property="icon" column="ICON" />
        <result property="priLevel" column="PRI_LEVEL" />
        <result property="priPath" column="PRI_PATH" />
        <result property="status" column="STATUS" />
        <result property="creatorId" column="CREATOR_ID" />
        <association property="sonList" column="pId=id,userId=userId" select="querySonMenuByUserId"></association>
</resultMap>
    <select id="queryMenuByUserId" parameterType="map" resultMap="MenuResultMap">
        SELECT t1.*
        <if test="userId != null and userId != ‘‘">
            ,#userId as userId
        </if>
         FROM T_PRI_MENU t1
        LEFT JOIN T_PRI_ROLE_MENU t2 ON t1.ID = t2.MENU_ID
        LEFT JOIN T_PRI_USER_ROLE t3 ON t2.ROLE_ID = t3.ROLE_ID
        WHERE
         t3.USER_ID = #userId
        <if test="pId != null and pId != ‘‘">
            <if test="pId != ‘-1‘">
                AND t1.P_ID = #pId
            </if>
        </if>
        <if test="pId == null">
            AND (ISNULL(t1.P_ID) OR t1.P_ID = 0)
        </if>
        AND t1.STATUS = ‘0‘
        AND t1.IS_DELETE = ‘0‘
        ORDER BY t1.MENU_ORDER ASC, t1.ID DESC
    </select>
    <select id="querySonMenuByUserId" parameterType="map" resultMap="BaseResultMap">
        SELECT t1.* FROM T_PRI_MENU t1
        LEFT JOIN T_PRI_ROLE_MENU t2 ON t1.ID = t2.MENU_ID
        LEFT JOIN T_PRI_USER_ROLE t3 ON t2.ROLE_ID = t3.ROLE_ID
        WHERE
         t3.USER_ID = #userId
        <if test="pId != null and pId != ‘‘">
            <if test="pId != ‘-1‘">
                AND t1.P_ID = #pId
            </if>
        </if>
        AND t1.STATUS = ‘0‘
        AND t1.IS_DELETE = ‘0‘
        ORDER BY t1.MENU_ORDER ASC, t1.ID DESC
    </select>

接口:

public interface IPriMenuService 
        public List<PriMenu> queryMenuByUserId(Map<String, Object> param);



@Service(interfaceClass=IPriMenuService.class, timeout=30000)
public class PriMenuServiceImpl implements IPriMenuService 
        @Override
    public List<PriMenu> queryMenuByUserId(Map<String, Object> param) 
        return priMenuMapper.queryMenuByUserId(param);
        


@MyBatisRepository
public interface PriMenuMapper 
        public List<PriMenu> queryMenuByUserId(Map<String, Object> param);

    

然后就是前后端接收和展示

  

 

以上是关于管理后台菜单查询的主要内容,如果未能解决你的问题,请参考以下文章

后台管理系统的权限控制与管理

hyii2后台管理系统-菜单篇

后台菜单和权限管理

Django 权限管理-后台根据用户权限动态生成菜单

wuzhicms后台菜单的添加

Ecshop :后台添加新功能 菜单及 管理权限 配置