Mybatis中实现递归查询

在Mybatis中实现递归查询,父类嵌套子类

swagger测试结果:

{
  "status": 0,
  "data": {
    "pageNum": 1,
    "pageSize": 11,
    "size": 2,
    "startRow": 1,
    "endRow": 2,
    "total": 2,
    "pages": 1,
    "list": [
      {
        "goodscateid": 1,
        "name": "食品类",
        "parentid": 0,
        "description": "11111111111111111",
        "displayorder": 1,
        "commissionrate": null,
        "enabled": 1,
        "catelist": [
          {
            "goodscateid": 2,
            "name": "生鲜类",
            "parentid": 1,
            "description": "222222222222222",
            "displayorder": 2,
            "commissionrate": null,
            "enabled": 1,
            "catelist": [
              {
                "goodscateid": 3,
                "name": "海鲜类",
                "parentid": 2,
                "description": "3333333",
                "displayorder": 3,
                "commissionrate": null,
                "enabled": 1,
                "catelist": []
              }
            ]
          }
        ]
      },
      {
        "goodscateid": 4,
        "name": "服装类",
        "parentid": 0,
        "description": "服装生成商",
        "displayorder": 2,
        "commissionrate": null,
        "enabled": 1,
        "catelist": []
      }
    ],
    "prePage": 0,
    "nextPage": 0,
    "isFirstPage": true,
    "isLastPage": true,
    "hasPreviousPage": false,
    "hasNextPage": false,
    "navigatePages": 8,
    "navigatepageNums": [
      1
    ],
    "navigateFirstPage": 1,
    "navigateLastPage": 1,
    "firstPage": 1,
    "lastPage": 1
  }
}

其中 parentid为父类goodscateid,默认无父类则parentid=0

实现如下:

    
        
        
        
        
    

    

mapper中方法为:

List getCategory(Integer pid);

pid为从parentid为几级的开始查询,如从根部开始即传pid=0

VO属性如下:

public class GoodsCategoryVo {
    private Integer goodscateid;
    private String name;
    private Integer parentid;
    private String description;
    private Integer displayorder;
    private Double commissionrate;
    private Integer enabled;
    private List catelist;
……
}

你可能感兴趣的:(Mybatis,递归)