Based on what you need, I would do it
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); }
First of all, you should return List from EmployeeEntity . Secondly, it’s better to use an option than checking if it is null , and you can pass Java 8 Optional values obtained from optional RequestParam such as this:
@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); .... }
And it is very important to use the containsIgnoreCase function in the predicate: it is better than typical like , cause case insensitivity.
In my opinion, you should use this approach:
@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"; } }
look here .
Eliux
source share