原文:https://www.cnblogs.com/hdwang/p/7843405.html
spring data jpa 分頁查詢
方法一(本地sql查詢,注意表名啥的都用數據庫中的名稱,適用於特定數據庫的查詢)
public interface UserRepository extends JpaRepository<User, Long> { @Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1", countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1", nativeQuery = true) Page<User> findByLastname(String lastname, Pageable pageable); }
方法二(jpa已經實現的分頁接口,適用於簡單的分頁查詢)
public interface PagingAndSortingRepository<T, ID extends Serializable> extends CrudRepository<T, ID> { Iterable<T> findAll(Sort sort); Page<T> findAll(Pageable pageable); } Accessing the second page of User by a page size of 20 you could simply do something like this: PagingAndSortingRepository<User, Long> repository = // … get access to a bean Page<User> users = repository.findAll(new PageRequest(1, 20));
User findFirstByOrderByLastnameAsc(); User findTopByOrderByAgeDesc(); Page<User> queryFirst10ByLastname(String lastname, Pageable pageable); Slice<User> findTop3ByLastname(String lastname, Pageable pageable); List<User> findFirst10ByLastname(String lastname, Sort sort); List<User> findTop10ByLastname(String lastname, Pageable pageable);
//service Sort sort = new Sort(Sort.Direction.DESC,"createTime"); //創建時間降序排序 Pageable pageable = new PageRequest(pageNumber,pageSize,sort); this.depositRecordRepository.findAllByUserIdIn(userIds,pageable); //repository Page<DepositRecord> findAllByUserIdIn(List<Long> userIds,Pageable pageable);
方法三(Query注解,hql語局,適用於查詢指定條件的數據)
@Query(value = "select b.roomUid from RoomBoard b where b.userId=:userId and b.lastBoard=true order by b.createTime desc") Page<String> findRoomUidsByUserIdPageable(@Param("userId") long userId, Pageable pageable);
Pageable pageable = new PageRequest(pageNumber,pageSize); Page<String> page = this.roomBoardRepository.findRoomUidsByUserIdPageable(userId,pageable); List<String> roomUids = page.getContent();
可以自定義整個實體(Page<User>),也可以查詢某幾個字段(Page<Object[]>),和原生sql幾乎一樣靈活。
方法四(擴充findAll,適用於動態sql查詢)
public interface UserRepository extends JpaRepository<User, Long> { Page<User> findAll(Specification<User> spec, Pageable pageable); }
@Service public class UserService { @Autowired private UserRepository userRepository; public Page<User> getUsersPage(PageParam pageParam, String nickName) { //規格定義 Specification<User> specification = new Specification<User>() { /** * 構造斷言 * @param root 實體對象引用 * @param query 規則查詢對象 * @param cb 規則構建對象 * @return 斷言 */ @Override public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> predicates = new ArrayList<>(); //所有的斷言 if(StringUtils.isNotBlank(nickName)){ //添加斷言 Predicate likeNickName = cb.like(root.get("nickName").as(String.class),nickName+"%"); predicates.add(likeNickName); } return cb.and(predicates.toArray(new Predicate[0])); } }; //分頁信息 Pageable pageable = new PageRequest(pageParam.getPage()-1,pageParam.getLimit()); //頁碼:前端從1開始,jpa從0開始,做個轉換 //查詢 return this.userRepository.findAll(specification,pageable); } }
