按关键字和类别在列表中搜索
search in the list by keyword and category
我想按类别和关键字执行搜索,但查询 returns 是一个空列表。我知道您可以为请求制定多种方法,但我想将其放入一种方法中。请告诉我如何正确地做到这一点
存储库:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByEmail(String email);
User findByNickname(String nickname);
User findByPhone(String phoneNumber);
@Query(value = "SELECT * FROM users WHERE :filter LIKE CONCAT('%',:keyword,'%')",
nativeQuery=true)
List<User> findByKeyword(@Param("filter") String filter,
@Param("keyword") String keyword);
}
控制器:
@Controller
@RequestMapping("/admin")
public class AdminController {
@Autowired
private UserService userService;
@Autowired
private OrderService orderService;
@GetMapping("/users")
public String index(Model model, Principal user,
String keyword, String filter){
model.addAttribute("user",
userService.findUserByNickname(user.getName()));
if(keyword!=null){
model.addAttribute("users",
userService.getUsersByKeyword(filter, keyword));
return "admin/index";
}
model.addAttribute("users", userService.findAll());
return "admin/index";
}
}
HTML:
<form th:action="@{/admin/users}" method="get">
<input type="text" name="keyword">
<input type="checkbox" name="filter" value="id"><label>ID</label>
<input type="checkbox" name="filter" value="nickname"><label>NAME</label>
<input type="checkbox" name="filter" value="phone"><label>PHONE</label>
<input type="checkbox" name="filter" value="email"><label>EMAIL</label>
<button type="submit">Go</button>
</form>
服务:
public List<User> getUsersByKeyword(String filter, String keyword){
return userRepository.findByKeyword(filter,keyword);
}
您可以使用规范:https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
只需让您的存储库扩展 JpaSpecificationExecutor<T>
:
public interface UserRepository extends JpaRepository<User, Long>,
JpaSpecificationExecutor<User> {
}
并按规格查询:
String column = "myColumn"; // user-supplied
String value = "myValue"; // user-supplied
Specification<User> spec = (root, query, criteriaBuilder) ->
criteriaBuilder.like(root.get(column), "%" + value + "%");
var result = repo.findAll(spec);
我想按类别和关键字执行搜索,但查询 returns 是一个空列表。我知道您可以为请求制定多种方法,但我想将其放入一种方法中。请告诉我如何正确地做到这一点
存储库:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByEmail(String email);
User findByNickname(String nickname);
User findByPhone(String phoneNumber);
@Query(value = "SELECT * FROM users WHERE :filter LIKE CONCAT('%',:keyword,'%')",
nativeQuery=true)
List<User> findByKeyword(@Param("filter") String filter,
@Param("keyword") String keyword);
}
控制器:
@Controller
@RequestMapping("/admin")
public class AdminController {
@Autowired
private UserService userService;
@Autowired
private OrderService orderService;
@GetMapping("/users")
public String index(Model model, Principal user,
String keyword, String filter){
model.addAttribute("user",
userService.findUserByNickname(user.getName()));
if(keyword!=null){
model.addAttribute("users",
userService.getUsersByKeyword(filter, keyword));
return "admin/index";
}
model.addAttribute("users", userService.findAll());
return "admin/index";
}
}
HTML:
<form th:action="@{/admin/users}" method="get">
<input type="text" name="keyword">
<input type="checkbox" name="filter" value="id"><label>ID</label>
<input type="checkbox" name="filter" value="nickname"><label>NAME</label>
<input type="checkbox" name="filter" value="phone"><label>PHONE</label>
<input type="checkbox" name="filter" value="email"><label>EMAIL</label>
<button type="submit">Go</button>
</form>
服务:
public List<User> getUsersByKeyword(String filter, String keyword){
return userRepository.findByKeyword(filter,keyword);
}
您可以使用规范:https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
只需让您的存储库扩展 JpaSpecificationExecutor<T>
:
public interface UserRepository extends JpaRepository<User, Long>,
JpaSpecificationExecutor<User> {
}
并按规格查询:
String column = "myColumn"; // user-supplied
String value = "myValue"; // user-supplied
Specification<User> spec = (root, query, criteriaBuilder) ->
criteriaBuilder.like(root.get(column), "%" + value + "%");
var result = repo.findAll(spec);