java - 将结果限制为 1
问题描述
我有这个 Spring JPA 存储库:
@Repository
public interface MerchantUserRepository extends JpaRepository<Users, Integer> {
Optional<Users> findByIdAndTypeIn(Integer id, String... types);
}
有没有办法将返回的行数限制为 1?
解决方案
您将 Pageable 参数添加到查询中:
Optional<Users> findByIdAndTypeIn(Integer id, String... types, Pageable page);
对于一个结果,您可以像这样创建它:
Pageable firstRow = PageRequest.of(0, 1);
在这种情况下,您也可以考虑为您的查询添加一个Sort
对象
推荐阅读
- java - 使用java 8从列表中删除具有基于另一个属性的重复属性的元素
- mysql - MySQL concat with not exits condition
- javascript - mobx computed / reactive context with event handlers?
- javascript - 在隐藏整行的同时搜索表格单元格
- c++ - 如何更新样式表的单个属性?
- javascript - 看不到卡
- jquery - 引导程序 4 上的导航栏下拉菜单
- php - Sending Image by file in Android (with FileInputStream) to PHP and saving mysql by Blob
- python - 使用带有 Python OpenCV 的 gstreamer 来捕获实时流?
- java - 用变量实例化对象数组