此基礎上,你需要,我會做到這一點
public List<EmployeeEntity> getEmployees(Optional<String> firstName, Optional<String> lastName)
{
BooleanExpression queryPredicate = QEmployeeEntity.employeeEntity.firstName.containsIgnoreCase(firstName.orElse("")).and(QEmployeeEntity.employeeEntity.lastName.containsIgnoreCase(lastName.orElse("")));
return empployeeDAO.findAll(queryPredicate);
}
,首先你應該返回EmployeeEntity
的List
什麼。其次,它能夠更好地使用可選的比,如果其null
檢查,你可以通過從Java可選RequestParam
的像這樣獲得8的Optional
值:
@RequestMapping(value = "/query", method = RequestMethod.GET)
public ModelAndView queryEmployee(@RequestParam(value = "firstName", required = false) Optional<String> firstName, @RequestParam(value = "lastName", required = false) Optional<String> lastName)
{
List<EmployeeEntity> result = getEmployees(firstName, lastName);
....
}
而且很重要的是要使用containsIgnoreCase
功能謂語:比典型的like
更好導致其不區分大小寫。
在我看來,你應該使用一些方法是這樣的:
@Controller
class UserController {
@Autowired UserRepository repository;
@RequestMapping(value = "/", method = RequestMethod.GET)
String index(Model model, @QuerydslPredicate(root = User.class) Predicate predicate,
Pageable pageable, @RequestParam MultiValueMap<String, String> parameters) {
model.addAttribute("users", repository.findAll(predicate, pageable));
return "index";
}
}
看看它在here。
但如果我們加入?你能幫我嗎?http://stackoverflow.com/questions/24792778/dynamic-search-by-criteria – Youssef