当前位置:   article > 正文

java树结构查询及分页_java树形结构查询

java树形结构查询

目录

1、简单表结构示例:

结果展示:

实体类TestTree

controller

service

mapper

2、多层次使用此方法也可以查询出树形结构

示例:

3、根据根节点分页

TreeEntity

BaseEntity 

sysDept

SysdeptPlus

serviceImpl


1、简单表结构示例:

结果展示:

  1. {
  2. "success": true,
  3. "message": "操作成功!",
  4. "code": 200,
  5. "result": [
  6. {
  7. "id": 1,
  8. "pid": 0,
  9. "name": "中国",
  10. "code": "0001",
  11. "children": [
  12. {
  13. "id": 2,
  14. "pid": 1,
  15. "name": "河南",
  16. "code": "0002",
  17. "children": [
  18. {
  19. "id": 4,
  20. "pid": 2,
  21. "name": "周口",
  22. "code": "0021",
  23. "children": []
  24. },
  25. {
  26. "id": 5,
  27. "pid": 2,
  28. "name": "郑州",
  29. "code": "0022",
  30. "children": []
  31. }
  32. ]
  33. },
  34. {
  35. "id": 3,
  36. "pid": 1,
  37. "name": "台湾",
  38. "code": "0003",
  39. "children": [
  40. {
  41. "id": 6,
  42. "pid": 3,
  43. "name": "台北",
  44. "code": "0031",
  45. "children": []
  46. }
  47. ]
  48. },
  49. {
  50. "id": 7,
  51. "pid": 1,
  52. "name": "北京",
  53. "code": "0004",
  54. "children": []
  55. }
  56. ]
  57. }
  58. ],
  59. "resultTwo": null,
  60. "resultThree": null,
  61. "timestamp": 1687765801212
  62. }

实体类TestTree

  1. @Data
  2. @TableName("test_tree")
  3. @Accessors(chain = true)
  4. @ApiModel(value="测试树", description="测试树")
  5. public class TestTree {
  6. @ApiModelProperty(value = "ID")
  7. private Integer id;
  8. @ApiModelProperty(value = "父id")
  9. private Integer pid;
  10. @ApiModelProperty(value = "姓名")
  11. private String name;
  12. @ApiModelProperty(value = "编码")
  13. private String code;
  14. @TableField(exist = false)
  15. private List<TestTree> children;
  16. }

controller

  1. @Slf4j
  2. @Api(tags = "测试树")
  3. @RestController
  4. @RequestMapping("/test")
  5. public class TestTreeController {
  6. @Resource
  7. private TestTreeService testTreeService;
  8. @ApiOperation(value = "测试树",notes = "测试树")
  9. @GetMapping("/selectTree")
  10. public Result<Object> selectTree() {
  11. Result<Object> result = testTreeService.selectTree();
  12. return Result.ok(result);
  13. }
  14. }

 service

  1. public interface TestTreeService {
  2. Result<Object> selectTree();
  3. }
  4. @Service
  5. public class TestTreeServiceImpl implements TestTreeService {
  6. @Resource
  7. private TestTreeMapper testTreeMapper;
  8. @Override
  9. public Result<Object> selectTree() {
  10. List<TestTree> testTreeList=this.testTreeMapper.selectTree();
  11. List<TestTree> testTrees = testTreeList.stream().filter(testTree -> 0 == testTree.getPid())
  12. .peek(testTree -> testTree.setChildren(createChildList(testTree, testTreeList)))
  13. .collect(Collectors.toList());
  14. return Result.ok(testTrees);
  15. }
  16. /**
  17. * @param testTree 父级
  18. * @param testTreeList 对应的list
  19. * @return
  20. */
  21. private static List<TestTree> createChildList(TestTree testTree, List<TestTree> testTreeList) {
  22. return testTreeList.stream().filter(model -> testTree.getId().equals(model.getPid()))
  23. .peek(model -> model.setChildren(createChildList(model, testTreeList)))
  24. .collect(Collectors.toList());
  25. }
  26. }

 mapper

sql:select * from test_tree 

2、多层次使用此方法也可以查询出树形结构

