分页查询思路

        

      

问题:  服务器向浏览器想用数据很多的时候可以对数据进行封装。

     domain层  封装数据   

package com.baidu.domain;

import java.util.List;

public class PageBean {
//总页数
private int totalPage;
//总条数
private int totalCount;
//数据集合
private List<Product> list;
//当前页
private int currPage;
//页数显示信息数
private int pageSize;
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public List<Product> getList() {
return list;
}
public void setList(List<Product> list) {
this.list = list;
}
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} }

  

    浏览器只需要使用项服务器发送页数,服务器会发送pageBean对象进行返回。

    Service层  处理业务

    public PageBean getPageListProduct(int currPage) throws Exception {
PageBean pb=new PageBean();
//设置页面数
pb.setPageSize(10);
pb.setCurrPage(currPage);
//获取集合列表
List<Product> list = pd.getPageListProduct(pb); pb.setList(list);
for (Product product : pb.getList()) {
System.out.println(product);
}
//获取总条数
int l = (int) pd.getPageProductAcount();
pb.setTotalCount(l);
//获取总页数
if (l%10==0) {
pb.setTotalPage(l/10);
}else{
pb.setTotalPage((l/10)+1);
}
return pb; }

  dao层

     public List<Product> getPageListProduct(PageBean pb) throws SQLException {
QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
String sql="select * from product limit ?,?";
List<Product> list = qr.query(sql, new BeanListHandler<Product>(Product.class),(pb.getCurrPage()-1)*pb.getPageSize(),pb.getPageSize());
return list;
}

  

public long getPageProductAcount() throws SQLException {
QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
String sql="Select count(*) from product";
long l = (Long)qr.query(sql, new ScalarHandler());
return l; }

    JDBCUtils

public class JDBCUtils {
private static final ComboPooledDataSource cpd=new ComboPooledDataSource(); /**
* 获取连接对象
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException{
Connection conn = cpd.getConnection();
return conn;
}
/**
* 获取数据源
* @return
*/
public static DataSource getDataSource(){
return cpd;
}
}

  

  

day26(分页查询)的更多相关文章

  1. JdbcTemplate+PageImpl实现多表分页查询

    一.基础实体 @MappedSuperclass public abstract class AbsIdEntity implements Serializable { private static ...

  2. 用Hibernate和Struts2+jsp实现分页查询、修改删除

    1.首先用get的方法传递一个页数过去 2.通过Struts2跳转到Action 3.通过request接受主页面index传过的页数,此时页数是1, 然后调用service层的方法获取DAO层分页查 ...

  3. MySQL、Oracle和SQL Server的分页查询语句

    假设当前是第PageNo页,每页有PageSize条记录,现在分别用Mysql.Oracle和SQL Server分页查询student表. 1.Mysql的分页查询: SELECT * FROM s ...

  4. 分页查询和分页缓存查询,List<Map<String, Object>>遍历和Map遍历

    分页查询 String sql = "返回所有符合条件记录的待分页SQL语句"; int start = (page - 1) * limit + 1; int end = pag ...

  5. mysql 分页查询

    mysql,; : mysql,; -last. //如果只给定一个参数,它表示返回最大的记录行数目: mysql; 个记录行 ,n. 动态传参的分页查询 SELECT * FROM table LI ...

  6. MongoDB 分页查询的方法及性能

    最近有点忙,本来有好多东西可以总结,Redis系列其实还应该有四.五.六...不过<Redis in Action>还没读完,等读完再来总结,不然太水,对不起读者. 自从上次Redis之后 ...

  7. .NET平台开源项目速览(7)关于NoSQL数据库LiteDB的分页查询解决过程

    在文章:这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧!(第二辑) 与 .NET平台开源项目速览(3)小巧轻量级NoSQL文件数据库LiteDB中,介绍了LiteDB的基本使用情况以及部 ...

  8. SubSonic3.0插件分页查询速度测试

    使用SubSonic3.0一段时间了,一直都想找机会测试一下各种查询分页速度,对比一下插件的查询效率到底怎么样,所以昨天写好了测试程序,准备好1K.1W.10W.50W和100W记录的数据表,早上详细 ...

  9. Node.js、express、mongodb 实现分页查询、条件搜索

    前言 在上一篇Node.js.express.mongodb 入门(基于easyui datagrid增删改查) 的基础上实现了分页查询.带条件搜索. 实现效果 1.列表第一页. 2.列表第二页 3. ...

随机推荐

  1. three.map.control

    网址:https://github.com/anvaka/three.map.control 在threejs群里发现的一个很有意思的问题之前没有接触过: 存在的问题:  我在微信小游戏中,用orbi ...

  2. 已经卸载了hyper-v 仍然提示 vmware 与 hyper-v 不兼容;天天模拟器,提示VT模式没有开启,BIOS里面已经设置过了

    环境win10,vm的失败和模拟器的失败都是hyper-v冲突导致的...网上看了很多千篇一律的都只是提到了卸载hyper-v,实际上我电脑仅仅卸载hyper-v是不够的. 解决办法: 卸载 hype ...

  3. Sublime Text3 常用快捷键必看

    Sublime Text3 常用快捷键必看  https://blog.csdn.net/md1688/article/details/53043525

  4. 10.8H5日记

    1.网页中去滚动条的属性 overflow:hidden: overflow-x:hidden:水平超出隐藏 2.z-index 层次叠加 元素重叠 谁的值大谁就在上边 当 当前元素有float和父级 ...

  5. Win7自带功能,刻录光盘遇到的问题

    Win7系统的可以使用系统自带有光盘刻录功能来刻录光盘. 把一张空白光盘放入刻录机,打开“计算机”窗口,双击刻录机图标,弹出“刻录光盘”对话框,选择刻录类型.这里有两个选项:一个是“类似于USB闪存驱 ...

  6. iOS.BackgroundTask

    Background Task 的运行时间在iOS 6及以前有大约10分钟左右,在iOS 7中有180秒. Reference: 1. Multitasking in iOS 7 http://www ...

  7. VirtualBox安装android-x86-4.4-r2

    https://jingyan.baidu.com/album/a681b0de1373133b184346cf.html?picindex=10

  8. Spring MVC 注解类型

    Spring 2.5 引入了注解 基于注解的控制器的优势 1. 一个控制器类可以处理多个动作,而一个实现了 Controller 接口的控制器只能处理一个动作 2. 基于注解的控制器的请求映射不需要存 ...

  9. zz Alex's BLOG 串口连接

    using System; using System.Collections.Generic;using System.ComponentModel;using System.Data;using S ...

  10. Spring 系列教程之容器的功能

    Spring 系列教程之容器的功能 经过前面几章的分析,相信大家已经对 Spring 中的容器功能有了简单的了解,在前面的章节中我们一直以 BeanFacotry 接口以及它的默认实现类 XmlBea ...