1.封装一个公共实体类用于返回:实体数据,当前页,总页数,总条数,每页多少条

public class PageInfo<T> {

    //一页显示的记录数
private int numPerPage=10;
//记录总数
private int totalRows;
//总页数
private int totalPages;
//当前页码
private int currentPage; private List<T> list; // 总数统计
private T sumObj; public T getSumObj() {
return sumObj;
}
public void setSumObj(T sumObj) {
this.sumObj = sumObj;
}
public PageInfo() {
this.totalRows = 0;
this.totalPages = 0;
this.currentPage = 1;
}
public PageInfo(Integer currentPage, int totalRows) {
if(currentPage==null)currentPage=1;
setCurrentPage(currentPage);
this.totalRows = totalRows;
}
public PageInfo(Integer currentPage){
if(currentPage==null)currentPage=1;
setCurrentPage(currentPage);
}
public int getCurrentPage() {
return currentPage;
} public void setCurrentPage(int currentPage) {
this.currentPage = currentPage <= 0 ? 1 : currentPage;
} public int getNumPerPage() {
return numPerPage;
} public void setNumPerPage(int numPerPage) {
this.numPerPage = numPerPage;
} public int getTotalPages() {
if (this.totalPages == 0) {
this.totalPages = this.totalRows / this.numPerPage + (this.totalRows % this.numPerPage == 0 ? 0 : 1);
}
return this.totalPages;
} public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getTotalRows() {
return totalRows;
}
public int getStartIndex() {
return (currentPage - 1) * numPerPage;
} public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
} public void setList(List<T> list) {
this.list = list;
} public List<T> getList() {
return list;
} }

  2.思路:

1)先计算总条数,每页条数可以固定,当前页数,总页数:

select count(*)from (自己写的查询语句) a;//计算出总条数,然后通过总条数和每页条数计算出总页数,当前页是前端传递过来的。将计算出来的这些数据放入PageInfo<T>的页数相关字段中

2)获取对应页数的T(实体类):

通过mysql的limit字段:

开始页数:

public int getStartIndex() {
return (currentPage - 1) * numPerPage;
}

条数:取每页条数。

3)将T封装到PageInfo<T>的private List<T> list中;

4)返回PageInfo<T>

3.具体方法:

public <T> PageInfo<T> getObjectsByPage(Class<T> cls,String sql,RowMapper<T> rowMapper,PageInfo<T> pageInfo,Object[] objects){
if(null==pageInfo){
pageInfo=new PageInfo<T>();
}
if(pageInfo.getTotalRows()<=0){
//总条数
Integer total= this.getCount(sql, objects);
pageInfo.setSumObj(sumObj);
//Integer total=getObjectCount(cls,sql, objects);
pageInfo.setTotalRows(total);//总条数:sum子查询出来的
//总页数
Integer totalPages= (int) Math.ceil((double)total/(double)pageInfo.getNumPerPage());
pageInfo.setTotalPages(totalPages);
}
List<T> list=getObjectList(cls,sql, objects, pageInfo, rowMapper);
pageInfo.setList(list);
return pageInfo;
} private Integer getCount(String sql,Object... objects){
StringBuffer count=new StringBuffer("select ");
String sqlStr=sql.toLowerCase(); count.append(" count(*) id "); count.append(" from (");
count.append(sqlStr);
count.append(") temp_for_page_count"); return getAccess(null).findInteger(count.toString(), objects);
} private <T> List<T> getObjectList(Class<T> cls,String sql,Object[] objects,PageInfo<T> pageInfo,RowMapper<T> rowMapper){
StringBuffer limit=new StringBuffer(sql);
limit.append(" limit ").append(pageInfo.getStartIndex()).append(",").append(pageInfo.getNumPerPage());
List<T> list=getAccess(cls).find(limit.toString(),rowMapper,objects);
return list; }

jdbc查询语句:

 public <T> List<T> find(String sql, RowMapper<T> rowMapper, Object... params) {
StopWatch watch = new StopWatch();
try {
return this.jdbcTemplate.query(sql, params, rowMapper);
} finally {
this.logger.debug(FIND_SQL_LOG, new Object[] { sql, params, Long.valueOf(watch.elapsedTime()) });
if (watch.elapsedTime() > SLOW_SQL_TIME_LENGTH)
this.logger.warn(SLOW_SQL_LOG, new Object[] { sql, Long.valueOf(watch.elapsedTime()) });
}
}

