假设你在Spring Repository类中有这个方法:
Page<SystemEntity> findByNameContainsAndFlagIsFalseOrFamilyContainsAndFlagIsFalseOrEmailContainsAndFlagIsFalse(String name, String family ,String email, Pageable pageable);
一个好的做法是将大查询拆分为多个部分,然后以这种方式使用它们:
Page<SystemEntity> findByNameContainsAndFlagIsFalse(String name, Pageable pageable);
Page<SystemEntity> findByFamilyContainsAndFlagIsFalse(String family, Pageable pageable);
Page<SystemEntity> findByEmailContainsAndFlagIsFals(String email, Pageable pageable);
default Page<SystemEntity> findByPrimaryLogicalCondition(String name, String family ,String email, Pageable pageable){
Page<SystemEntity> pageName = findByNameContainsAndFlagIsFalse(name, pageable);
Page<SystemEntity> pageFamily = findByFamilyContainsAndFlagIsFalse(family, pageable);
Page<SystemEntity> pageEmail = findByEmailContainsAndFlagIsFals(email, pageable);
List<SystemEntity> combinedResults = new ArrayList<>(pageName.getContent());
combinedResults.addAll(pageFamily.getContent());
combinedResults.addAll(pageEmail.getContent());
return new PageImpl<>(combinedResults, pageable, combinedResults.size());
}
这种方法允许您在需要时重复使用较小的方法,并使您的代码更具模块化和可维护性。