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>

controller

@RestController
@RequestMapping("/menu")
@Api(value = "菜单级联查询-WsMenuController",tags = "menu")
public class WsMenuController {

    @Autowired
    WsMenuService wsMenuService;

    private Logger log = LoggerFactory.getLogger(getClass());
    /**
     * 查询所有菜单
     *
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pid", value = "父菜单ID",defaultValue = "0", required = false, dataType = "int")
    })
    @ApiOperation(value = "查询所有菜单-返回树形列表", httpMethod = "GET", notes = "能显示出层级多层列表")
    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    public List<WsMenuVO> selectAllMenu(@RequestParam(value = "pid",required = false)Integer pid){

        if (pid == null){
            pid = 0;
        }
        List<WsMenuVO> result =  wsMenuService.selectAllMenu(pid);

     return  result;
    }
  }

service

@Service
public class WsMenuService {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private WsMenuMapper wsMenuMapper;


    public List<WsMenuVO> selectAllMenu(Integer pid) {

        return wsMenuMapper.selectAllMenu(pid);
    }
 }

mapper

public interface WsMenuMapper extends BaseMapper<WsMenu> {

	 List<WsMenuVO> selectAllMenu(@Param("pid") Integer pid);

}

 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值