• Mybatis实现多级菜单查询


    写在前面

    最近实现一个小需求,前端需要菜单的信息,需要向后端发起获取菜单的请求,菜单又是一个多级菜单,后端我用的mybatis进行数据库查询,实现的方法我这里想到有两种,欢迎大家补充。

    1. 在Menu类中添加属性private List children

    在菜单类中添加一个属性private List<Menu> children 用来存储子节点

    package com.example.springbootvue.entity;
    
    import java.io.Serializable;
    import java.util.List;
    
    import lombok.Data;
    
    /**
     * sp_permission
     * @author 
     */
    @Data
    public class Menu implements Serializable {
        private Short psId;
    
        /**
         * 权限名称
         */
        private String psName;
    
        /**
         * 父id
         */
        private Short psPid;
    
        /**
         * 控制器
         */
        private String psC;
    
        /**
         * 操作方法
         */
        private String psA;
    
        /**
         * 权限等级
         */
        private Object psLevel;
    
        /**
         * 子菜单列表
         */
        private List<Menu> children;
    
        /**
         * null
         */
        private String path;
    
    
        private static final long serialVersionUID = 1L;
    }
    

    在mapper接口中定义两个方法

    package com.example.springbootvue.mapper;
    
    import com.example.springbootvue.entity.Menu;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public interface MenuMapper {
        /**
         * 获取一级菜单
         * @param rootMenuId
         * @return
         */
        Menu getRootMenu(Integer rootMenuId);
    
        /**
         * 获取子菜单
         * @param parentId
         * @return
         */
        List<Menu> findMenuByParentId(String parentId);
       
    }
    

    在resultMap中定义一个collection

        <resultMap id="menuMap" type="com.example.springbootvue.entity.Menu">
            <id column="ps_id" jdbcType="SMALLINT" property="psId"/>
            <result column="ps_name" jdbcType="VARCHAR" property="psName"/>
            <result column="ps_pid" jdbcType="SMALLINT" property="psPid"/>
            <result column="ps_c" jdbcType="VARCHAR" property="psC"/>
            <result column="ps_a" jdbcType="VARCHAR" property="psA"/>
            <result column="ps_level" jdbcType="OTHER" property="psLevel"/>
            <result column="path" jdbcType="VARCHAR" property="path"/>
            //这是关键语句,对应Menu类中的List<Menu> children字段
            <collection property="children" ofType="com.example.springbootvue.entity.Menu"
                        column="ps_id" select="findMenuByParentId"/>
        </resultMap>
    

    这是关键代码,对象Menu类中的List<Menu> children属性

    然后在menuMapper.xml实现mapper中的两个方法

        <select id="getRootMenu" resultMap="menuMap" parameterType="integer">
            select *
            from sp_permission
            where ps_id = #{value}
        </select>
    
        <select id="findMenuByParentId" resultMap="menuMap" parameterType="string">
            select *
            from sp_permission
            where ps_pid = #{value}
        </select>
    

    再贴上我数据库的表结构

  • 相关阅读:
    文件系统
    用户
    Kali Linux命令(3)
    Kali Linux命令(2)
    Kali Linux命令(1)
    文件上传测试 bugku
    Seay源代码审计系统
    实验吧 BrainFuck
    zigbee学习之路(十一):看门狗
    zigbee学习之路(十):串口(接收)
  • 原文地址:https://www.cnblogs.com/Fzeng/p/14743195.html
Copyright © 2020-2023  润新知