当前位置: 首页 > news >正文

廊坊网站建设系统广告联盟全自动赚钱系统

廊坊网站建设系统,广告联盟全自动赚钱系统,北京网站建设兴田德润官网多少,眉山做网站前提: 我们在写查询的时候,有时候会遇到多表联查,一遇到多表联查大家就会直接写sql语句,不会使用较为方便的LambdaQueryWrapper去查询了。作为一个2024新进入码农世界的小白,我喜欢使用LambdaQueryWrapper,…

前提:

   我们在写查询的时候,有时候会遇到多表联查,一遇到多表联查大家就会直接写sql语句,不会使用较为方便的LambdaQueryWrapper去查询了。作为一个2024新进入码农世界的小白,我喜欢使用LambdaQueryWrapper,虽然他会有很多缺点,但是能跑就行嘛。

背景:

  我在公司写了一套查询,遍历一个list,在遍历的时候每次都会查询一次数据库,该list极有可能是十万级的,我的好师兄这时候给我说为什么我的接口调的那么慢,我说我也不知道啊,然后他给我看了一下我的代码,咬牙切齿到,你一个查询要跟数据库交互10万次啊,查一次就算是10ms,你这也得超过10m了。随后我就一边被他吐槽一边听他说解决方案。

解决方案:

  不要循环查找数据库,和数据库交互是很慢的,我们选择的应该是先直接把这一大把数据全部查出来,然后交给内存处理这些数据就好了,内存处理数据是非常快的

举个栗子:

  现在我们有一个类目(类目名称:金属),该类目有一些属性(金属颜色,金属材料),属性会有属性值(颜色:黄绿蓝;材料:金银铁)。类目,属性,都是单独一张表来记录,属性值单表中记录属性的id类目和属性之间存在一张关联表。我们根据此关联关系需要做一个属性的分页,分页需要展示的数据为属性的基本数据+属性的类目+属性值

思路:

  OK!!!!!我们来理一下思路。基本数据就不在多说了,主要关注一下我们的分类info和属性值info。首先是属性值:我们查到属性分页(只含有基本数据)数据后,需要根据属性ids到属性值表中查询到属性值然后塞进返回值中返回。一下我想到的就是遍历ids,然后每一次遍历的时候拿着属性id在属性值表中查到这个属性值List然后添加到结果集合中,最后返回出去。很好想对吧,但是这就触及到了我们的这篇文章的问题了,假如有10万的属性,那我们就会10万次交互数据库,最后造成接口查询十分缓慢。

            这个方法我就是用了这个循环遍历查询数据库的方式,导致接口反应速度极慢

public List<ItemAttributePoInfo> processCategoryAndValues(List<ItemAttributePo> itemAttributePos) {List<ItemAttributePoInfo> results = AbstractModelConverter.convertListByBeanCopier(itemAttributePos, ItemAttributePoInfo.class);//最终需要被返回的结果集List<Long> attributeIds = results.stream().map(e -> e.getId()).collect(Collectors.toList());for (ItemAttributePoInfo result : results) {Long attributeId = result.getId();//属性idLambdaQueryWrapper<ItemAttributeValue> attributeValueWrapper = new LambdaQueryWrapper<>();attributeValueWrapper.eq(ItemAttributeValue::getAttributeId, attributeId);List<ItemAttributeValue> itemAttributeValueList = itemAttributeValueReadService.list(attributeValueWrapper);List<ItemAttributeValueInfo> itemAttributeValueInfos = AbstractModelConverter.convertList(itemAttributeValueList, ItemAttributeValueInfo.class);result.setItemAttributeValueInfos(itemAttributeValueInfos);}return results;}
}

正确做法:

  那么正确的做法是什么呢,就是我上文所说的我们应该根据属性ids一次性把所有的数据都查出来,然后我们根据属性id分组。分组成为一个Map<Long, List<属性值>>。这样我们最后直接循环结果集,将对应属性id作为map的key,从map中查到对应属性值的list塞入即可。下面的代码为正确做法。

