• 递归的使用-- 国民经济行业类别集合递归树


    controller

    private static DCCommUtil commUtil; 
    1

    @RequestMapping("/getALLIndustryTotree") 2 public Result getALLIndustryTotree() { 3 Result result = new Result(); 4 //1、查询所有国民经济行业类别 5 List<industry> industryList=codeService.getALLIndustry(); 6 List<industry> returnIndustryList = new ArrayList<industry>(); 7 //2、遍历国民经济行业类别 8 for (industry industry : industryList) { 9 if (industry.getPid().equals("0")) { 10 //3、调用递归组装树 11 industry = commUtil.buildTree(industryList, industry); 12 returnIndustryList.add(industry); 13 } 14 } 15 try { 16 result = result.buileSuccess(); 17 result.setData(returnIndustryList); 18 } catch (Exception e) { 19 result = result.buileFailure(); 20 result.setData(e.getMessage()); 21 } 22 return result; 23 }

    DCCommUtil.java

     1 public class DCCommUtil {
     2     /**
     3      * 国民经济行业类别集合递归树
     4      * @param list
     5      * @param industry
     6      * @return
     7      */
     8     public static industry buildTree(List<industry> list, industry industry){
     9         List<industry> children = new ArrayList<industry>();
    10         for (industry item : list) {
    11             if (item.getPid().equals(industry.getId())) {
    12                 children.add(buildTree(list, item));
    13             }
    14         }
    15         industry.setChildren(children);
    16         return industry;
    17     }

    industry.java

    1 public class industry {
    2     private String id;
    3     private String pid;
    4     private String singlehyname;
    5     private List<industry> children=new ArrayList();

    mapper.xml         CASE  WHEN

    1 <!-- 查询所有国民经济行业类别 -->
    2     <select id="getALLIndustry" resultType="com.mapuni.datacenter.entity.industry">
    3         select 
    4             id,
    5             (CASE pid WHEN '' THEN '0' WHEN NULL THEN '0' ELSE pid END) as pid, 
    6             singlehyname 
    7         from t_cod_industry
    8     </select>
  • 相关阅读:
    信息安全基本概念
    GmSSL开发环境搭建及双证书生成
    Git使用教程
    linux软件管理
    在Windows中查看文件的MD5值
    odoo显示页面格式化日期的一个方法
    Ubuntu 查看文件夹大小
    docker容器中启动postgresql 9.5失败:could not locate a valid checkpoint record
    如何让postgresql日志记录所有的执行语句
    vim快捷键
  • 原文地址:https://www.cnblogs.com/bxbo/p/13921016.html
Copyright © 2020-2023  润新知