我有一个 Spring Boot 应用程序。有一个实体:
@Entity
@Table(name = "user")
public class User {
private Long id;
private String name;
private Long schoolId;
private Boolean isActive;
// getters and setters
}
我有一个存储库:
@Repository
public interface UserRepositoryPageable extends PagingAndSortingRepository<User, Long> {
}
我需要提出搜索请求schoolId
和过滤器所有领域通过一些字符串。
像这样的事情:
@Query("SELECT u FROM User u " +
"WHERE u.schoolId = :schoolId AND (" +
"u.id like %:searchVal% OR " +
"u.name like %:searchVal% OR " +
"u.isActive like %:searchVal%)")
Page<User> getUserBySchoolIdWithFilter(@Param("schoolId") Long schoolId,
Pageable pageable,
@Param("searchVal") String searchVal);
但我收到了例外,因为我尝试申请like
to Long
and Boolean
.
例如,如果我尝试按“testSearchValue”进行过滤,我会收到此异常:
java.lang.IllegalArgumentException:参数值 [%testSearchValue%] 与预期类型 [java.lang.Long(不适用)] 不匹配
很遗憾,CAST
and CONVERT
没有为我工作。
那么有什么解决方法吗?
一些细节
我发送一个GET
请求该API:
@RequestMapping(path = "users/{schoolId}/search", method = GET)
public ResponseEntity<Page<User>> searchUserBySchoolWithFilter(
@PathVariable(value = "schoolId") Long schoolId, Pageable pageable,
@RequestParam(value = "searchVal", required = false) String searchVal) {
return new ResponseEntity<>(userService
.getUserBySchoolIdWithFilter(schoolId, pageable, searchVal), HttpStatus.OK);
}
Then in UserService
:
public Page<User> getUserBySchoolIdWithFilter(Long schoolId, Pageable pageable, String searchVal) {
return userRepositoryPageable.getUserBySchoolIdWithFilter(schoolId, pageable, searchVal);
}
So:
在我看来,这个问题的基本点是代表Long
and Boolean
as String
.
也许更好用nativeQuery
?如果是这样,那么你能给我一个关于如何使用的提示吗CAST()
or CONVERT()
with LIKE
clause?