码迷,mamicode.com
首页 > 其他好文 > 详细

管理后台菜单查询

时间:2019-08-16 14:31:21      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:role   bsp   and   ace   left join   err   tis   通过   order by   

本查询用于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);
}
    

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

  

 

管理后台菜单查询

标签:role   bsp   and   ace   left join   err   tis   通过   order by   

原文地址:https://www.cnblogs.com/gjq1126-web/p/11363635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!