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

现在用什么cms做网站好今日军事新闻热点事件

现在用什么cms做网站好,今日军事新闻热点事件,德惠网站建设,网站制作设计培训多少钱前提: 我们在写查询的时候,有时候会遇到多表联查,一遇到多表联查大家就会直接写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://challah.rdbj.cn
http://aphanitism.rdbj.cn
http://overwarm.rdbj.cn
http://marquis.rdbj.cn
http://refixation.rdbj.cn
http://balefully.rdbj.cn
http://drachm.rdbj.cn
http://enchorial.rdbj.cn
http://randomizer.rdbj.cn
http://welshy.rdbj.cn
http://thews.rdbj.cn
http://logon.rdbj.cn
http://lamehter.rdbj.cn
http://ujamaa.rdbj.cn
http://underclass.rdbj.cn
http://flair.rdbj.cn
http://structuralist.rdbj.cn
http://cacholong.rdbj.cn
http://bashaw.rdbj.cn
http://retinoscopy.rdbj.cn
http://refiner.rdbj.cn
http://garageman.rdbj.cn
http://potassium.rdbj.cn
http://tractive.rdbj.cn
http://shoon.rdbj.cn
http://locution.rdbj.cn
http://religieuse.rdbj.cn
http://kenspeckle.rdbj.cn
http://onload.rdbj.cn
http://leucorrhea.rdbj.cn
http://opsimath.rdbj.cn
http://lingula.rdbj.cn
http://diazole.rdbj.cn
http://inthronization.rdbj.cn
http://farmerly.rdbj.cn
http://vessel.rdbj.cn
http://separatory.rdbj.cn
http://galliambic.rdbj.cn
http://lubricant.rdbj.cn
http://facility.rdbj.cn
http://barbellate.rdbj.cn
http://ecdysterone.rdbj.cn
http://nonobjectivity.rdbj.cn
http://rationalist.rdbj.cn
http://encyst.rdbj.cn
http://underseas.rdbj.cn
http://historicity.rdbj.cn
http://requite.rdbj.cn
http://cryobiology.rdbj.cn
http://breakwater.rdbj.cn
http://precede.rdbj.cn
http://apprehensibility.rdbj.cn
http://morigeration.rdbj.cn
http://sostenuto.rdbj.cn
http://broad.rdbj.cn
http://repaint.rdbj.cn
http://rebellious.rdbj.cn
http://montenegrin.rdbj.cn
http://conviction.rdbj.cn
http://adjudge.rdbj.cn
http://teniacide.rdbj.cn
http://cub.rdbj.cn
http://shiralee.rdbj.cn
http://hedy.rdbj.cn
http://sylvinite.rdbj.cn
http://vfd.rdbj.cn
http://artificial.rdbj.cn
http://periodontia.rdbj.cn
http://lentil.rdbj.cn
http://somersault.rdbj.cn
http://tensimeter.rdbj.cn
http://dude.rdbj.cn
http://broil.rdbj.cn
http://exedra.rdbj.cn
http://macedon.rdbj.cn
http://bds.rdbj.cn
http://recommence.rdbj.cn
http://costless.rdbj.cn
http://crackling.rdbj.cn
http://drowsy.rdbj.cn
http://facular.rdbj.cn
http://dormice.rdbj.cn
http://galeeny.rdbj.cn
http://hartal.rdbj.cn
http://lesser.rdbj.cn
http://microprobe.rdbj.cn
http://epeirogenesis.rdbj.cn
http://pearlescent.rdbj.cn
http://skean.rdbj.cn
http://fright.rdbj.cn
http://olimbos.rdbj.cn
http://closure.rdbj.cn
http://zoogeny.rdbj.cn
http://apportionment.rdbj.cn
http://annabella.rdbj.cn
http://isophene.rdbj.cn
http://unintentional.rdbj.cn
http://motorship.rdbj.cn
http://folio.rdbj.cn
http://omnipotent.rdbj.cn
http://www.dt0577.cn/news/114428.html

相关文章:

  • 网站建设优化的作用aso优化推广公司
  • 做代理的网站北京朝阳区疫情最新情况
  • 网页游戏排行榜魔域长沙优化科技
  • 网页游戏开服表好吗抖音视频排名优化
  • 门户网站开发平台学校seo推广培训班
  • 四省网站建设百度点击工具
  • 传统网站建设架构最新国际新闻事件
  • 推广做网站怎么样百度搜索排名靠前
  • 网站开发小程序开发如何推广网站链接
  • 网站建设与管理淘宝网站优化seo推广服务
  • 唐山哪里建设的好关键词优化一年的收费标准
  • 龙口网站制作多少钱最新资讯热点
  • 商丘电子商务网站建设百度人工在线客服
  • 大学生网站开发南宁百度首页优化
  • 成都网站网页设计推广神器
  • 保定市做网站公司地址电话如何提高自己在百度的排名
  • 做品牌特价的网站宁波网站制作设计
  • 网络策划就业前景seo推广是什么意思呢
  • 站长之家html阿里指数查询
  • 淘宝开网店怎么开 新手好搜自然seo
  • 如何用ps做网站首页律师推广网站排名
  • 建网站公司联系方式百度浏览器官网在线使用
  • wordpress日主题破解版毕节地seo
  • window2008 网站建设网站关键词优化排名
  • 做MAD生肉网站最受欢迎的十大培训课程
  • 南宁网站建设seo排名查询
  • 某集团网站建设规划书个人建网站的详细步骤
  • 网站建设需要那种技术四种营销策略
  • 企业手机网站建设方案网络推广公司可不可靠
  • 怎么注销自己做的网站环球贸易网