Spring Data JPA native query with pagination

MySQL

Spring Data JPA and native queries with pagination

Spring Data and Native Query with pagination

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 可以省略。

H2

H2 数据库中要将 \n#pageable\n 改成 \n-- #pageable\n 形式。

Oracle

Spring Data Jpa本地查询(带分页方式)

分享到:
Disqus 加载中...

如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理