【发布时间】:2021-01-08 21:40:58
【问题描述】:
我正在尝试为一个人实现跨多个属性的搜索功能。
这是模型。
@Entity
@NoArgsConstructor
@Getter
@Setter
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USER_ID")
private long id;
private String firstName;
private String surName;
private int age;
private Date DOB;
private String description;
private String highestEducationQualification;
private String occupation;
private String employer;
private String college;
private String school;
private String eyecolor;
private double weight;
private double height;
private String PPSnumber;
private boolean driversLicence;
private boolean provisionalLicence;
private String bankIBAN;
private long phoneNumber;
private char gender;
private String emailAddress;
private String websiteAddress;
private String homeAddress;
}
这是我的存储库。
@Repository
public interface PersonRepo extends JpaRepository<Person, Long>{
List<Person> searchByFirstNameContainingAllIgnoreCase(String firstName, Pageable page);
List<Person> searchBySurNameContainingAllIgnoreCase(String surName, Pageable page);
List<Person> searchByAge(int age, Pageable page);
List<Person> searchByDescriptionContainingAllIgnoreCase(String desc, Pageable page);
List<Person> searchByHighestEducationQualificationContainingAllIgnoreCase(String edu, Pageable page);
List<Person> searchByOccupationContainingAllIgnoreCase(String occ, Pageable page);
List<Person> searchByEmployerContainingAllIgnoreCase(String emp, Pageable page);
List<Person> searchByCollegeContainingAllIgnoreCase(String emp, Pageable page);
List<Person> searchBySchoolContainingAllIgnoreCase(String emp, Pageable page);
List<Person> searchByEyecolorContainingAllIgnoreCase(String eye, Pageable page);
List<Person> searchByWeight(double weight, Pageable page);
List<Person> searchByHeight(double height, Pageable page);
List<Person> searchByPPSnumberIgnoreCase(String emp, Pageable page);
List<Person> searchByDriversLicence(boolean emp, Pageable page);
List<Person> searchByProvisionalLicence(boolean emp, Pageable page);
List<Person> searchByBankIBANAllIgnoreCase(String emp, Pageable page);
List<Person> searchByPhoneNumber(long phone, Pageable page);
List<Person> searchByGender(char emp, Pageable page);
List<Person> searchByEmailAddressIgnoreCase(String emp, Pageable page);
List<Person> searchByWebsiteAddressContainingAllIgnoreCase(String emp, Pageable page);
List<Person> searchByHomeAddressContainingAllIgnoreCase(String emp, Pageable page);
}
服务功能。
class PersonService {
@Autowired
private PersonRepo personRepo;
@Override
public List<Person> searchByAllAttributes(String toSearch, int page, int quan, String sortBy, boolean ascending) {
int ageToSearch = 0;
try {
ageToSearch = Integer.parseInt(toSearch);
} catch (Exception e) {
}
double toSearchDouble = 0;
try {
toSearchDouble = Double.parseDouble(toSearch);
} catch (Exception e) {
}
long phoneToSearch = 0;
try {
phoneToSearch = Long.parseLong(toSearch);
} catch (Exception e) {
}
System.out.println(toSearchDouble);
List<Person> results;
Pageable firstPageWithTwoElements = PageRequest.of(page, quan, Sort.by("firstName").descending());
results = personRepo.searchByFirstNameContainingAllIgnoreCase(toSearch, firstPageWithTwoElements);
results.addAll(personRepo.searchBySurNameContainingAllIgnoreCase(toSearch,firstPageWithTwoElements));
results.addAll(personRepo.searchByAge(ageToSearch,firstPageWithTwoElements));
results.addAll(personRepo.searchByDescriptionContainingAllIgnoreCase(toSearch,firstPageWithTwoElements));
results.addAll(personRepo.searchByCollegeContainingAllIgnoreCase(toSearch, firstPageWithTwoElements));
results.addAll(personRepo.searchBySchoolContainingAllIgnoreCase(toSearch,firstPageWithTwoElements));
results.addAll(personRepo.searchByEmployerContainingAllIgnoreCase(toSearch,firstPageWithTwoElements));
results.addAll(personRepo.searchByOccupationContainingAllIgnoreCase(toSearch,firstPageWithTwoElements));
results.addAll(personRepo.searchByHighestEducationQualificationContainingAllIgnoreCase(toSearch,firstPageWithTwoElements));
results.addAll(personRepo.searchByEyecolorContainingAllIgnoreCase(toSearch,firstPageWithTwoElements));
results.addAll(personRepo.searchByWeight(toSearchDouble,firstPageWithTwoElements));
results.addAll(personRepo.searchByHeight(toSearchDouble,firstPageWithTwoElements));
results.addAll(personRepo.searchByPPSnumberIgnoreCase(toSearch,firstPageWithTwoElements));
//drivers and provisional
results.addAll(personRepo.searchByBankIBANAllIgnoreCase(toSearch,firstPageWithTwoElements));
results.addAll(personRepo.searchByPhoneNumber(phoneToSearch,firstPageWithTwoElements));
//gender
results.addAll(personRepo.searchByEmailAddressIgnoreCase(toSearch,firstPageWithTwoElements));
results.addAll(personRepo.searchByWebsiteAddressContainingAllIgnoreCase(toSearch,firstPageWithTwoElements));
results.addAll(personRepo.searchByHomeAddressContainingAllIgnoreCase(toSearch,firstPageWithTwoElements));
results = removeDuplicatePersons(results);
return results;
}
List<Person> removeDuplicatePersons(List<Person> toRemove){
List<Person> result = toRemove;
List<Person> listWithoutDuplicates = new ArrayList<>(
new HashSet<Person>(result));
return listWithoutDuplicates;
}
}
正如您将看到的,有一个硬编码的 Sort 对象,其中包含 firstName 和降序。每当我调用这个函数时,它都会返回一个随机排序的数据。排序不起作用。我努力对其进行硬编码以消除参数数据损坏的可能性,但即使是硬编码也不起作用。
toSearch 是一个字符串搜索查询。 Page 和 quan(数量)用于分页。分页有效,但排序无效。感谢您的帮助,如果您需要我更多地解释代码,请添加评论。
正如您可能想象的那样,还有一个控制器类。我也可以添加该代码,但它不会直接影响该代码的逻辑。控制器调用服务函数并将其作为 JSON 返回给 Web 应用程序。通过请求调用服务功能的 REST 控制器功能,我已经在 Postman 中调试了这两个功能。它以 JSON 格式将数据返回给 Postman,我在实现 Web 应用程序中也做了同样的事情,但数据没有排序。
您会注意到 Person 类模型上的 4 个注释。实体是为了持久化。 NoArgsConstructor 和 Getter 和 Setter 是 Lombok 的一部分,Lombok 是一个允许您省略 getter、setter、构造函数的包,它们是在编译时添加的。
【问题讨论】:
标签: java spring spring-boot hibernate spring-data-jpa