博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring JDBC Pagination Tutorial
阅读量:5828 次
发布时间:2019-06-18

本文共 4470 字,大约阅读时间需要 14 分钟。

This tutorial uses the use code of listing a number of companies. This might be a public service listing records of public companies.

Using Spring JDBC without Pagination

First of all, let's look at a standard technique using Spring JDBC to return a list of companies in a single result set.

public List
 getCompanies() throws SQLException { return jdbcTemplate.query( "SELECT id, name FROM company WHERE user_id = ? AND deleted = 0 ORDER BY name", new ParameterizedRowMapper
() { public Company mapRow(ResultSet rs, int i) throws SQLException { return new Company( rs.getInt(1), rs.getString(2) ); } }, userId ); }

 

Using Spring JDBC with Pagination

Here's a new version of the method which uses a PaginationHelper class, which is shown further on in this tutorial. As you can see, the usage is very similar to standard Spring JDBC. The most obvious difference is that we now have two SQL statements instead of one. This is necessary if we want to be able to show the users how many pages of data there are but it is an extra performance hit so there is room for refinement in this approach by perhaps caching the number of pages. The code uses the standard ParameterizedRowMapper to minimize impact on existing code.

public Page
 getCompanies(final int pageNo, final int pageSize) throws SQLException { PaginationHelper
ph = new PaginationHelper
(); return ph.fetchPage( jdbcTemplate, "SELECT count(*) FROM company WHERE user_id = ? AND deleted = 0 ORDER BY name", "SELECT id, name FROM company WHERE user_id = ? AND deleted = 0 ORDER BY name", new Object[]{ userId}, pageNo, pageSize, new ParameterizedRowMapper
() { public Company mapRow(ResultSet rs, int i) throws SQLException { return new Company( rs.getInt(1), rs.getString(2) ); } } ); }

The Page class

The Page class is a very simple template class that contains a list of items, the page number, and the number of pages that are available.

 

public class Page
 {        private int pageNumber; private int pagesAvailable; private List
pageItems = new ArrayList
(); public void setPageNumber(int pageNumber) { this.pageNumber = pageNumber; } public void setPagesAvailable(int pagesAvailable) { this.pagesAvailable = pagesAvailable; } public void setPageItems(List
pageItems) { this.pageItems = pageItems; } public int getPageNumber() { return pageNumber; } public int getPagesAvailable() { return pagesAvailable; }      public List
 getPageItems() {
            return pageItems; } }  

Pagination Helper

Here's the source code for the PaginationHelper class. This is actually very simple. The first SQL query is executed to determine how many rows of data are available. This allows the number of pages to be calculated. The second query is then executed using the JdbcTemplate query method that accepts a Spring ResultSetExtractor. The implementation of this ResultSetExtractor processes the result set and delegates to the supplied ParameterizedRowMapper for those rows that should be returned as part of the current page of data.

public class PaginationHelper
 {    public Page
fetchPage( final JdbcTemplate jt, final String sqlCountRows, final String sqlFetchRows, final Object args[], final int pageNo, final int pageSize, final ParameterizedRowMapper
rowMapper) { // determine how many rows are available final int rowCount = jt.queryForInt(sqlCountRows, args); // calculate the number of pages int pageCount = rowCount / pageSize; if (rowCount > pageSize * pageCount) { pageCount++; } // create the page object final Page
page = new Page
(); page.setPageNumber(pageNo); page.setPagesAvailable(pageCount);
     // fetch a single page of results        final int startRow = (pageNo - 1) * pageSize; jt.query( sqlFetchRows, args, new ResultSetExtractor() { public Object extractData(ResultSet rs) throws SQLException, DataAccessException { final List pageItems = page.getPageItems(); int currentRow = 0; while (rs.next() && currentRow < startRow + pageSize) { if (currentRow >= startRow) { pageItems.add(rowMapper.mapRow(rs, currentRow)); } currentRow++; } return page; } }); return page; }
}
 
 

转载于:https://www.cnblogs.com/renwei/p/5660992.html

你可能感兴趣的文章
四、配置开机自动启动Nginx + PHP【LNMP安装 】
查看>>
Linux 目录结构及内容详解
查看>>
OCP读书笔记(24) - 题库(ExamD)
查看>>
.net excel利用NPOI导入oracle
查看>>
$_SERVER['SCRIPT_FLENAME']与__FILE__
查看>>
hive基本操作与应用
查看>>
html5纲要,细谈HTML 5新增的元素
查看>>
Android应用集成支付宝接口的简化
查看>>
[分享]Ubuntu12.04安装基础教程(图文)
查看>>
django 目录结构修改
查看>>
win8 关闭防火墙
查看>>
CSS——(2)与标准流盒模型
查看>>
C#中的Marshal
查看>>
linux命令:ls
查看>>
Using RequireJS in AngularJS Applications
查看>>
hdu 2444(二分图最大匹配)
查看>>
【SAP HANA】关于SAP HANA中带层次结构的计算视图Cacultation View创建、激活状况下在系统中生成对象的研究...
查看>>
DevOps 前世今生 | mPaaS 线上直播 CodeHub #1 回顾
查看>>
iOS 解决UITabelView刷新闪动
查看>>
CentOS 7 装vim遇到的问题和解决方法
查看>>