zoukankan      html  css  js  c++  java
  • 递归的使用-- 国民经济行业类别集合递归树

    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>
  • 相关阅读:
    VS创建MVC出错解决方法
    服务器环境~某个页面无法访问的处理
    SSIS 处理错误的方法
    SSIS 数据流的执行树和数据管道
    利用SSIS的ForcedExecutionResult 属性 和CheckPoint调试Package
    处于同一域中的两台SQL Server 实例无法连接
    SQL Server 日期和时间类型
    Lookup 转换组件
    约束2:主键约束,唯一约束和唯一索引
    查询语句影响的行数
  • 原文地址:https://www.cnblogs.com/bxbo/p/13921016.html
Copyright © 2011-2022 走看看