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

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>

 

posted @ 2020-11-03 16:26  小菜bxb  阅读(126)  评论(0编辑  收藏  举报