视频演示的递归太耗时了,优化之后,查询速度提高不少,还望采纳

视频演示的递归太耗时了,优化之后,查询速度提高不少,还望采纳

演示代码:

public List<CategoryVO> listCategoryForCustomer(){
    List<CategoryVO> categoryVOList = new ArrayList<>();
    recursivelyFindCategories(categoryVOList, 0);
    return categoryVOList;
}
private void recursivelyFindCategories(List<CategoryVO> categoryVOList, Integer parentId){
    //递归获取所有子类别,并组合成为一个“目录树”
    List<Category> categoryList = categoryMapper.selectCategoriesByParentId(parentId);
    if (!CollectionUtils.isEmpty(categoryList)) {
        categoryList.forEach(category -> {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(category, categoryVO);
            categoryVOList.add(categoryVO);
            recursivelyFindCategories(categoryVO.getChildCategory(), categoryVO.getId());
        });
    }
}

我写代码:

package com.imooc.mall.model.vo;

import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CategoryTree {
    private Integer id;
    private String name;
    private Integer type;
    private Integer parentId;
    private Integer orderNum;
    private Date createTime;
    private Date updateTime;
    private List<CategoryTree> childCategory;
    public CategoryTree() {
        childCategory = new ArrayList<>();
    }
    public CategoryTree(Integer id, String name, Integer type, Integer parentId, Integer orderNum) {
        this();
        this.id = id;
        this.name = name;
        this.type = type;
        this.parentId = parentId;
        this.orderNum = orderNum;
    }
    public void addTreeNode(CategoryTree treeNode) {
        if ("0".equals(treeNode.getParentId()) || StringUtils.isEmpty(treeNode.getParentId())) {
            this.childCategory.add(treeNode);
        } else if (this.id.equals(treeNode.getParentId())) {
            this.childCategory.add(treeNode);
        } else {
            for (CategoryTree category : this.childCategory) {
                category.addTreeNode(treeNode);
            }
        }
    }
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("{");
        builder.append("\"id\"").append(":").append(id);
        builder.append(",\"name\"").append(":").append("\"" + name + "\"");
        builder.append(",\"type\"").append(":").append(type);
        builder.append(",\"parentId\"").append(":").append(parentId);
        builder.append(",\"orderNum\"").append(":").append(orderNum);
        builder.append(",\"createTime\"").append(":").append(createTime);
        builder.append(",\"updateTime\"").append(":").append(updateTime);
        builder.append(",\"childCategory\"").append(":").append(childCategory);
        builder.append("}");
        return builder.toString();
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getType() {
        return type;
    }
    public void setType(Integer type) {
        this.type = type;
    }
    public Integer getParentId() {
        return parentId;
    }
    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }
    public Integer getOrderNum() {
        return orderNum;
    }
    public void setOrderNum(Integer orderNum) {
        this.orderNum = orderNum;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    public Date getUpdateTime() {
        return updateTime;
    }
    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
    public List<CategoryTree> getChildCategory() {
        return childCategory;
    }
    public void setChildCategory(List<CategoryTree> childCategory) {
        this.childCategory = childCategory;
    }
}
package com.imooc.mall;

import com.imooc.mall.model.dao.CategoryMapper;
import com.imooc.mall.model.pojo.Category;
import com.imooc.mall.model.vo.CategoryTree;
import com.imooc.mall.model.vo.CategoryVO;
import com.imooc.mall.service.CategoryService;
import com.imooc.mall.utils.JacksonUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.Instant;
import java.util.Comparator;
import java.util.List;

@SpringBootTest
class MallApplicationTests {

    @Resource
    private CategoryService categoryService;
    @Resource
    private CategoryMapper categoryMapper;

