主要代码在service实现类中

表结构

CREATE TABLE `cancel_reason`  (
  `id` bigint NOT NULL COMMENT '主键id',
  `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '类目名称',
  `pid` bigint NULL DEFAULT 0 COMMENT '父id',
  `sort` int NULL DEFAULT NULL COMMENT '排序',
  `del_flag` tinyint NULL DEFAULT 0 COMMENT '删除状态(0未删除,1已删除)',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '取消订单原因配置表' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of cancel_reason
-- ----------------------------
INSERT INTO `cancel_reason` VALUES (1845001217768427520, '研发部', 0, NULL, NULL);
INSERT INTO `cancel_reason` VALUES (1845001497000022016, '研发一组', 1845001217768427520, NULL, 0);
INSERT INTO `cancel_reason` VALUES (1845001571499249664, '研发二组', 1845001217768427520, NULL, 0);
INSERT INTO `cancel_reason` VALUES (1845001646707314688, '网约车', 1845001497000022016, NULL, 0);
INSERT INTO `cancel_reason` VALUES (1845001739825057792, '新能源充电', 1845001571499249664, NULL, 0);
INSERT INTO `cancel_reason` VALUES (1845001856208605184, '综合部', 0, NULL, 0);
INSERT INTO `cancel_reason` VALUES (1845001940480561152, '财务部', 1845001856208605184, NULL, 0);
INSERT INTO `cancel_reason` VALUES (1845002035875811328, '人事部', 1845001856208605184, NULL, 0);
INSERT INTO `cancel_reason` VALUES (1845002099369185280, '考勤专员', 1845002035875811328, NULL, 0);
INSERT INTO `cancel_reason` VALUES (1845002186484879360, '薪酬专员', 1845001940480561152, NULL, 0);

SET FOREIGN_KEY_CHECKS = 1;

实体类

/**
 * @description cancel_reason
 */
public class CancelReason implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 主键id
     */
    private Long id;

    /**
     * 类目名称
     */
    private String name;

    /**
     * 父id
     */
    private Long pid;

    /**
     * 排序
     */
    private Integer sort;

    /**
     * 删除状态(0未删除,1已删除)
     */
    private Integer delFlag;

    /**
     * 子节点
     */
    private List<CancelReason> children;


    public List<CancelReason> getChildren() {
        return children;
    }

    public CancelReason setChildren(List<CancelReason> children) {
        this.children = children;
        return this;
    }

    public Long getId() {
        return id;
    }

    public CancelReason setId(Long id) {
        this.id = id;
        return this;
    }

    public String getName() {
        return name;
    }

    public CancelReason setName(String name) {
        this.name = name;
        return this;
    }

    public Long getPid() {
        return pid;
    }

    public CancelReason setPid(Long pid) {
        this.pid = pid;
        return this;
    }

    public Integer getSort() {
        return sort;
    }

    public CancelReason setSort(Integer sort) {
        this.sort = sort;
        return this;
    }

    public Integer getDelFlag() {
        return delFlag;
    }

    public CancelReason setDelFlag(Integer delFlag) {
        this.delFlag = delFlag;
        return this;
    }
}

mapper

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @description cancel_reasonMapper
 */
@Mapper
@Repository
public interface CancelReasonMapper {

    @Select("select * from cancel_reason")
    List<CancelReason> list();

}

service

import java.util.List;

/**
 * @description cancel_reason
 */
public interface ICancelReasonService {

     List<CancelReason> list();

}

service实现类

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @description cancel_reason
 * @author BEJSON
 * @date 2024-10-12
 */
@Service
public class CancelReasonServiceImpl implements ICancelReasonService {

    @Resource
    private CancelReasonMapper cancelReasonMapper;


    @Override
    public List<CancelReason> list() {
        return recursionMethod(cancelReasonMapper.list());
    }

    /**
     * 递归方法转换成树形结构
     * @param treeList
     * @return  返回树形的list
     */
    public static List<CancelReason> recursionMethod(List<CancelReason> treeList) {
        List<CancelReason> trees = new ArrayList<>();
        //先遍历所有数据
        for (CancelReason tree : treeList) {
            // 如果pid是0,则说明是根节点
            if (0 == tree.getPid()) {
                // 调用递归方法找到当前循环对象的所有子节点
                trees.add(findChildren(tree, treeList));
            }
        }
        return trees;
    }

    /**
     * 递归方法
     * @param tree 父节点对象
     * @param treeList 所有的List
     * @return  返回父节点
     */
    public static CancelReason findChildren(CancelReason tree, List<CancelReason> treeList) {
        //遍历所有子节点
        for (CancelReason node : treeList) {
            // 如果节点id与父节点id相同,则说明是子节点
            if (tree.getId().equals(node.getPid())) {
                // 如果子节点还不存在,创建一个空的list
                if (tree.getChildren() == null) {
                    tree.setChildren(new ArrayList<>());
                }
                // 递归调用,将当前循环对象加到父节点的children中,然后继续找当前循环对象的子节点
                tree.getChildren().add(findChildren(node, treeList));
            }
        }
        //返回父节点
        return tree;
    }
}

controller

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/test")
public class CancelReasonController {

    @Resource
    private ICancelReasonService cancelReasonService;

    @GetMapping
    public AjaxResult list() {
        return AjaxResult.success(cancelReasonService.list());
    }
}

效果

List转树形结构_子节点