jdbc实现分页,需要前端传当前页码的更多相关文章

  1. js 页码分页的前端写法

    <script type="text/javascript"> var curPage = 1;//当前页码 var total;//总页数 $(function () ...

  2. vue+element的表格分页和前端搜索

    1.前端后台管理会存在很多表格,表格数据过多就需要分页;2.前端交互每次搜索如果都请求服务器会加大服务器的压力,所以在数据量不是很大的情况下可以一次性将数据返回,前端做检索3.下面贴上一个demo & ...

  3. php-数据库-分页类-上传类

    config.ini.php <?php header("content-type:text/html;charset=utf-8"); //项目的根目录 define(&q ...

  4. 正确的前端传后台json方式

    DEMO: var data=JSON.stringify({"page": {"pagenow": 1,"pagesize": 20},& ...

  5. asp.net MVC 框架中控制器里使用Newtonsoft.Json对前端传过来的字符串进行解析

    下面我用一个实例来和大家分享一下我的经验,asp.net MVC 框架中控制器里使用Newtonsoft.Json对前端传过来的字符串进行解析. using Newtonsoft.Json; usin ...

  6. jdbc 实现分页

    jdbc 实现分页,的实现 原理这个就不介绍了.. 总之是用jdbc 的游标移动 package com.sp.person.sql.util; import java.sql.Connection; ...

  7. @requestbody---接受前端传json对象并绑定javabean

    @requestbody---接受前端传json对象并绑定javabean----https://blog.csdn.net/ljxbbss/article/details/74452326 最近代码 ...

  8. springboot 服务端获取前端传过来的参数7种方式

    下面为7种服务端获取前端传过来的参数的方法  1.直接把表单的参数写在Controller相应的方法的形参中,适用于GET 和 POST请求方式 这种方式不会校验请求里是否带参数,即下面的userna ...

  9. vue+element实现分页--之--前端分页

    效果图: 访问的数据量小,一次返回所有数据,再次利用elementUI-Table 和el-pagination组件进行展示,关键点事数据的筛选 官网的完整案例 <div class=" ...

随机推荐

  1. 【转载】ssh-keygen 基本用法

    [转载]ssh-keygen 基本用法 原文地址:https://www.liaohuqiu.net/cn/posts/ssh-keygen-abc/ ssh 公钥认证是ssh认证的方式之一.通过公钥 ...

  2. Node入门教程(12)第十章:Node的HTTP模块

    Ryan Dahl开发node的初衷就是:把Nginx非阻塞IO功能和一个高度封装的WEB服务器结合在一起的东东.所以Node初衷就是为了高性能的Web服务器去的,所以:Node的HTTP模块也是核心 ...

  3. headfirst python 07 ~ 08

    Web 不论你在 web 上做什么, 都离不开请求和响应, web请求作为某个用户交互的结果由web浏览器发送到web服务器, 在web服务器上, 会生成web响应(或应答)并发回到 web 浏览器. ...

  4. c++ linux socket编程 c++网络编程

    声明:大部分代码来自这篇博客http://www.cnblogs.com/diligenceday/p/6241021.html, 感谢博主 思路: 思路很重要呦~~~ socket详细信息,思路:h ...

  5. 多线程开发之一 NSThread

    每个 iOS 应用程序都有个专门用来更新显示 UI 界面.处理用户的触摸事件的主线程,因此不能将其他太耗时的操作放在主线程中执行,不然会造成主线程堵塞(出现卡机现象),带来不好的用户体验. 一般的解决 ...

  6. Tomcat容器做到自我保护,设置最大连接数(服务限流:tomcat请求数限制)

    http://itindex.net/detail/58707-%E5%81%87%E6%AD%BB-tomcat-%E5%AE%B9%E5%99%A8 为了确保服务不会被过多的http长连接压垮,我 ...

  7. DOTween 使用方法

    参考链接: http://dotween.demigiant.com/documentation.php https://www.cnblogs.com/backlighting/p/5344047. ...

  8. AES和RSA算法的demo代码

    aes代码示例: package com.autoyol.util.security.test; import java.security.Key; import java.security.NoSu ...

  9. WampServer自己DIY添加apache、php、mysql版本

    下载自己需要的apache版本. 下载地址: http://httpd.apache.org/download.cgi http://www.apachelounge.com/download/ 解压 ...

  10. Hadoop2.7.3+HBase1.2.5+ZooKeeper3.4.6搭建分布式集群环境

    Hadoop2.7.3+HBase1.2.5+ZooKeeper3.4.6搭建分布式集群环境 一.环境说明 个人理解:zookeeper可以独立搭建集群,hbase本身不能独立搭建集群需要和hadoo ...