how can i implement a pagination in spring jdbcTemplate

后端 未结 4 1814
时光取名叫无心
时光取名叫无心 2021-01-13 23:22

here is my following dao implementaion

@Override
    public List getAddresses(int pageid,int total) {

        String sql = \"select * FRO         


        
4条回答
  •  失恋的感觉
    2021-01-14 00:08

    I agree with @Erica Kane for use of LIMIT and OFFSET.

    However, If Database is not supporting LIMIT and OFFSET then you can use ROW_NUMBER()

    for example -
    SELECT * FROM ( SELECT ROW_NUMBER() OVER(ORDER BY id) as RRN FROM user_addresses as T1 ) WHERE RRN between :start and :end; :start and :end you can give whatever number you wish to fetch result. 1 to 100 etc.
    If Total rows are less than end number then it will simply return whatever rows present.

    Some of the best link I found regarding ROW_NUMBER() with great explanation-

    https://blog.sqlauthority.com/2011/08/12/sql-server-tips-from-the-sql-joes-2-pros-development-series-ranking-functions-rank-dense_rank-and-row_number-day-12-of-35/

    https://blog.sqlauthority.com/2007/10/09/sql-server-2005-sample-example-of-ranking-functions-row_number-rank-dense_rank-ntile/

    https://blog.sqlauthority.com/2008/03/12/sql-server-2005-find-nth-highest-record-from-database-table-using-ranking-function-row_number/

    https://blog.sqlauthority.com/2015/09/03/sql-server-whats-the-difference-between-row_number-rank-and-dense_rank-notes-from-the-field-096/

提交回复
热议问题