场景还原:
由于业务调整,现有有一个新的需求改动,在之前返回的VO的基础上增加大概10个返回字段,在这里不讨论这次业务调整的合理性,仅讨论在已经调整的情况下,如何完成需求。这里在改之前的业务大概是需要用到A,B,C,D,E五张表,核心表是A,然后通过A.b_id与A.e_id与B,E表产生关联,再通过B,E表中的某些字段与C,D表进行关联。现在需要新返回的字段是D,F,G产生关联之后才能产生的。此外这个查询还需要对A上的check_a,check_b,check_c,check_status字段进行返回,通过当前用户的check权限匹配对应的字段是否为空同时上一级是否已经check,所以这里需要动态的生成SQL。
需求实现
刚刚接触业务时想到使用Mybatis的<if>标签,但是在权限判断部分如果使用在xml中写动态SQL进行逻辑的判断,是比较复杂的,同时还要考虑到后期对应判断条件进行修改的情况,再结合每次返回的数据在A中都只会查到一次,故采用了第二种方式:对应A的单表部分使用Mybatis-Plus的Lambda表达式进行查询,再根据返回的b_id与e_id当做参数进行第二次的多表关联查询。大致逻辑如下:
@Override
public IPage<InspectionAuditInfoVO> page(PageBeanSearch<SearchCriteria> pageBeanSearch) {
// 获取当前用户的审批角色配置
Map<String, AuditConfiguration> auditNodes = auditConfigurationService.getAuditNodes();
if (auditNodes == null || auditNodes.isEmpty()) {
return new Page<>();
}
// 构建查询条件:当前审批节点为用户的待审批列表,且前置审批已完成
LambdaQueryWrapper<BusinessEntity> queryWrapper = loadQueryWrapper(auditNodes);
// 根据搜索条件追加过滤逻辑
queryWrapper.eq(Objects.nonNull(pageBeanSearch.getQuery().getCriteriaField()), BusinessEntity::getEntityField, pageBeanSearch.getQuery().getCriteriaValue());
queryWrapper.ge(Objects.nonNull(pageBeanSearch.getQuery().getStartTime()), BusinessEntity::getCreateTime, pageBeanSearch.getQuery().getStartTime());
queryWrapper.le(Objects.nonNull(pageBeanSearch.getQuery().getEndTime()), BusinessEntity::getCreateTime, pageBeanSearch.getQuery().getEndTime());
Page<BusinessEntity> rawPage = businessEntityMapper.selectPage(new Page<>(pageBeanSearch.getCurrentPage(), pageBeanSearch.getPageSize()), queryWrapper);
return rawPage.convert(this::convertEntityToVO);
}
这里还可以定义一个接口进行逻辑的封装传递
private LambdaQueryWrapper<BusinessEntity> loadQueryWrapper(Map<String, AuditConfiguration> auditConfigurations) {
LambdaQueryWrapper<BusinessEntity> queryWrapper = new LambdaQueryWrapper<>();
boolean firstCondition = false;
if (auditConfigurations.containsKey("P1")) {
queryWrapper.isNull(BusinessEntity::getFirstApproverId);
queryWrapper.eq(BusinessEntity::getApprovalStatus, 1); // 一级审批未审批
firstCondition = true;
}
if (auditConfigurations.containsKey("P2")) {
if (firstCondition) {
queryWrapper.or(w ->
w.isNotNull(BusinessEntity::getFirstApproverId)
.isNull(BusinessEntity::getSecondApproverId)
.eq(BusinessEntity::getApprovalStatus, 2)
); // 如果前面已经有条件了,添加 OR
} else {
queryWrapper.isNotNull(BusinessEntity::getFirstApproverId)
.isNull(BusinessEntity::getSecondApproverId)
.eq(BusinessEntity::getApprovalStatus, 2);
}
firstCondition = true;
}
if (auditConfigurations.containsKey("P3")) {
if (firstCondition) {
queryWrapper.or(v -> v.isNotNull(BusinessEntity::getSecondApproverId)
.isNotNull(BusinessEntity::getSecondApproverId)
.isNull(BusinessEntity::getThirdApproverId))
.eq(BusinessEntity::getApprovalStatus, 3);
} else {
queryWrapper.isNotNull(BusinessEntity::getSecondApproverId)
.isNotNull(BusinessEntity::getSecondApproverId)
.isNull(BusinessEntity::getThirdApproverId)
.eq(BusinessEntity::getApprovalStatus, 3);
}
}
return queryWrapper;
}
private AuditInfoVO convertEntityToVO(BusinessEntity businessEntity) {
AuditInfoVO info = BeanUtilExt.copyBean(businessEntity, new AuditInfoVO());
// 获取所有的标准名称以及编码
Map<String, StandardNameCodeVO> standardMap = standardService.getStandardNameAndCode().stream().collect(Collectors.toMap(StandardNameCodeVO::getStdId, Function.identity()));
// 实体检验结果
List<EntityResultVO> entityResultList = entityResultMapper.selectDetailByEntityId(businessEntity.getId());
entityResultList.forEach(v -> {
StandardEntity standardEntity = standardEntityMapper.selectById(v.getStandardEntityId());
if (standardEntity != null) {
v.setStandard(standardMap.get(standardEntity.getStandardId()));
}
});
info.setResult(entityResultList);
return info;
}
在考虑新业务的调整下,由于之前的关联查询返回的结果是一个List而且是返回VO的一个字段,无法对其进行扩展然后将需要的数据进行返回,且如果在原有关联查询的基础上进行扩展,表的数量会变成六表关联查询,是极其不推荐的,不仅影响性能(可能要造成索引失效)还会增加业务复杂度,造成后期维护的困难。
所以在这里就需要巧用"外挂"思想在不破坏原有代码结构的基础上进行字段的新增,同时还可以实现Lambda与原始SQL查询的结合
简单的思想就是在之前的业务逻辑之后,在拿到VO后,再对VO中的新增加字段部分进行添加,这里有前提是第二次的关联查询比如这里是DFG关联,是不能影响上一次查询的结果的,尽量避免与之前的字段进行冲突,实现逻辑如下:
// 获取详情额外信息
AuditInfoVO extraInfo = businessEntityMapper.selectExtraDetailById(businessEntity.getId());
// 设置额外信息
BeanUtilExt.copyProperties(extraInfo, info, getNullPropertyNames(extraInfo));