我正在 Spring Data JPA 中按CrudRepository
检索数据。我想过滤从@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);
}
我希望我的List<SLSNotification> getApplicationsByUserIdforManage(@Param("userId") String userId);
方法检索有限的数据集。我怎样才能打电话给实体经理或其他什么东西呢?
答案 0 :(得分:12)
您可以在SQL中通过Hi testuser1,
Welcome to the Sample App! Click on the link below to activate your account:
https://still-beyond-10331.herokuapp.com/account_activations/0MwIEqEXZaLzmWJb8KhGOg/edit?email=testuser1%40testemail1.com
语句提供限制。并在limit
注释中设置nativeQuery = true
,以便将JPA提供程序视为@Query
SQL查询。
native
如果您想利用 Spring Data JPA 的强大功能 你可以通过适当的方法命名
来做到这一点@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会根据方法名称构造Query。太棒了吧?请阅读此documentation以便更好地理解。
你刚刚离开称这个方法就像
List<SLSNotification> findByUserIdOrderBySNumber(@Param("userId") String userId, Pageable pageable);
您可以利用在界面中使用默认方法的功能,并使事情变得更加便利
Pageable topTwenty = new PageRequest(0, 20);
List<SLSNotification> notifications = repository.findByUserIdOrderBySNumber("101", topTwenty);