mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-04-24 11:22:49 +00:00
Query Tunned for the Search Functionality
Signed-off-by: Perumal Raj A <perumalraj024@gmail.com>
This commit is contained in:
parent
1615fda9b2
commit
2ab1cef91f
3 changed files with 33 additions and 28 deletions
|
@ -92,27 +92,33 @@ class OwnerController {
|
|||
@GetMapping("/owners")
|
||||
public String processFindForm(@RequestParam(defaultValue = "1") int page, Owner owner, BindingResult result,
|
||||
Model model) {
|
||||
// allow parameterless GET request for /owners to return all records
|
||||
if (owner.getLastName() == null) {
|
||||
owner.setLastName(""); // empty string signifies broadest possible search
|
||||
try {
|
||||
// allow parameterless GET request for /owners to return all records
|
||||
if (owner.getLastName() == null) {
|
||||
owner.setLastName(""); // empty string signifies broadest possible search
|
||||
}
|
||||
|
||||
// find owners by last name
|
||||
Page<Owner> ownersResults = findPaginatedForOwnersLastName(page, owner.getLastName());
|
||||
if (ownersResults.isEmpty()) {
|
||||
// no owners found
|
||||
result.rejectValue("lastName", "notFound", "not found");
|
||||
return "owners/findOwners";
|
||||
}
|
||||
|
||||
if (ownersResults.getTotalElements() == 1) {
|
||||
// 1 owner found
|
||||
owner = ownersResults.iterator().next();
|
||||
return "redirect:/owners/" + owner.getId();
|
||||
}
|
||||
// multiple owners found
|
||||
return addPaginationModel(page, model, ownersResults);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();// print exception to console or log in file
|
||||
return "error"; // redirect to generic error page
|
||||
}
|
||||
|
||||
// find owners by last name
|
||||
Page<Owner> ownersResults = findPaginatedForOwnersName(page, owner.getLastName());
|
||||
if (ownersResults.isEmpty()) {
|
||||
// no owners found
|
||||
result.rejectValue("lastName", "notFound", "not found");
|
||||
return "owners/findOwners";
|
||||
}
|
||||
|
||||
if (ownersResults.getTotalElements() == 1) {
|
||||
// 1 owner found
|
||||
owner = ownersResults.iterator().next();
|
||||
return "redirect:/owners/" + owner.getId();
|
||||
}
|
||||
|
||||
// multiple owners found
|
||||
return addPaginationModel(page, model, ownersResults);
|
||||
}
|
||||
|
||||
private String addPaginationModel(int page, Model model, Page<Owner> paginated) {
|
||||
|
@ -124,10 +130,10 @@ class OwnerController {
|
|||
return "owners/ownersList";
|
||||
}
|
||||
|
||||
private Page<Owner> findPaginatedForOwnersName(int page, String lastname) {
|
||||
private Page<Owner> findPaginatedForOwnersLastName(int page, String lastname) {
|
||||
int pageSize = 5;
|
||||
Pageable pageable = PageRequest.of(page - 1, pageSize);
|
||||
return owners.findByNameContaining(lastname, pageable);
|
||||
return owners.findByLastNameStartingWith(lastname, pageable);
|
||||
}
|
||||
|
||||
@GetMapping("/owners/{ownerId}/edit")
|
||||
|
|
|
@ -27,8 +27,6 @@ import org.springframework.data.jpa.repository.Query;
|
|||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Repository class for <code>Owner</code> domain objects All method names are compliant
|
||||
* with Spring Data naming conventions so this interface can easily be extended for Spring
|
||||
|
@ -58,9 +56,8 @@ public interface OwnerRepository extends JpaRepository<Owner, Integer> {
|
|||
* found)
|
||||
*/
|
||||
|
||||
@Query("SELECT o FROM Owner o " + "WHERE LOWER(o.firstName) LIKE LOWER(CONCAT('%', :namePart, '%')) "
|
||||
+ "OR LOWER(o.lastName) LIKE LOWER(CONCAT('%', :namePart, '%'))")
|
||||
Page<Owner> findByNameContaining(@Param("namePart") String namePart, Pageable pageable);
|
||||
@Query("SELECT o FROM Owner o WHERE LOWER(CONCAT(o.firstName, ' ', o.lastName)) LIKE LOWER(CONCAT('%', :namePart, '%'))")
|
||||
Page<Owner> findByLastNameStartingWith(@Param("namePart") String namePart, Pageable pageable);
|
||||
|
||||
/**
|
||||
* Retrieve an {@link Owner} from the data store by id.
|
||||
|
|
|
@ -18,8 +18,10 @@ management.endpoints.web.exposure.include=*
|
|||
|
||||
# Logging
|
||||
logging.level.org.springframework=INFO
|
||||
# logging.level.org.springframework.web=DEBUG
|
||||
# logging.level.org.springframework.context.annotation=TRACE
|
||||
logging.level.org.springframework.web=DEBUG
|
||||
logging.level.org.springframework.context.annotation=TRACE
|
||||
logging.level.org.springframework=DEBUG
|
||||
logging.level.org.hibernate.SQL=DEBUG
|
||||
|
||||
# Maximum time static resources should be cached
|
||||
spring.web.resources.cache.cachecontrol.max-age=12h
|
||||
|
|
Loading…
Reference in a new issue