Spring Data JPA native query 分页

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本地查询(带分页方式)

Oracle 数据库中要将 \n#pageable\n 改成 ?#{#pageable} 形式。

posted @ 2018-09-08 17:38  唯学而知  阅读(5727)  评论(0编辑  收藏  举报