分页查询


总结:1.分页查询:1.先建立一个封装分页所需数据的类,1.当前页:private Intager currentPage; 每页显示的条数:private Intager currentCount; 总页数:private Integer tatalPage
总条数:private Intafer totalCount; 每页显示的数据:private List<T> list=new private<T>();
2.建立servlet接收jsp传来的数据:1.从前台获取当前页:String currtPagestr=request.getParameter("currentPage");判断如果当前页是空的那么自动赋值1
if(currentPagestr==null){ currentPage="1";} int currentPage=Integer.parseInt(currentPagestr);手动设置每页显示的条数int currentCount=12;
调用service方法:PageBean<Product> page=productService.getPageBean(currentPage, currentCount);
向域中设置值:request.setAttribute("Page",page); 请求转发:request.getRequestDispatcher("/product_list.jsp").forword(request,response);
3.Service层(为了封装一个PageBean给servlet):先new实例:PageBean<Product> page=new PageBean<Product>();封装当前页:page.setCurrentPage(currentPage)
封装每一页显示的条数:page.setCurrentCount(currentCount); 封装总条数:int totalcount=0; totalcount=productDao.gettotalCount(); page.setTotalCount(tatlacount)
封装总页数:int totalPage=(int)Math.ceil((tatlacount)*1.0/currentCount);因为这里要向上专的先转成double类型的所以需要*1.0才能转成double;得到的值需要向上取 整,ceil(); 因为得到的是double类型所以需要强转为int; page.setTotalPage(totalPage);封装每页显示的数据select * from product limit起始页,每页显示的条数
计算起始页=(当前页-1)*每页显示的条数: int index=(currentPage-1)*currentCount;
List<public> list=null; list=productDao.getPage(index, currentCount); page.setList(list); return page;
Dao层:1.查询总条数:QueryRunner qr=new QueryRunner(MyDBUtils.getDataSource()); String sql="select count(*) from product";
Long count=qr.query(sql,new ScalarHandler<long>()) return count.intValue();因为这里调用的ScalarHandler返回的是long类型,所以这里需要强转才行.
2.查询要封装的内容:QueryRunner qr=new QueryRunner(MyDBUtils.getDataSource()); String sql="select * from product limit ?,?";
List<Product> list=qr.query(new BeanListHandler<Product>(Product.class),index,currentCount) return list;




 

posted @ 2019-09-27 15:01  韩凯  阅读(721)  评论(0编辑  收藏  举报