SSM for循环优化 查询数据返回树结构

@Override
    public List<Bidsection<Workarea<Pname<Name>>>> getComponentInfo() {
        // 1级节点树
        List<Bidsection<Workarea<Pname<Name>>>> resultData = new ArrayList<>();
        long start = System.currentTimeMillis();
        // 查询1级节点
        List<Bidsection> bidsectionList = progressDao.queryBidsection();
        for (int i = 0; i < bidsectionList.size(); i++) {
            // 1级节点实体
            Bidsection<Workarea<Pname<Name>>> bidsection = new Bidsection<>();
            // 2级节点树
            List<Workarea<Pname<Name>>> workareaData = new ArrayList<>();
            // 查询2级节点
            List<Workarea> workareaList = progressDao.queryWorkarea(bidsectionList.get(i).getTitle());
            for (int j = 0; j < workareaList.size(); j++) {
                // 2级节点实体
                Workarea<Pname<Name>> workarea = new Workarea<>();
                // 3级节点树
                List<Pname<Name>> pnameData = new ArrayList<>();
                // 查询3级节点
                List<Pname> pnameList = progressDao.queryPname(workareaList.get(j).getTitle());
                for (int k = 0; k < pnameList.size(); k++) {
                    // 3级节点实体
                    Pname<Name> pname = new Pname<>();
                    // 4级节点树
                    List<Name> nameData = new ArrayList<>();
                    // 查询4级节点
                    List<Name> nameList = progressDao.queryName(pnameList.get(k).getTitle());
                    for (int l = 0; l < nameList.size(); l++) {
                        // 4级节点实体
                        Name name = new Name();
                        name.setTitle(nameList.get(l).getTitle());
                        name.setModelId(nameList.get(l).getModelId());
                        nameData.add(name);
                    }
                    // 添加3级标题
                    pname.setTitle(pnameList.get(k).getTitle());
                    // 添加3级内容
                    pname.setChildren(nameList);
                    // 生成3级树
                    pnameData.add(pname);
                }
                // 添加2级标题
                workarea.setTitle(workareaList.get(j).getTitle());
                // 添加2级内容
                workarea.setChildren(pnameData);
                // 生成2级树
                workareaData.add(workarea);
            }
            // 添加1级标题
            bidsection.setTitle(bidsectionList.get(i).getTitle());
            // 添加1级内容
            bidsection.setChildren(workareaData);
            // 生成1级树
            resultData.add(bidsection);
        }
        long end = System.currentTimeMillis();
        System.out.print("-------耗时-------" + (end - start) / 1000 + "秒-------");

        return resultData;
    }

循环查询数据处理数据,查询下来40多秒,哪位大神帮忙看下怎么优化实现比较快啊!!急急急!!!

比较麻烦的方式就是使用程序查出根节点,然后通过递归调用的方式循环遍历查询当前节点下的子节点,然后保存到当前节点的子节点集合中。

public class Node{
   private String title;
     private List<Node> children;
}
//下面部分是构造函数和get、set方法实现

这样list集合遍历结束后(list集合中保存的对象是Node),构造的树结构集合对象就完毕了,最后将list转换为json格式的字符串就是满足你提供的这种树结构所需的数据格式的结果了。
希望对你有所帮助。