public interface UserRepository extends JpaRepository<User, Long> { @Query(value = "select * from users order by id desc \n#pageable\n", countQuery = "select count(*) from users", nativeQuery = true) Page<User> findAllRandom(Pageable pageable); }
若是 pageable 對象中有排序,這裏能夠去掉
order by
排序。
countQuery
能夠省略。sql
H2 數據庫中要將 \n#pageable\n
改爲 \n-- #pageable\n
形式。數據庫
Oracle 數據庫中要將 \n#pageable\n
改爲 ?#{#pageable}
形式。dom