示例:

  1. {
  2. "success": true,
  3. "message": "操作成功!",
  4. "code": 200,
  5. "result": [
  6. {
  7. "id": 1,
  8. "pid": 0,
  9. "name": "中国",
  10. "code": "0001",
  11. "children": [
  12. {
  13. "id": 2,
  14. "pid": 1,
  15. "name": "河南",
  16. "code": "0002",
  17. "children": [
  18. {
  19. "id": 4,
  20. "pid": 2,
  21. "name": "周口",
  22. "code": "0021",
  23. "children": [
  24. {
  25. "id": 10,
  26. "pid": 4,
  27. "name": "郸城县",
  28. "code": "0211",
  29. "children": [
  30. {
  31. "id": 12,
  32. "pid": 10,
  33. "name": "汲冢镇",
  34. "code": "2111",
  35. "children": []
  36. },
  37. {
  38. "id": 13,
  39. "pid": 10,
  40. "name": "李楼乡",
  41. "code": "2112",
  42. "children": []
  43. }
  44. ]
  45. },
  46. {
  47. "id": 11,
  48. "pid": 4,
  49. "name": "沈丘县",
  50. "code": "0212",
  51. "children": []
  52. }
  53. ]
  54. },
  55. {
  56. "id": 5,
  57. "pid": 2,
  58. "name": "郑州",
  59. "code": "0022",
  60. "children": []
  61. }
  62. ]
  63. },
  64. {
  65. "id": 3,
  66. "pid": 1,
  67. "name": "台湾",
  68. "code": "0003",
  69. "children": [
  70. {
  71. "id": 6,
  72. "pid": 3,
  73. "name": "台北",
  74. "code": "0031",
  75. "children": []
  76. }
  77. ]
  78. },
  79. {
  80. "id": 7,
  81. "pid": 1,
  82. "name": "北京",
  83. "code": "0004",
  84. "children": []
  85. }
  86. ]
  87. },
  88. {
  89. "id": 8,
  90. "pid": 0,
  91. "name": "俄罗斯",
  92. "code": "1001",
  93. "children": [
  94. {
  95. "id": 9,
  96. "pid": 8,
  97. "name": "莫斯科",
  98. "code": "1011",
  99. "children": []
  100. }
  101. ]
  102. }
  103. ],
  104. "resultTwo": null,
  105. "resultThree": null,
  106. "timestamp": 1687766628068
  107. }

3、根据根节点分页

TreeEntity<T>

  1. @Data
  2. @EqualsAndHashCode(callSuper = true)
  3. public class TreeEntity<T> extends BaseEntity {
  4. private static final long serialVersionUID = 1L;
  5. /**
  6. * 父菜单名称
  7. */
  8. @TableField(exist = false)
  9. private String parentName;
  10. /**
  11. * 父菜单ID
  12. */
  13. private Long parentId;
  14. /**
  15. * 子部门
  16. */
  17. @TableField(exist = false)
  18. private List<T> children = new ArrayList<>();
  19. }

 BaseEntity 

  1. @Data
  2. public class BaseEntity implements Serializable {
  3. private static final long serialVersionUID = 1L;
  4. /**
  5. * 搜索值
  6. */
  7. @JsonIgnore
  8. @TableField(exist = false)
  9. private String searchValue;
  10. /**
  11. * 创建者
  12. */
  13. @TableField(fill = FieldFill.INSERT)
  14. @ApiModelProperty(value = "创建人")
  15. private String createName;
  16. /**
  17. * 创建时间
  18. */
  19. @TableField(fill = FieldFill.INSERT)
  20. @ApiModelProperty(value = "创建时间")
  21. private LocalDateTime createTime;
  22. /**
  23. * 更新者
  24. */
  25. @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
  26. @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  27. @TableField(fill = FieldFill.INSERT_UPDATE)
  28. @ApiModelProperty(value = "更新人")
  29. private String updateName;
  30. /**
  31. * 更新时间
  32. */
  33. @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
  34. @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  35. @TableField(fill = FieldFill.INSERT_UPDATE)
  36. @ApiModelProperty(value = "更新时间")
  37. private LocalDateTime updateTime;
  38. /**
  39. * 开始时间
  40. */
  41. @TableField(exist = false)
  42. @ApiModelProperty(value = "时间查询开始时间")
  43. private String beginCreateTime;
  44. /**
  45. * 结束时间
  46. */
  47. @TableField(exist = false)
  48. @ApiModelProperty(value = "时间查询结束时间")
  49. private String endCreateTime;
  50. /**
  51. * 页码
  52. */
  53. @TableField(exist = false)
  54. @ApiModelProperty(value = "页码")
  55. private Integer pageNum = 1;
  56. /**
  57. * 条数
  58. */
  59. @TableField(exist = false)
  60. @ApiModelProperty(value = "每页条数")
  61. private Integer pageSize = 10;
  62. /**
  63. * 请求参数
  64. */
  65. @JsonInclude(JsonInclude.Include.NON_EMPTY)
  66. // 在序列化Java对象为JSON时,只有属性值不为null,或者如果属性是集合或数组,则至少包含一个元素,或者如果属性是字符串,则不是空字符串,这些属性才会被包含在生成的JSON中。
  67. @TableField(exist = false)
  68. private Map<String, Object> params = new HashMap<>();
  69. }

