cy
2022-06-22 6e06bba1c89f8077e29d0fbf0ce12f89f027d8d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package cn.ksource.test.test;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
 
import cn.ksource.core.dao.BaseDao;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext_core.xml"})
@Transactional
public class TreeTest {
 
    @Autowired
    private BaseDao baseDao;
    
    
    @Test
    public void treeTest() {
        String sql = "SELECT * FROM CMS_CATEGORY WHERE USINGSTATE = 1 ORDER BY LEVEL,ORDERNUM";
        List<Map> treeList = baseDao.queryForList(sql);
        
        List<Map> resultList = new ArrayList<Map>();
        
        Map<String,Map> yjgnMap = new HashMap<String, Map>();
        Map<String,Map> ejgnMap = new HashMap<String, Map>();
        
        for(Map map : treeList) {
            if(map.get("LEVEL").toString().equals("1")) {
                yjgnMap.put(map.get("ID").toString(), map);
                List<Map> ejgnList = new LinkedList<Map>();
                map.put("ejgn", ejgnList);
                resultList.add(map);
                continue;
            }
            
            if(map.get("LEVEL").toString().equals("2")) {
                Map yjgn = yjgnMap.get(map.get("UP_ID").toString());
                List<Map> ejgn = (List<Map>)yjgn.get("ejgn");
                List<Map> sjgnList = new LinkedList<Map>();
                map.put("sjgn", sjgnList);
                ejgn.add(map);
                ejgnMap.put(map.get("ID").toString(), map);
                continue;
            }
        }
        System.out.println(resultList);
    }
}