public List<ItemAttributePoInfo> processCategoryAndValues(List<ItemAttributePo> itemAttributePos) {List<ItemAttributePoInfo> results = AbstractModelConverter.convertListByBeanCopier(itemAttributePos, ItemAttributePoInfo.class);List<Long> attributeIds = results.stream().map(e -> e.getId()).collect(Collectors.toList());//封装属性值信息LambdaQueryWrapper<ItemAttributeValue> attributeValueWrapper = new LambdaQueryWrapper<>();attributeValueWrapper.in(ItemAttributeValue::getAttributeId, attributeIds);List<ItemAttributeValue> itemAttributeValueList = itemAttributeValueReadService.list(attributeValueWrapper);Map<Long, List<ItemAttributeValue>> attributeValueMaps = itemAttributeValueList.stream().collect(Collectors.groupingBy(ItemAttributeValue::getAttributeId));if (CollectionUtils.isNotEmpty(itemAttributeValueList)) {for (ItemAttributePoInfo result : results) {List<ItemAttributeValue> attributeValueResult = attributeValueMaps.get(result.getId());if (CollectionUtils.isEmpty(attributeValueResult)) {result.setItemAttributeValueInfos(new ArrayList<>());} else {result.setItemAttributeValueInfos(AbstractModelConverter.convertList(attributeValueResult, ItemAttributeValueInfo.class));}}}return results;}

注:

  该方法可能对刚刚使用这个方法的同学不太友好,理解起来相对比较费力,大家要多看两遍,代码也很重要,理解其中的意思,该例子中的类目由于设计到关联表,使用起来可能理解难度会更大一些,先把属性值理解了再来看类目更容易一些。

附:

类目info:

    public List<ItemAttributePoInfo> processCategoryAndValues(List<ItemAttributePo> itemAttributePos) {List<ItemAttributePoInfo> results = AbstractModelConverter.convertListByBeanCopier(itemAttributePos, ItemAttributePoInfo.class);//最终需要被返回的结果集List<Long> attributeIds = results.stream().map(e -> e.getId()).collect(Collectors.toList());//封装类目信息LambdaQueryWrapper<ItemCategoryAttributeRel> categoryAttributeRelWrapper = new LambdaQueryWrapper<>();categoryAttributeRelWrapper.in(ItemCategoryAttributeRel::getAttributeId, attributeIds);List<ItemCategoryAttributeRel> itemCategoryAttributeRelList = itemCategoryAttributeRelReadService.list(categoryAttributeRelWrapper);if (CollectionUtils.isNotEmpty(itemCategoryAttributeRelList)) {List<Long> categoryIds = itemCategoryAttributeRelList.stream().map(e -> e.getCategoryId()).collect(Collectors.toList());Map<Long, List<ItemCategoryAttributeRel>> categoryAttributeMaps = itemCategoryAttributeRelList.stream().collect(Collectors.groupingBy(ItemCategoryAttributeRel::getAttributeId));List<ItemCategoryPo> categorise = itemCategoryPoReadService.findByIds(categoryIds);if (CollectionUtils.isNotEmpty(categorise)) {Map<Long, ItemCategoryPo> categoryMap = categorise.stream().collect(Collectors.toMap(e -> e.getId(), e -> e));for (ItemAttributePoInfo result : results) {List<ItemCategoryAttributeRel> itemCategoryAttributeRels = categoryAttributeMaps.get(result.getId());if (CollectionUtils.isNotEmpty(itemCategoryAttributeRels)) {List<ItemCategoryPo> categoryResult = itemCategoryAttributeRels.stream().map(ItemCategoryAttributeRel::getCategoryId).map(categoryMap::get).collect(Collectors.toList());if (CollectionUtils.isEmpty(categoryResult)) {result.setItemCategoryPoInfo(new ItemCategoryPoInfo());} else {result.setItemCategoryPoInfo(AbstractModelConverter.convertModelByBeanCopier(categoryResult.get(0), ItemCategoryPoInfo.class));}}}}}
}

http://www.yayakq.cn/news/635090/

相关文章:

  • 建站资源共享成都住建局官网平台登录
  • 中国建设工程造价管理系统网站百度服务
  • 域名年费多少网站建设公司网站建设服务
  • 健身会所网站模板北京朝阳区房价
  • 中时讯通信建设有限公司网站沈阳网站建设策划方案
  • 无锡电商网站旧房改造装修翻新
  • 帝国cms 网站地图插件oa软件定制开发
  • 做pc端网站咨询能源科技网站建设
  • 河北网站制作菏泽做网站建设找哪家好
  • 做推广哪个网站效果好建设官方网站的主要作用
  • 丽水市建设局网站贵阳网站建设方案推广
  • 求职网站怎么做网站实施过程
  • 网站建设 软件开发的公司排名衣服货源怎么找厂家拿
  • 1999年怎样建立企业网站wordpress自建站上可以买卖
  • 电商网站设计流程图wordpress前端会员中心开发教程
  • php mysql视频网站开发建设通相似网站
  • 菏泽机关建设网站微信的微网站模板下载
  • 小型 网站 源码金山郑州阳网站建设
  • 珠海做网站价格网站投放广告怎么做
  • 推荐企业手机网站建设游戏设计网站
  • 网站开发充值功能020网站建设合同范本
  • 怎么在各大网站做推广国内开源代码网站
  • 专业企业网站设计网络公司东莞网站优化找哪家
  • 网页与网站设计关于网站建设的案例分析
  • 东莞如何搭建网站建设烟酒网上商城
  • 购物网站开发教案做网站最快的编程语言
  • 网站的实现怎么写有没有网站做设计可以赚钱
  • 曲靖网站制作一条龙兼职做网站安全么
  • 网站怎么样建设wordpress 浮动窗口插件
  • 可信赖的网站建设公司dw制作网页的作业免费