foreach这样用

在进行forEach循环时,需要通过当前实体编号查询另一服务的账号信息.下面给出了两种解决方案,先将所有的编号循环后add进定义好的DTO实体,根据此DTO实体中的编号信息查询账号信息,返回list后,将其存入Map中即可,通过foreach循环map将所有查询的信息拿出来后在进行set即可.

		// 定义storeNo
		List<String> storeNos = Lists.newArrayList();
		// 循环storeNo
		simpleOrderResultDTOIPage.getRecords().forEach(simpleOrderResultDTO -> {
			storeNos.add(simpleOrderResultDTO.getStoreNo());
		});
		StoreAccountInfoQueryDTO storeAccountInfo = new StoreAccountInfoQueryDTO();
		storeAccountInfo.setStoreAccountInfos(storeNos);
		R<List<StoreAccountInfo>> storeAccountInfoDTOs = remoteStoreInfoService.getStoreAccountInfoByNo(storeAccountInfo, SecurityConstants.FROM_IN);
		Map<String, List<StoreAccountInfo>> collect = storeAccountInfoDTOs.getData().stream().collect(Collectors.groupingBy(StoreAccountInfo::getStoreNo));
		simpleOrderResultDTOIPage.getRecords().forEach(simpleOrderResultDTO -> {
			StoreAccountInfo storeAccountInfo1 = collect.get(simpleOrderResultDTO.getStoreNo()).get(0);
			simpleOrderResultDTO.setAccountBank(storeAccountInfo1.getAccountBank());
			simpleOrderResultDTO.setAccountName(storeAccountInfo1.getAccountName());
			simpleOrderResultDTO.setAccountId(storeAccountInfo1.getAccountId());
	
		});
	@Select("<script> " +
		" select store_no as storeNo,account_id as accountId,account_name as accountName,account_bank as accountBank " +
		" from store_account_info as info where info.deleted =0 " +
		" <if test='storeAccountInfoQueryDTO.storeAccountInfos !=null and storeAccountInfoQueryDTO.storeAccountInfos.size() > 0 '>" +
		" and info.store_no in " +
		" <foreach item=\"storeNo\" collection=\"storeAccountInfoQueryDTO.storeAccountInfos\" open=\"(\" separator=\",\" close=\")\">" +
		" #{storeNo}" +
		" </foreach>" +
		"</if>" +
		" </script>")
	List<StoreAccountInfo> getStoreAccountInfoByNo(@Param("storeAccountInfoQueryDTO") StoreAccountInfoQueryDTO storeAccountInfoDTO);
@Data
@EqualsAndHashCode
@ApiModel
public class StoreAccountInfoQueryDTO {

	private List<String> storeAccountInfos;
}

最简单的实现方式是直接进行循环编号并进行查询,然后进行set,但这样对于数据库的操作的影响巨大

		StoreAccountInfo data = remoteStoreInfoService.getStoreAccountInfoByNo1(simpleOrderResultDTO.getStoreNo(), SecurityConstants.FROM_IN).getData();
		simpleOrderResultDTO.setAccountBank(data.getAccountBank());
		simpleOrderResultDTO.setAccountId(data.getAccountId());
		simpleOrderResultDTO.setAccountName(data.getAccountName());
	@Select("<script> " +
		" select store_no as storeNo,account_id as accountId,account_name as accountName,account_bank as accountBank " +
		" from store_account_info as info where info.deleted =0 and info.store_no=#{storeNo}" +
		" </script>")
	StoreAccountInfo getStoreAccountInfoByNo1(@Param("storeNo") String storeNo);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值