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

Nip*_*ana 3 hibernate hql spring-data spring-data-jpa

CrudRepositorySpring Data JPA中检索数据.我想过滤从@Query注释中提供的自定义查询中检索到的记录.我试过.setMaxResults(20);select rows..,但它给错误.我想从我的表中过滤前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);

}
Run Code Online (Sandbox Code Playgroud)

我希望我的List<SLSNotification> getApplicationsByUserIdforManage(@Param("userId") String userId);方法检索有限的数据集.我怎样才能打电话给实体经理或其他什么东西呢?

请帮我这样做. 输出img

Sha*_*mud 14

您可以limit在SQL中通过语句提供限制.并且nativeQuery = true@Query注释中设置JPA提供程序将其视为nativeSQL查询.

@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);
Run Code Online (Sandbox Code Playgroud)

要么

如果您想利用Spring Data JPA的强大功能, 可以通过适当的方法命名来实现

List<SLSNotification> findByUserIdOrderBySNumber(@Param("userId") String userId, Pageable pageable);
Run Code Online (Sandbox Code Playgroud)

如果您还不知道,Spring Data JPA会根据方法名称构造Query.太棒了吧?阅读本文档以便更好地理解.

而你刚刚离开称这种方法就像

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

此外,如果您使用的是Java 8

您可以利用界面中具有默认方法的功能,使事情变得更加方便

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

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