    @Test
    void contextLoads() {
        Instant start = Instant.now();
        List<CategoryVO> categoryVOList = categoryService.listCategoryForCustomer();
        Instant end = Instant.now();
        Duration between = Duration.between(start, end);
        System.out.println("time: "+between.toMillis());
        System.out.println("rest: "+JacksonUtils.objectToJson(categoryVOList));

        Instant start2 = Instant.now();
        CategoryTree rootTree = new CategoryTree(0,"全部目录",0,0,0);
        List<Category> categoryList = categoryMapper.selectList();
        categoryList.stream().sorted(Comparator.comparing(Category::getType).thenComparing(Category::getParentId).thenComparing(Category::getId)).forEach(category -> {
            CategoryTree categoryTree = new CategoryTree();
            BeanUtils.copyProperties(category, categoryTree);
            rootTree.addTreeNode(categoryTree);
        });
        Instant end2 = Instant.now();
        Duration between2 = Duration.between(start2, end2);
        System.out.println("time: "+between2.toMillis());
        System.out.println("rest: "+JacksonUtils.objectToJson(rootTree.getChildCategory()));
    }

}

运行结果:

[08:17 18:02:56.685] [INFO] [com.alibaba.druid.pool.DruidDataSource] - {dataSource-1} inited
time: 1651
rest: [{"id":3,"name":"新鲜水果","type":1,"parentId":0,"orderNum":1,"createTime":1576603020000,"updateTime":1577524286000,"childCategory":[{"id":4,"name":"橘子橙子","type":2,"parentId":3,"orderNum":1,"createTime":1576603020000,"updateTime":1577521510000,"childCategory":[{"id":19,"name":"果冻橙","type":3,"parentId":4,"orderNum":1,"createTime":1576603020000,"updateTime":1581352622000,"childCategory":[]}]},{"id":11,"name":"草莓","type":2,"parentId":3,"orderNum":2,"createTime":1576603020000,"updateTime":1577519082000,"childCategory":[]},{"id":12,"name":"奇异果","type":2,"parentId":3,"orderNum":3,"createTime":1576603020000,"updateTime":1577521512000,"childCategory":[]},{"id":14,"name":"车厘子","type":2,"parentId":3,"orderNum":4,"createTime":1576603020000,"updateTime":1577521512000,"childCategory":[]},{"id":28,"name":"其他水果","type":2,"parentId":3,"orderNum":4,"createTime":1576603020000,"updateTime":1577521512000,"childCategory":[]}]},{"id":5,"name":"海鲜水产","type":1,"parentId":0,"orderNum":2,"createTime":1576603020000,"updateTime":1577521520000,"childCategory":[{"id":7,"name":"螃蟹","type":2,"parentId":5,"orderNum":1,"createTime":1576603020000,"updateTime":1577521515000,"childCategory":[]},{"id":8,"name":"鱼类","type":2,"parentId":5,"orderNum":2,"createTime":1576603020000,"updateTime":1577521516000,"childCategory":[]},{"id":13,"name":"海参","type":2,"parentId":5,"orderNum":3,"createTime":1576603020000,"updateTime":1577521517000,"childCategory":[]}]},{"id":6,"name":"精选肉类","type":1,"parentId":0,"orderNum":3,"createTime":1576603020000,"updateTime":1577521521000,"childCategory":[{"id":16,"name":"牛羊肉","type":2,"parentId":6,"orderNum":1,"createTime":1576603020000,"updateTime":1577521518000,"childCategory":[]}]},{"id":9,"name":"冷饮冻食","type":1,"parentId":0,"orderNum":4,"createTime":1576820728000,"updateTime":1577521522000,"childCategory":[{"id":17,"name":"冰淇淋","type":2,"parentId":9,"orderNum":1,"createTime":1576603020000,"updateTime":1577521518000,"childCategory":[]}]},{"id":10,"name":"蔬菜蛋品","type":1,"parentId":0,"orderNum":5,"createTime":1576820728000,"updateTime":1577521523000,"childCategory":[{"id":18,"name":"蔬菜综合","type":2,"parentId":10,"orderNum":1,"createTime":1576603020000,"updateTime":1581353307000,"childCategory":[]}]},{"id":27,"name":"美味菌菇","type":1,"parentId":0,"orderNum":7,"createTime":1576820728000,"updateTime":1581348036000,"childCategory":[{"id":15,"name":"火锅食材","type":2,"parentId":27,"orderNum":5,"createTime":1576603020000,"updateTime":1581352953000,"childCategory":[]}]}]
time: 8
rest: [{"id":3,"name":"新鲜水果","type":1,"parentId":0,"orderNum":1,"createTime":1576603020000,"updateTime":1577524286000,"childCategory":[{"id":4,"name":"橘子橙子","type":2,"parentId":3,"orderNum":1,"createTime":1576603020000,"updateTime":1577521510000,"childCategory":[{"id":19,"name":"果冻橙","type":3,"parentId":4,"orderNum":1,"createTime":1576603020000,"updateTime":1581352622000,"childCategory":[]}]},{"id":11,"name":"草莓","type":2,"parentId":3,"orderNum":2,"createTime":1576603020000,"updateTime":1577519082000,"childCategory":[]},{"id":12,"name":"奇异果","type":2,"parentId":3,"orderNum":3,"createTime":1576603020000,"updateTime":1577521512000,"childCategory":[]},{"id":14,"name":"车厘子","type":2,"parentId":3,"orderNum":4,"createTime":1576603020000,"updateTime":1577521512000,"childCategory":[]},{"id":28,"name":"其他水果","type":2,"parentId":3,"orderNum":4,"createTime":1576603020000,"updateTime":1577521512000,"childCategory":[]}]},{"id":5,"name":"海鲜水产","type":1,"parentId":0,"orderNum":2,"createTime":1576603020000,"updateTime":1577521520000,"childCategory":[{"id":7,"name":"螃蟹","type":2,"parentId":5,"orderNum":1,"createTime":1576603020000,"updateTime":1577521515000,"childCategory":[]},{"id":8,"name":"鱼类","type":2,"parentId":5,"orderNum":2,"createTime":1576603020000,"updateTime":1577521516000,"childCategory":[]},{"id":13,"name":"海参","type":2,"parentId":5,"orderNum":3,"createTime":1576603020000,"updateTime":1577521517000,"childCategory":[]}]},{"id":6,"name":"精选肉类","type":1,"parentId":0,"orderNum":3,"createTime":1576603020000,"updateTime":1577521521000,"childCategory":[{"id":16,"name":"牛羊肉","type":2,"parentId":6,"orderNum":1,"createTime":1576603020000,"updateTime":1577521518000,"childCategory":[]}]},{"id":9,"name":"冷饮冻食","type":1,"parentId":0,"orderNum":4,"createTime":1576820728000,"updateTime":1577521522000,"childCategory":[{"id":17,"name":"冰淇淋","type":2,"parentId":9,"orderNum":1,"createTime":1576603020000,"updateTime":1577521518000,"childCategory":[]}]},{"id":10,"name":"蔬菜蛋品","type":1,"parentId":0,"orderNum":5,"createTime":1576820728000,"updateTime":1577521523000,"childCategory":[{"id":18,"name":"蔬菜综合","type":2,"parentId":10,"orderNum":1,"createTime":1576603020000,"updateTime":1581353307000,"childCategory":[]}]},{"id":27,"name":"美味菌菇","type":1,"parentId":0,"orderNum":7,"createTime":1576820728000,"updateTime":1581348036000,"childCategory":[{"id":15,"name":"火锅食材","type":2,"parentId":27,"orderNum":5,"createTime":1576603020000,"updateTime":1581352953000,"childCategory":[]}]}]


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕珊 2020-08-18 10:51:45

同学,你好!老师进一步对代码进行了分析,其实最主要的耗时并不是在递归处理,你写的方法里面也是有递归的,递归是处理这类问题的正确写法。如果同学把测试的代码中两种方法换一下顺序,会发现时间差距就不大了。这是因为主要耗时发生在数据库的查询,第一次查询需要连接到数据库,所以比较耗时,而且一次性查询出来所有数据会比分多次查询节省时间。课程中,对目录列表做了Redis缓存,所以从第二次访问起,速度会有很大提高。祝学习愉快!

  • 提问者 DaVinciYangWang #1
    嗯嗯,这个问题我也发现了
    2020-08-18 15:35:33
  • 好帮手慕珊 回复 提问者 DaVinciYangWang #2
    能提出问题,说明进行了认真的思考,非常棒!加油!
    2020-08-18 18:49:22
好帮手慕珊 2020-08-17 18:56:36

同学,你好!非常感谢你的建议,这种研究精神非常值得学习,我们已经把这个问题反馈给了相关老师进行处理。祝学习愉快!

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师