java递归查询省市区树形结构

具体代码实现如下:

 @RequestMapping("getTree")
    public Map<String, Object> getTree(int id) {
        Map<String, Object> map = new HashMap<>();
        try {
            ChinaCitys province = dao.findProvince(id); //查询出一个省
            if (province != null) {
                List<ChinaCitys> citys = dao.findChildren(province.getId());//查询省下面的所有市
                digui(citys); //调用递归算法查询市以下的区县
                province.setChildren(citys);
            }
            map.put("data", province);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public void digui(List<ChinaCitys> citys) {
        List<ChinaCitys> retList = new ArrayList<>();
        for (ChinaCitys c : citys) {
            retList = dao.findChildren(c.getId());
            if (retList.size() > 0) {
                c.setChildren(retList);
                digui(retList); //循环调用自己
            }
        }
    }

sql语句:

<select id="findProvince" parameterType="java.lang.Integer" resultMap="BaseResultMap">
             select id,name from china_citys
             WHERE id = #{id}
    </select>

    <select id="findChildren" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select id,name  from china_citys
        WHERE upid = #{id}
    </select>

 

转载于:https://www.cnblogs.com/superjie/p/11138438.html

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现树形结构递归查询可以采用深度优先遍历算法(DFS)。具体实现可以按照以下步骤进行: 1. 定义树节点类,包括节点ID、父节点ID、节点名称等属性。 2. 定义树形结构类,包括根节点、节点列表等属性。节点列表可以使用Map集合来存储节点对象,以节点ID作为键。 3. 实现深度优先遍历算法,递归遍历树形结构。具体实现方式可以按照以下步骤进行: a. 从根节点开始遍历,将节点加入遍历路径中。 b. 遍历当前节点的所有子节点,如果子节点在遍历路径中已存在,则说明存在环路,返回空;否则,递归遍历子节点。 c. 如果子节点遍历完成后,没有找到目标节点,则将当前节点从遍历路径中移除,回溯到上一级节点继续遍历。 4. 调用深度优先遍历算法,查询目标节点。 下面是一个简单的Java代码示例: ```java public class TreeNode { private String id; private String parentId; private String name; // getter和setter方法省略 } public class Tree { private Map<String, TreeNode> nodeMap = new HashMap<String, TreeNode>(); private TreeNode root; // 添加节点方法 public void addNode(TreeNode node) { if (node == null) { return; } if (node.getParentId() == null || node.getParentId().equals("")) { this.root = node; } nodeMap.put(node.getId(), node); } // 根据节点ID查询节点 public TreeNode findNodeById(String id) { TreeNode node = nodeMap.get(id); if (node == null) { return null; } List<TreeNode> path = new ArrayList<TreeNode>(); if (dfs(node, path)) { return node; } else { return null; } } // 深度优先遍历算法 private boolean dfs(TreeNode node, List<TreeNode> path) { path.add(node); if (node.getId().equals(id)) { return true; } for (Map.Entry<String, TreeNode> entry : nodeMap.entrySet()) { TreeNode child = entry.getValue(); if (child.getParentId() == null || child.getParentId().equals("")) { continue; } if (child.getParentId().equals(node.getId())) { if (path.contains(child)) { // 存在环路,返回空 return false; } if (dfs(child, path)) { return true; } } } path.remove(node); return false; } } ``` 其中,addNode方法用于添加节点,findNodeById方法用于根据节点ID查询节点,dfs方法是深度优先遍历算法的实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值