我需要列出所有用户,当我必须按以下方式过滤时:
通过使用类似的东西,我很容易做到,
Employee
id() firstName() lastName()
1 Victor More
2 Peter Gade
EmployeeDepartment(one(Employee) to one(Department))
employeeId departmentId
1 1
2 2
EmployeeAddress(one(Employee) to many(Address))
id employeeId doorNo streetName cityName
1 1 #12 South Lane Newyork
2 1 #13 North Lane Chicago
这是我的Controller类:
@RequestMapping(value = "/employees", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public PageResponse<BaseEmployeeDetail> getEmployees(
@RequestParam(value = "pageNumber", required = false) Integer pageNumber,
@RequestParam(value = "pageSize", required = false) Integer pageSize,
@RequestParam(value = "search") String search) {
if (pageNumber == null || pageNumber <= 0) {
pageNumber = 1;
}
if (pageSize == null || pageSize <= 0) {
pageSize = 10;
}
List<EmployeeSearchCriteria> params = new ArrayList<EmployeeSearchCriteria>();
if (search != null) {
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
Matcher matcher = pattern.matcher(search + ",");
while (matcher.find()) {
params.add(new EmployeeSearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3)));
}
}
EmployeeDetail employeeDetails = employeeService.getEmployees(params, pageNumber, pageSize);
.......
.......
return response;
}
这是我的实现类,
@Override
public EmployeeDetail getEmployees(List<EmployeeSearchCriteria> params,
Integer pageNumber, Integer pageSize) {
final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
final CriteriaQuery<CampaignDetail> query = builder.createQuery(User.class);
final Root r = query.from(Employee.class);
Join<Employee, EmployeeDepartment> employeeDepartment = employee.join("employeeDepartments",
JoinType.LEFT);
Join<EmployeeAddress, Address> address = r.join("address", JoinType.LEFT);
// isDataEntered or isPartialDataEntered
Predicate predicate = builder.conjunction();
for (final EmployeeSearchCriteria param : params) {
if (param.getOperation().equalsIgnoreCase(">")) {
predicate = builder.and(predicate,
builder.greaterThanOrEqualTo(r.get(param.getKey()), param.getValue().toString()));
} else if (param.getOperation().equalsIgnoreCase("<")) {
predicate = builder.and(predicate,
builder.lessThanOrEqualTo(r.get(param.getKey()), param.getValue().toString()));
} else if (param.getOperation().equalsIgnoreCase(":")) {
if (param.getKey().equals("firstName") || param.getKey().equals("lastName")) { //employees table
if (user.get(param.getKey()).getJavaType() == String.class) {
predicate = builder.and(predicate,
builder.like(user.get(param.getKey()), "%" + param.getValue() + "%"));
} else {
predicate = builder.and(predicate,
builder.equal(user.get(param.getKey()), param.getValue()));
}
} else if (param.getKey().equals("departmentId")) { // employee_departments table
if (employeeDepartment.get(param.getKey()).getJavaType() == String.class) {
predicate = builder.and(predicate,
builder.equal(employeeDepartment.get(param.getKey()), param.getValue()));
}
}
}
}
query.where(predicate);
Query query2 = entityManager.createQuery(query);
query2.setFirstResult((pageNumber - 1) * pageSize);
query2.setMaxResults(pageSize);
Long countResult = (long) entityManager.createQuery(query).getResultList().size();
EmployeeDetail employeeDetail = new EmployeeDetail();
employeeDetail.setTotalEmployeeCount(countResult);
employeeDetail.setEmployeeDetail(query2.getResultList());
return employeeDetail;
}
这是我的终点:
http://localhost:8080/employees?search=firstName:Peter,lastName:Gade,departmentId:4
但是,当我必须使用以下要求进行过滤时,我很难找到结果,我怎么能这样做,有人可以帮我这个,下面的过滤器是可选的。,
1. Filter By FirstName()
2. Filter By LastName()
3. Filter By Department()
4. Filter By
(i) AddressExist (Employees who hold addres row)
(ii) AddressNotExist (Employees who doesnt hold address)