sysDept

  1. @Data
  2. @EqualsAndHashCode(callSuper = true)
  3. @TableName("sys_dept")
  4. @NoArgsConstructor
  5. @AllArgsConstructor
  6. public class SysDept extends TreeEntity<SysDept> {
  7. private static final long serialVersionUID = 1L;
  8. /**
  9. * 部门ID
  10. */
  11. @TableId(value = "dept_id")
  12. private Long deptId;
  13. /**
  14. * 部门编码
  15. */
  16. @NotBlank(message = "部门编码不能为空")
  17. @Size(min = 0, max = 30, message = "部门编码长度不能超过{max}个字符")
  18. private String deptCode;
  19. /**
  20. * 部门名称
  21. */
  22. @NotBlank(message = "部门名称不能为空")
  23. @Size(min = 0, max = 30, message = "部门名称长度不能超过{max}个字符")
  24. private String deptName;
  25. /**
  26. * 部门类型 0机器人 1标件公司 2非标件公司
  27. */
  28. private String deptType;
  29. /**
  30. * 显示顺序
  31. */
  32. @NotNull(message = "显示顺序不能为空")
  33. private Integer orderNum;
  34. /**
  35. * 负责人
  36. */
  37. private String leader;
  38. /**
  39. * 联系电话
  40. */
  41. @Size(min = 0, max = 11, message = "联系电话长度不能超过{max}个字符")
  42. private String phone;
  43. /**
  44. * 邮箱
  45. */
  46. @Email(message = "邮箱格式不正确")
  47. @Size(min = 0, max = 50, message = "邮箱长度不能超过{max}个字符")
  48. private String email;
  49. /**
  50. * 部门状态:0正常,1停用
  51. */
  52. private String status;
  53. /**
  54. * 删除标志(0代表存在 2代表删除)
  55. */
  56. @TableLogic
  57. private String delFlag;
  58. /**
  59. * 祖级列表
  60. */
  61. private String ancestors;
  62. }

SysdeptPlus

  1. /**
  2. * @author ChenSir
  3. * @description
  4. * @date 2023/10/26
  5. **/
  6. @Data
  7. @NoArgsConstructor
  8. @AllArgsConstructor
  9. public class SysdeptPlus {
  10. private SysDept sysDept;
  11. private List<SysDept> rows;
  12. private int total;
  13. }

serviceImpl

  1. @Override
  2. public SysdeptPlus getTreePage(SysDept sysDept, PageQuery pageQuery) {
  3. int pageNum = pageQuery.getPageNum();
  4. int pageSize = pageQuery.getPageSize();
  5. // 1、查询所有分类
  6. QueryWrapper<SysDept> wrapper = Wrappers.query();
  7. wrapper.eq(StrUtil.isNotBlank(sysDept.getStatus()), "status", sysDept.getStatus());
  8. List<SysDept> sysDeptList = treePageMapper.selectList(wrapper);
  9. // 2、组装成父子树形结构 每条均为parentId=0 的根节点,其children包含其子节点
  10. List<SysDept> testTrees = sysDeptList.stream().filter(testTree -> 0 == testTree.getParentId())
  11. .peek(testTree -> testTree.setChildren(createChildList(testTree, sysDeptList)))
  12. .collect(Collectors.toList());
  13. // 3、分页
  14. List<SysDept> page = new ArrayList<>();
  15. int start = (pageNum - 1) * pageSize;
  16. for (int i = start; i < (start + pageSize > testTrees.size() ? testTrees.size() : start + pageSize); i++) {
  17. page.add(testTrees.get(i));
  18. }
  19. SysdeptPlus sysdeptPlus = new SysdeptPlus();
  20. sysdeptPlus.setRows(page);
  21. sysdeptPlus.setTotal(testTrees.size());
  22. return sysdeptPlus;
  23. }
  24. /**
  25. * @param testTree 父级
  26. * @param testTreeList 对应的list
  27. * @return
  28. */
  29. private static List<SysDept> createChildList(SysDept testTree, List<SysDept> testTreeList) {
  30. return testTreeList.stream().filter(model -> testTree.getDeptId().equals(model.getParentId()))
  31. .peek(model -> model.setChildren(createChildList(model, testTreeList)))
  32. .collect(Collectors.toList());
  33. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/528283
推荐阅读
相关标签
  

闽ICP备14008679号