【发布时间】:2019-11-19 10:31:09
【问题描述】:
我正在尝试实现受 IN 子句限制的搜索功能:
我想实现带有过滤器限制的搜索实现:
@GetMapping("find")
public Page<MerchantUserDTO> getAllBySpecification(
@And({
@Spec(path = "name", spec = LikeIgnoreCase.class),
@Spec(path = "login", spec = LikeIgnoreCase.class),
@Spec(path = "email", spec = LikeIgnoreCase.class),
}) Specification<Users> specification,
@SortDefault(sort = "login", direction = Sort.Direction.DESC) Pageable pageable
) {
return merchantUserService.getAllBySpecification(specification, pageable)
.map(g -> MerchantUserDTO.builder()
.id(g.getId())
.login(g.getLogin())
.build()
);
}
@Override
public Page<Users> getAllBySpecification(Specification<Users> specification, Pageable pageable) {
return dao.findAllByTypeIn(specification, pageable, "MerchantUser");
}
存储库:
@Repository
public interface MerchantUserRepository extends JpaRepository<Users, Integer>, JpaSpecificationExecutor<Users> {
Page<Users> findAllByTypeIn(Pageable page, String... types);
Page<Users> findAllByTypeIn(Specification<Users> specification, Pageable pageable, String... types);
}
用 IN 子句扩展规范的正确方法是什么?
specification.and(path.in(types)) path 是一个属性但是如何正确实现呢?
【问题讨论】:
-
您需要帮助的具体问题是什么?
-
@JensSchauder 上述规范工作正常,但我想限制所有带有 IN 子句的 SELECT 子句(WHERE type IN ('MerchantUser'))。如何将此条款添加到规范中?你能给我一些代码示例怎么做吗?
-
@JensSchauder 我也试过了,但它不起作用stackoverflow.com/questions/56942688/…
-
向我们展示您的尝试以及失败的原因。
-
@JensSchauder 我已经尝试了上面的代码。这是错误堆栈:pastebin.com/WJrJRC01
标签: spring-boot jpa spring-data-jpa jpa-2.0 jpa-2.1