Spring Data Pageables可以与原始JDBC查询一起使用吗?

uqjltbpv  于 2023-04-06  发布在  Spring
关注(0)|答案(1)|浏览(114)

Sping Boot 在这里。我看到Pageable与存储库方法一起使用,如下所示:

@Repository
public interface SomeThingRepository extends JpaRepository<SomeThing, Long> {
    List<SomeThing> findByClientName(String clientName, Pageable pageable);
}

然而,我有一个情况,我没有能力使用存储库类/方法,而是在后台使用原始的DataSource
有什么方法可以让Pageable与JDBC DataSource一起工作吗?

String ctQuery = "SELECT blah blah blah";

Connection conn = null;
PreparedStatement statement = null;
ResultSet rs = null;
try {

    DataSource ds = getDataSource();
    conn = ds.getConnection();

    // anyway to infuse/inject a Pageable here?
    statement = conn.prepareStatement(ctQuery);
    statement.setString(1, clientName);
    rs = statement.executeQuery();

    rs.next();

    // process rs here

} catch (SQLException sqlException) {
    log.error(ExceptionUtils.getStackTrace(sqlException));
    // etc...omitted for brevity
} finally {
    // etc...omitted for brevity
}

先谢谢你了!

iyr7buue

iyr7buue1#

您需要在SQL中设置限制。遗憾的是,每个DB都有自己的关键字来执行此操作-请参阅here
对于Oracle,请执行以下操作:

SELECT blah blah blah

WHERE ROWNUM >= :pageSize * (:pageNumber - 1)
AND ROWNUM < :pageSize * :pageNumber

相关问题