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; } } 如何优化这段代码
时间: 2024-01-19 10:01:58 浏览: 73
<input>标记-JSP网页编程
根据代码的逻辑,可以将查询语句进行优化,减少查询次数,提高查询效率。可以将以下两个查询合并成一个:
```
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;
}
}
```
另外,可以考虑使用缓存来提高查询效率,如果用户数据不经常更新,可以将查询结果缓存到内存中,下一次查询时直接从内存中获取。
阅读全文