如何限制 Spring Data Repository 中使用的 @Query 的结果

How to limit result in @Query used in Spring Data Repository

我正在 Spring Data JPA 中通过 CrudRepository 检索数据。我想过滤我的记录,这些记录是从 @Query 注释中提供的自定义查询中检索到的。我尝试 .setMaxResults(20);select rows.. 但它给出了错误。我想从 table

中筛选前 20 行

这是我的存储库

package lk.slsi.repository;

import java.util.Date;
import lk.slsi.domain.SLSNotification;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
 * Created by ignotus on 2/10/2017.
 */
public interface SLSNotificationRepository extends CrudRepository<SLSNotification, Integer> {


    @Override
    SLSNotification save(SLSNotification slsNotification);

    @Override
    SLSNotification findOne(Integer snumber);

    @Override
    long count();

    @Override
    void delete(Integer integer);

    @Override
    void delete(SLSNotification slsNotification);

    @Override
    void delete(Iterable<? extends SLSNotification> iterable);

    @Override
    List<SLSNotification> findAll();

    @Query("select a from SLSNotification a where a.slsiUnit in :unitList order by snumber desc")
    List<SLSNotification> getApplicationsByUnit(@Param("unitList") List<String> unitList);

    @Query("select a from SLSNotification a where a.userId = :userId")
    List<SLSNotification> getApplicationsByUserId(@Param("userId") String userId);

    @Query("select a.snumber, a.date, a.slsNo, a.slsiUnit, a.productDesc, a.status from SLSNotification a where a.userId = :userId ORDER BY snumber desc")
    List<SLSNotification> getApplicationsByUserIdforManage(@Param("userId") String userId);

    @Query("select a from SLSNotification a where a.slsNo = :slsNo")
    SLSNotification getApplicationBySLSNumber(@Param("slsNo") String slsNo);

}

我希望我的 List<SLSNotification> getApplicationsByUserIdforManage(@Param("userId") String userId); 方法检索一组有限的数据。我怎样才能打电话给实体经理或其他人来做这件事?

请帮我做这个。

您可以在 SQL 中通过 limit 声明提供限制。并在 @Query 注释中设置 nativeQuery = true 以设置 JPA 提供程序(如 Hibernate)将其视为 native SQL 查询。

@Query(nativeQuery = true, value = "SELECT * FROM SLSNotification s WHERE s.userId = :userId ORDER BY snumber DESC LIMIT 20")
List<SLSNotification> getUserIdforManage(@Param("userId") String userId);

此外,如果您想利用 Spring Data JPA 中的便捷功能, 你可以通过正确的方法命名来做到这一点

List<SLSNotification> findByUserIdOrderBySNumber(@Param("userId") String userId, Pageable pageable);

如果您还不知道,Spring Data JPA 从方法名称构造查询。太棒了,对吧?阅读此 documentation 以更好地理解。

现在只需调用此方法即可

Pageable topTwenty = PageRequest.of(0, 20);
List<SLSNotification> notifications = repository.findByUserIdOrderBySNumber("101", topTwenty);

此外,如果您正在使用 Java 8

您可以选择在界面中使用默认方法,让生活更轻松

 List<SLSNotification> findByUserIdOrderBySNumber(@Param("userId") String userId, Pageable pageable);

 default List<User> findTop20ByUserIdOrderBySNumber(String userId) {
    return findByUserIdOrderBySNumber(userId, PageRequest.of(0,20));
 }

根据 Spring Data JPA 提供的通过方法名称创建查询的功能,您可以使用它。更多Supported query method predicate keywords and modifiers检查这个。

Optional<List<SLSNotification>> findTop20ByUserId(String id); 

Optional<List<SLSNotification>> findFirst20ByUserId(String id); 

这会将查询结果限制为第一个结果。

如果要限制有序结果集,请在方法名称中使用 DescAscOrderBy

// return the first 20 records which are ordered by SNumber. If you want Asc order, no need to add that keyword since it is the default of `OrderBy` 
Optional<List<SLSNotification>> findFirst20ByUserIdOrderBySNumberDesc(String id);  

建议 -- 从代码方面来说,在 find 这样的查询中使用 Optional 类型的 return 更好更安全, get 怀疑至少有一个结果。