Пользовательское название города и результат отображает город из моей базы данных, но когда пользователь ищет результат с именем состояния, ничего не показывает!
У меня есть 4 столбца (город, штат, службы, название) в моей базе данных, что пользователь может искать значения из них, но результат ничего не показывает, кроме как только для столбца города.
Я знаю, что могу сделать это с помощью jQuery, но я хочу сделать это с помощью PagingAndSortingRepository
Мой HTML:
....
<form th:action="@{/admin/users/allusers}" method="get">
<div class="input-group input-group-sm" style="width: 450px;">
<input type="text" name="city" class="form-control pull-right" placeholder="search" th:if="${key} == null"/>
<input type="text" name="city" class="form-control pull-right" placeholder="search" th:if="${key} != null" th:value="${key}"/>
<div class="input-group-btn">
<button type="submit" class="btn btn-primary"><i class="fa fa-search"></i>search</button>
</div>
</div>
</form>
.....
My Controller:
@RequestMapping(value = "/admin/users/allusers",method = RequestMethod.GET)
public ModelMap usersList(@PageableDefault(size = 10)Pageable pageable,
@RequestParam(name = "state", required = false)String state,
@RequestParam(name = "city", required = false)String city,
@RequestParam(name = "title", required = false)String title,Model model)
{
if (state != null) {
model.addAttribute("key", state);
return new ModelMap().addAttribute("usersList", usersRepository.findByStateContainingIgnoreCase(state, pageable));
}else if(city != null){
model.addAttribute("key", city);
return new ModelMap().addAttribute("usersList", usersRepository.findByCityContainingIgnoreCase(city, pageable));
}else if(title != null){
model.addAttribute("key", title);
return new ModelMap().addAttribute("usersList", usersRepository.findByTitleContainingIgnoreCase(title, pageable));
}
else{
return new ModelMap().addAttribute("usersList", usersRepository.findAll(pageable));
}
}
Мой репозиторий:
public interface UsersRepository extends PagingAndSortingRepository<User,String> {
User findById(Long id);
public Page<User> findByIdContainingIgnoreCase(Long id, Pageable pageable);
Page<User>findByCityContainingIgnoreCase(String city, Pageable pageable);
Page<User>findByStateContainingIgnoreCase(String state, Pageable pageable);
Page<User>findByTitleContainingIgnoreCase(String title, Pageable pageable);
Page<User>findAll(Pageable pageable);
@Transactional
void deleteById(Long id);
}
Насколько я вижу, вам нужно комбинировать поля поиска. Существует несколько способов поддержать это.
Вы можете определить метод в вашем репозитории, чтобы объединить все поля
Page<User> findByIdContainingIgnoreCaseAndCityContainingIgnoreCaseAndStateContainingIgnoreCaseAndTitleContainingIgnoreCase(Long id, String city, String state, String title, Pageable pageable);
Да, это немного дольше, чем нам нужно.
Или вы можете определить метод с помощью @Query
@Query("SELECT u FROM User u WHERE LOWER(u.city) = LOWER(:city) and LOWER(u.state) = LOWER(:state) ...")
Page<User> search(@Param("id") Long id,
@Param("city") String city,
@Param("state") String state,
@Param("title") String title, Pageable pageable);