if (!CollectionUtils.isEmpty(empIdList)){ List<EmpDTO> empList = userFeignService.getEmpByIds(empIdList); if (!CollectionUtils.isEmpty(empList)){ ListUtil.matchAndSet(userDetailList, UserDetailDTO::getEmpId, empList, EmpDTO::getId, UserDetailDTO::setEmpDTO); } }
时间: 2023-05-23 10:04:25 浏览: 129
A: 这段代码是对一个 empIdList 进行非空判断,如果不为空则调用 userFeignService 的 getEmpByIds 方法来获取一组 EmpDTO 对象,并进行非空判断。如果也不为空,则调用 ListUtil 匹配函数,将获取到的 EmpDTO 对象与 userDetailList 中的 UserDetailDTO 对象进行匹配,并将匹配到的 EmpDTO 对象设置到 UserDetailDTO 对象中的 EmpDTO 属性中。最终返回处理后的 userDetailList 列表。
相关问题
if (!StringUtil.isEmpty(idCard)) { LambdaQueryWrapper<UserExtend>queryWrapper=new LambdaQueryWrapper<>(); queryWrapper.eq(UserExtend::getIdcard,idCard); List<UserExtend> userList = userExtendMapper.selectList(queryWrapper); if (!CollectionUtils.isEmpty(userList)) { UserExtend userExtend = userList.get(0); String userId = userExtend.getUserId(); userExtend = queryExtendByUserId(userId); List<UserBase> userBases = userBaseMapper.selectUserBaseList(userId); if (!CollectionUtils.isEmpty(userBases)) { userExtend.setUserBase(userBases.get(0)); } return userExtend; } } 如何优化这段代码
根据代码的逻辑,可以将查询语句进行优化,减少查询次数,提高查询效率。可以将以下两个查询合并成一个:
```
LambdaQueryWrapper<UserExtend>queryWrapper=new LambdaQueryWrapper<>();
queryWrapper.eq(UserExtend::getIdcard,idCard);
List<UserExtend> userList = userExtendMapper.selectList(queryWrapper);
```
优化后的代码如下:
```
if (!StringUtil.isEmpty(idCard)) {
LambdaQueryWrapper<UserExtend> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserExtend::getIdcard, idCard);
List<UserExtend> userList = userExtendMapper.selectList(queryWrapper);
if (!CollectionUtils.isEmpty(userList)) {
UserExtend userExtend = userList.get(0);
String userId = userExtend.getUserId();
userExtend = queryExtendByUserId(userId);
List<UserBase> userBases = userBaseMapper.selectUserBaseList(userId);
if (!CollectionUtils.isEmpty(userBases)) {
userExtend.setUserBase(userBases.get(0));
}
return userExtend;
}
}
```
另外,可以考虑使用缓存来提高查询效率,如果用户数据不经常更新,可以将查询结果缓存到内存中,下一次查询时直接从内存中获取。
for (DatabaseType d : dataTypes) { DatabaseContextHolder.setDatabaseType(d); List<String> bankPaidOutRefNbrs = unionLoanMapper.getCustPaidOutRefNbrsUnionLoan(); if(!CollectionUtils.isEmpty(bankPaidOutRefNbrs)){ DatabaseContextHolder.setDatabaseType(d); List<PaidOutRemainBalVo> unionBankBals = unionLoanMapper.getUnionBalByRefNbrs(bankPaidOutRefNbrs); List<String> refNbrs = unionBankBals.stream().map(p -> p.getRefNbr()).collect(Collectors.toList()); DatabaseContextHolder.setDatabaseType(d); List<String> waiveRefNbrs = new ArrayList<>(); if (!CollectionUtils.isEmpty(refNbrs)){ waiveRefNbrs = unionLoanMapper.getUnionWaiveRefNbr(refNbrs); } Iterator<PaidOutRemainBalVo> iterator = unionBankBals.iterator(); while (iterator.hasNext()) { PaidOutRemainBalVo vo = iterator.next(); //排除豁免未入账 if (!CollectionUtils.isEmpty(waiveRefNbrs)) { if (waiveRefNbrs.contains(vo.getRefNbr())) { iterator.remove(); continue; } } //去除三峡本金为负数的告警 if (vo.getCoopId() != null && vo.getCoopId().contains("SXBANK") && vo.getPrincipalBal().compareTo(BigDecimal.ZERO) < 0) { iterator.remove(); } } if (!CollectionUtils.isEmpty(unionBankBals)){ unionBalsAll.addAll(unionBankBals); } } } 用多线程实现该循环
可以使用Python内置的`concurrent.futures`模块中的`ThreadPoolExecutor`来实现多线程循环。具体代码如下:
```python
from concurrent.futures import ThreadPoolExecutor
from functools import partial
def process_data(d, data):
DatabaseContextHolder.setDatabaseType(d)
bankPaidOutRefNbrs = unionLoanMapper.getCustPaidOutRefNbrsUnionLoan()
if not bankPaidOutRefNbrs:
return []
DatabaseContextHolder.setDatabaseType(d)
unionBankBals = unionLoanMapper.getUnionBalByRefNbrs(bankPaidOutRefNbrs)
refNbrs = [p.getRefNbr() for p in unionBankBals]
DatabaseContextHolder.setDatabaseType(d)
waiveRefNbrs = unionLoanMapper.getUnionWaiveRefNbr(refNbrs) if refNbrs else []
result = []
for vo in unionBankBals:
if waiveRefNbrs and vo.getRefNbr() in waiveRefNbrs:
continue
if vo.getCoopId() and "SXBANK" in vo.getCoopId() and vo.getPrincipalBal() < 0:
continue
result.append(vo)
return result
dataTypes = [...]
with ThreadPoolExecutor(max_workers=2) as executor:
func = partial(process_data, data=dataTypes)
results = executor.map(func, dataTypes)
unionBalsAll = []
for result in results:
if result:
unionBalsAll.extend(result)
```
以上代码中,我们将原来的循环拆分成了一个`process_data`函数,并使用`partial`函数将`dataTypes`作为参数传递给该函数。接着,创建一个`ThreadPoolExecutor`实例,设置最大工作线程数为2。使用`map`方法将`process_data`函数提交给线程池,并传入`dataTypes`参数,得到一个生成器对象`results`。最后,遍历`results`并将结果存入`unionBalsAll`中。这样就实现了多线程循环。
阅读全文