Reputation: 460
I'm using spring data jpa and am writing a search function. In my model include firstName and lastName. I want my search dialog to do a search on both firstName and lastName. For example, firstName is Alen and lastName is Lee if I enter in the search box is Alen or Lee then spring still find the correct value. Is there any way to help me achieve that in spring data jpa?
EmployeeDao
package com.baotrung.springcrudjpafull.dao;
import com.baotrung.springcrudjpafull.model.Employee;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EmployeeDao extends CrudRepository<Employee,Integer> {
Employee findAllByFirstNameAndLastName(String firstName,String lastName);
}
EmployeeController
@RequestMapping(value = "/searchEmployee", method = RequestMethod.GET)
public String getEmployeeByName(@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName, ModelMap modelMap) {
Employee employee = servicesDao.findAllByFirstNameAndLastName(firstName,lastName);
modelMap.addAttribute("employees", employee);
return "Employee";
}
However in jsp I do not know how to pass the parameter to be able to call it. If using , only one parameter is used.So how can I find multiple fields in a search box in spring.Please help
Upvotes: 1
Views: 21200
Reputation: 31
Check this out:
In repo:
@Repository
public interface EmployeeDao extends JpaRepository<Employee, Integer> {
@Query("select e from Employee e where lower(e.firstName) like lower(concat('%', :search, '%')) " +
"or lower(e.lastName) like lower(concat('%', :search, '%'))")
List<Employee> findByFirstNameOrLastName(@Param("search") String search);
}
In controller:
@RequestMapping("/searchEmployee", method = RequestMethod.GET)
public String getEmployeeByName(@RequestParam(name = "search") String search, Model model) {
Employee employee = servicesDao.findAllByFirstNameAndLastName(search);
model.addAttribute("employees", employee);
return "Employee";
}
And in jsp should be similar as I did in thymeleaf:
<form action="#" th:action="@{search}" method="get" class="input-group">
<input class="form-control form-control-dark" type="text" placeholder="Search" name="search" aria-label="Search">
<div class="input-group-append">
<button class="btn btn-outline-light" value="Search">Search</button>
</div>
</form>
Upvotes: 3
Reputation: 124
If you want to use GET method, the parameters should be @PathVaraible instead of @RequestParam and you have to call method with /searchEmployee/Alen/Lee
And the controller should be;
@RequestMapping(value = "/searchEmployee/{firstname}/{lastname}", method = RequestMethod.GET)
public String getEmployeeByName(@PathVariable("firstName") String firstName, @PathVariable("lastName") String lastName, ModelMap modelMap) {
List<Employee> employee = servicesDao.findAllByFirstNameAndLastName(firstName,lastName);
modelMap.addAttribute("employees", employee);
return "employee";
}
If you want to the user can type to search value, you need to create a html form in jsp and submit your form /searchEmployee
uri and also controller should be
@RequestMapping(value = "/searchEmployee", method = RequestMethod.POST)
public String getEmployeeByName(@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName, ModelMap modelMap) {
List<Employee> employee = servicesDao.findAllByFirstNameAndLastName(firstName,lastName);
modelMap.addAttribute("employees", employee);
return "employee";
}
And your controller method should be return jsp file name without suffix. For ex. if your jsp file name employee.jsp
, method should be return "employee";
And i think the repository should be like;
@Repository
public interface EmployeeDao extends CrudRepository<Employee,Integer> {
List<Employee> findAllByFirstNameAndLastName(String firstName,String lastName);
}
But this depends on to your Employee data model. I mean if the firstname and lastname have unique constraint with both, you can return just Employee object as well
Upvotes: 0
Reputation: 2568
Try this
findAllByFirstNameLikeOrLastNameLike
Or
findAllByFirstNameContainingOrLastNameContaining
The first one will produce a request with like
comparation, and the second one will wrap parameters into %%
Upvotes: 3