如何降低代码的复杂性?

bis0qfac  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(326)

我正在尝试通过dto创建一个搜索方法。我的意思是用户可以通过多个领域搜索产品。你能提出更好的方法吗?或者帮助降低这个问题的复杂性?

  1. public List<ProductDTO> search(SearchProductDTO productDTO) {
  2. Criteria criteria = null;
  3. String insensitive = "i";
  4. if (StringUtils.isNotBlank(productDTO.getName()))
  5. criteria = Criteria.where(NAME.getLabel()).regex(productDTO.getName(), insensitive);
  6. if (StringUtils.isNotBlank(productDTO.getDescription()))
  7. criteria = criteria != null ? criteria.and(DESCRIPTION.getLabel()).regex(productDTO.getDescription(), insensitive) : Criteria.where(DESCRIPTION.getLabel()).regex(productDTO.getDescription(), insensitive);
  8. if (productDTO.getPrice() != 0)
  9. criteria = criteria != null ? criteria.and(PRICE.getLabel()).is(productDTO.getPrice()) : Criteria.where(PRICE.getLabel()).is(productDTO.getPrice());
  10. if (StringUtils.isNotBlank(productDTO.getBrand()))
  11. criteria = criteria != null ? criteria.and(BRAND.getLabel()).regex(productDTO.getBrand(), insensitive) : Criteria.where(BRAND.getLabel()).regex(productDTO.getBrand(), insensitive);
  12. if (productDTO.getProductSize() != null)
  13. criteria = criteria != null ? criteria.and(SIZE.getLabel()).is(productDTO.getProductSize()) : Criteria.where(SIZE.getLabel()).is(productDTO.getProductSize());
  14. if (productDTO.getStockCount() != 0)
  15. criteria = criteria != null ? criteria.and(STOCK_COUNT.getLabel()).is(productDTO.getStockCount()) : Criteria.where(STOCK_COUNT.getLabel()).is(productDTO.getStockCount());
  16. if (StringUtils.isNotBlank(productDTO.getType()))
  17. criteria = criteria != null ? criteria.and(TYPE.getLabel()).regex(productDTO.getType(), insensitive) : Criteria.where(TYPE.getLabel()).regex(productDTO.getType(), insensitive);
  18. if (StringUtils.isNotBlank(productDTO.getColor()))
  19. criteria = criteria != null ? criteria.and(COLOR.getLabel()).regex(productDTO.getColor(), insensitive) : Criteria.where(COLOR.getLabel()).regex(productDTO.getColor(), insensitive);
  20. if (productDTO.getGender() != null)
  21. criteria = criteria != null ? criteria.and(GENDER.getLabel()).is(productDTO.getGender()) : Criteria.where(GENDER.getLabel()).is(productDTO.getGender());
  22. Pageable pageable = PageRequest.of(productDTO.getPage(), productDTO.getSize());
  23. Query query = new Query().with(pageable);
  24. query = criteria != null ? query.addCriteria(criteria).with(pageable) : query;
  25. List<Product> products = mongoTemplate.find(query, Product.class);
  26. return products.stream().map(productMapper::modelToDto).collect(Collectors.toList());
  27. }
5jvtdoz2

5jvtdoz21#

一个更好的方法是将空检查+where子句的追加+参数的赋值具体化到它自己的方法中。这样你的代码就更容易理解了。
您还可以 Package criteria类并添加例如“optionalwhere()”,它执行空检查,并且仅当参数有值时才附加where子句。
…不幸的是,我不知道为什么动态查询的问题,比如gui中的搜索,没有在api本身中解决。。。

wmvff8tz

wmvff8tz2#

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

  1. List<Criteria> criterias = new ArrayList<>();
  2. // collect
  3. if (StringUtils.isNotBlank(productDTO.getName()))
  4. criterias.add(Criteria.where(NAME.getLabel()).regex(productDTO.getName(), insensitive));
  5. if (StringUtils.isNotBlank(productDTO.getDescription()))
  6. criterias.add(Criteria.where(DESCRIPTION.getLabel()).regex(productDTO.getDescription(), insensitive));
  7. if (productDTO.getPrice() != 0)
  8. criterias.add(Criteria.where(PRICE.getLabel()).is(productDTO.getPrice()));
  9. // and so on ...
  10. // combine
  11. Criteria multiCriteria = new Criteria().andOperator(criterias.toArray(new Criteria[criterias.size()]));
  12. // use
  13. Pageable pageable = PageRequest.of(productDTO.getPage(), productDTO.getSize());
  14. Query query = new Query().with(pageable);
  15. query.addCriteria(multiCriteria);
展开查看全部

相关问题