mybatis递归查询父子菜单

查询结果

[
    {
        "id": 1,
        "menuName": "菜单01(1层)",
        "pid": 0,
        "list": [
            {
                "id": 3,
                "menuName": "菜单01(2层)",
                "pid": 1,
                "list": [
                    {
                        "id": 5,
                        "menuName": "菜单01(3层)",
                        "pid": 3,
                        "list": [
                            {
                                "id": 7,
                                "menuName": "菜单01(4层)",
                                "pid": 5,
                                "list": []
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "id": 2,
        "menuName": "菜单02(1层)",
        "pid": 0,
        "list": [
            {
                "id": 4,
                "menuName": "菜单02(2层)",
                "pid": 2,
                "list": [
                    {
                        "id": 6,
                        "menuName": "菜单02(3层)",
                        "pid": 4,
                        "list": [
                            {
                                "id": 8,
                                "menuName": "菜单02(4层)",
                                "pid": 6,
                                "list": []
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

数据库截图

返回模型


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * @author yang
 * @Title: WsMenuVO.java
 * @Description: 菜单模型
 * @date 2019年03月18日 9:35
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class WsMenuVO {
    //菜单ID
    private Integer id;
    //菜单名称
    private String menuName;
    //父菜单ID
    private Integer pid;

    private List<WsMenuVO> list;
}

mybatis 语句

<!--级联查询返回模型-->
    <resultMap type="com.cloudtop.demo19.VO.WsMenuVO" id="fatherMap">
        <id column="id" property="id"/>
        <id column="menuName" property="menuName"/>
        <collection property="list" ofType="com.cloudtop.demo19.VO.WsMenuVO" column="id" select="findMenuByPid"/>
    </resultMap>

    <!--级联查询父菜单-->
    <select id="selectAllMenu" resultMap="fatherMap" >
         select * from ws_menu where pid = 0
    </select>

    <!--级联查询子菜单-->
    <select id="findMenuByPid" resultMap="fatherMap" >
         select * from ws_menu where pid = #{id}
    </select>
发布了51 篇原创文章 · 获赞 18 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/m0_37882063/article/details/88633781