将结果限制为1

时间:2019-07-02 09:40:17

标签: java spring spring-data-jpa spring-data single-page-application

我有这个Spring JPA存储库:

@Repository
public interface MerchantUserRepository extends JpaRepository<Users, Integer> {

    Optional<Users> findByIdAndTypeIn(Integer id, String... types);
}

是否有某种方法可以将返回的行数限制为1?

1 个答案:

答案 0 :(得分:0)

您在查询中添加了可分页参数:

Optional<Users> findByIdAndTypeIn(Integer id, String... types, Pageable page);

对于一个结果,您可以像这样创建它:

Pageable firstRow = PageRequest.of(0, 1);

在这种情况下,您可以考虑为查询添加一个Sort对象

相关问题