【问题标题】:How can I reduce the Complexity of this code?如何降低此代码的复杂性?
【发布时间】:2020-11-19 15:04:13
【问题描述】:

我正在尝试通过 DTO 创建搜索方法。我的意思是用户可以通过多一个字段搜索产品。您能否为此提出任何更好的方法或帮助降低此方法的复杂性?

public List<ProductDTO> search(SearchProductDTO productDTO) {
    Criteria criteria = null;
    String insensitive = "i";
    if (StringUtils.isNotBlank(productDTO.getName()))
        criteria = Criteria.where(NAME.getLabel()).regex(productDTO.getName(), insensitive);
    if (StringUtils.isNotBlank(productDTO.getDescription()))
        criteria = criteria != null ? criteria.and(DESCRIPTION.getLabel()).regex(productDTO.getDescription(), insensitive) : Criteria.where(DESCRIPTION.getLabel()).regex(productDTO.getDescription(), insensitive);
    if (productDTO.getPrice() != 0)
        criteria = criteria != null ? criteria.and(PRICE.getLabel()).is(productDTO.getPrice()) : Criteria.where(PRICE.getLabel()).is(productDTO.getPrice());
    if (StringUtils.isNotBlank(productDTO.getBrand()))
        criteria = criteria != null ? criteria.and(BRAND.getLabel()).regex(productDTO.getBrand(), insensitive) : Criteria.where(BRAND.getLabel()).regex(productDTO.getBrand(), insensitive);
    if (productDTO.getProductSize() != null)
        criteria = criteria != null ? criteria.and(SIZE.getLabel()).is(productDTO.getProductSize()) : Criteria.where(SIZE.getLabel()).is(productDTO.getProductSize());
    if (productDTO.getStockCount() != 0)
        criteria = criteria != null ? criteria.and(STOCK_COUNT.getLabel()).is(productDTO.getStockCount()) : Criteria.where(STOCK_COUNT.getLabel()).is(productDTO.getStockCount());
    if (StringUtils.isNotBlank(productDTO.getType()))
        criteria = criteria != null ? criteria.and(TYPE.getLabel()).regex(productDTO.getType(), insensitive) : Criteria.where(TYPE.getLabel()).regex(productDTO.getType(), insensitive);
    if (StringUtils.isNotBlank(productDTO.getColor()))
        criteria = criteria != null ? criteria.and(COLOR.getLabel()).regex(productDTO.getColor(), insensitive) : Criteria.where(COLOR.getLabel()).regex(productDTO.getColor(), insensitive);
    if (productDTO.getGender() != null)
        criteria = criteria != null ? criteria.and(GENDER.getLabel()).is(productDTO.getGender()) : Criteria.where(GENDER.getLabel()).is(productDTO.getGender());
    Pageable pageable = PageRequest.of(productDTO.getPage(), productDTO.getSize());
    Query query = new Query().with(pageable);
    query = criteria != null ? query.addCriteria(criteria).with(pageable) : query;
    List<Product> products = mongoTemplate.find(query, Product.class);
    return products.stream().map(productMapper::modelToDto).collect(Collectors.toList());
}

【问题讨论】:

  • 格式化您的代码并分享一些细节
  • 我正在尝试通过 DTO 创建搜索方法。我不能这样做。这就是我写意大利面条代码的原因((
  • 您的代码以最佳方式正确编写。只需将圆括号放在下面以便更好地阅读。 if (StringUtils.isNotBlank(productDTO.getDescription())) criteria = (criteria != null ? criteria.and(DESCRIPTION.getLabel()).regex(productDTO.getDescription(), insensitive) : Criteria.where(DESCRIPTION.getLabel ()).regex(productDTO.getDescription(), insensitive));

标签: java spring mongodb spring-boot mongodb-query


【解决方案1】:

您可以通过将标准动态收集到列表中并将此列表组合成单个“多”标准来摆脱空检查

List<Criteria> criterias = new ArrayList<>();

// collect
if (StringUtils.isNotBlank(productDTO.getName()))
    criterias.add(Criteria.where(NAME.getLabel()).regex(productDTO.getName(), insensitive));
if (StringUtils.isNotBlank(productDTO.getDescription()))
    criterias.add(Criteria.where(DESCRIPTION.getLabel()).regex(productDTO.getDescription(), insensitive));
if (productDTO.getPrice() != 0)
    criterias.add(Criteria.where(PRICE.getLabel()).is(productDTO.getPrice()));
// and so on ...

// combine
Criteria multiCriteria = new Criteria().andOperator(criterias.toArray(new Criteria[criterias.size()]));

// use
Pageable pageable = PageRequest.of(productDTO.getPage(), productDTO.getSize());
Query query = new Query().with(pageable);
query.addCriteria(multiCriteria);

【讨论】:

    【解决方案2】:

    更好的做法是将 null 检查 + 附加 where 子句 + 分配参数到它自己的方法中。这样你的代码会更容易理解。

    您还可以包装 Criteria 类并添加例如“optionalWhere()”,它会执行 null 检查并仅在参数具有值时附加 where 子句。

    ...不幸的是,我不知道为什么动态查询的问题,如从 GUI 搜索,在 APi 本身中没有解决...

    【讨论】:

      猜你喜欢
      • 2022-12-07
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多