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 
#pageable
",
            countQuery = "select count(*) from users",
            nativeQuery = true)
    Page<User> findAllRandom(Pageable pageable);
}

如果 pageable 对象中有排序,这里可以去掉 order by 排序。
countQuery 可以省略。

H2

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

Oracle

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

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

原文地址:https://www.cnblogs.com/wu726/p/9609847.html