• 分页


    DAO  层接口
    package com.itheima.dao;
    
    import java.util.List;
    
    import com.itheima.domain.Account;
    
    public interface AccountDao {
    
        /**
         * 获取表的总记录数
         * @return
         */
        public int getCount();
        
        /**
         * 分页查询
         * @param startIndex
         * @param size
         * @return
         */
        public List<Account> getAccountByPage(int startIndex,int size);
    }

    DAO层实现

    package com.itheima.dao.impl;
    
    import java.sql.SQLException;
    import java.util.List;
    
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    import org.apache.commons.dbutils.handlers.ScalarHandler;
    
    import com.itheima.dao.AccountDao;
    import com.itheima.domain.Account;
    import com.itheima.utils.C3P0Util;
    
    public class AccountDaoImpl implements AccountDao {
    
        private QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
        public int getCount() {//获取总数
            try {
                int count = ((Long)qr.query("select count(*) from account",new ScalarHandler())).intValue();
                return count;
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    
        public List<Account> getAccountByPage(int startIndex, int size) {//获取当前页面的数据
            try {
                
                return  qr.query("select * from account limit ?,?",new BeanListHandler<Account>(Account.class),startIndex,size);
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    
    }

    service 层接口

    package com.itheima.service;
    
    import com.itheima.utils.PageBean;
    
    public interface AccountService {
    
        /**
         * 进行分页查询
         * @param pb 引用数据类型,在方法体内做的修改,可以保留
         */
        public void findAccountByPage(PageBean pb);
    }

    service层实现

    package com.itheima.service.impl;
    
    import java.util.List;
    
    import com.itheima.dao.AccountDao;
    import com.itheima.dao.impl.AccountDaoImpl;
    import com.itheima.domain.Account;
    import com.itheima.service.AccountService;
    import com.itheima.utils.PageBean;
    
    public class AccountServiceImpl implements AccountService {
    
        private AccountDao dao = new AccountDaoImpl();
        public void findAccountByPage(PageBean pb) {
            int count = dao.getCount();//总记录数
            pb.setTotalRecordes(count);//设置总记录数
            
            List<Account> list = dao.getAccountByPage(pb.getStartIndex(), pb.getPageSize());//查询出当前页的记录
            pb.setRecordes(list);//设置当前页的记录
        }
    
    }

    pagebeans

    package com.itheima.utils;
    
    import java.io.Serializable;
    import java.util.List;
    
    public class PageBean implements Serializable {
    
        private int pageNo=1;//当前页
        private int pageSize=3;//每页显示的记录数
        private int prep;//上一页
        private int nextp;//下一页
        private int totalPage;//总页数
        private int totalRecordes;//总记录数  
        
        private List recordes;// 当前页的记录
        private int startIndex;//当前页的第一条记录的索引   limit startIndex,size
        
        
        //为了加入滚动数字
        private int beginPageno;
        private int endPageno;
        
        
        
        public int getBeginPageno() {
            //设置页码    1  2 3 4 5 6 7 8 9 (看到的数字)
            if(pageNo<=9){
                beginPageno=1;
                endPageno = this.getTotalPage();//总记录数
            }else{
                //说明当前页已超出9
                beginPageno=pageNo-4;
                endPageno = pageNo+4;
                
                if(beginPageno<=0)
                    beginPageno=1;
                if(endPageno>getTotalPage())
                    endPageno=this.getTotalPage();
                
            }
            return beginPageno;
        }
        public void setBeginPageno(int beginPageno) {
            this.beginPageno = beginPageno;
        }
        public int getEndPageno() {
            return endPageno;
        }
        public void setEndPageno(int endPageno) {
            this.endPageno = endPageno;
        }
        private String url;
        public int getPageNo() {
            return pageNo;
        }
        public void setPageNo(int pageNo) {
            this.pageNo = pageNo;
        }
        public int getPageSize() {
            return pageSize;
        }
        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }
        public int getPrep() {
            if(pageNo<=1){
                prep=1;
            }else{
                prep=pageNo-1;
            }
            return prep;
        }
        public void setPrep(int prep) {
            this.prep = prep;
        }
        public int getNextp() {
            if(pageNo>=this.getTotalPage()){
                nextp=this.getTotalPage();
            }else{
                //没有超出边界
                nextp=pageNo+1;
            }
            return nextp;
        }
        public void setNextp(int nextp) {
            this.nextp = nextp;
        }
        public int getTotalPage() {
            if(totalRecordes%pageSize==0){
                totalPage = totalRecordes/pageSize;
            }else{
                totalPage = totalRecordes/pageSize+1;
            }
            return totalPage;
        }
        public void setTotalPage(int totalPage) {
            this.totalPage = totalPage;
        }
        public int getTotalRecordes() {
            return totalRecordes;
        }
        public void setTotalRecordes(int totalRecordes) {
            this.totalRecordes = totalRecordes;
        }
        public List getRecordes() {
            return recordes;
        }
        public void setRecordes(List recordes) {
            this.recordes = recordes;
        }
        public int getStartIndex() {
            startIndex = (pageNo-1)*pageSize;
            return startIndex;
        }
        public void setStartIndex(int startIndex) {
            this.startIndex = startIndex;
        }
        public String getUrl() {
            return url;
        }
        public void setUrl(String url) {
            this.url = url;
        }
        
    }

    sevlet

    package com.itheima.web.servlet;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.itheima.service.AccountService;
    import com.itheima.service.impl.AccountServiceImpl;
    import com.itheima.utils.PageBean;
    
    public class ControllerServlet extends HttpServlet {
    
        private AccountService as = new AccountServiceImpl();
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            //1.乱码
            response.setContentType("text/html;charset=UTF-8");
            //2.生成一个分页组件对象
            PageBean pb = new PageBean();
            //3.设置一个新的当前页  (假设页面上传入进来当前页的参数名pageNo)
            String pageNo = request.getParameter("pageNo");
            if(pageNo!=null && !"".equals(pageNo)){
                pb.setPageNo(Integer.parseInt(pageNo));//设置当前页
            }
            //4.调用业务方法,实现分页查询
            as.findAccountByPage(pb);
            
            //4.1补充url的设置
            pb.setUrl(request.getContextPath()+"/servlet/ControllerServlet");
            //5.将分页组件放入request域中
            request.setAttribute("page", pb);
            //6.转发到页面
            request.getRequestDispatcher("/list.jsp").forward(request, response);
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            this.doGet(request, response);
    
        }
    
    }

    jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title></title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <style type="text/css">
            div{
                text-align:center;
            }
        </style>
          </head>
      <body>
           <table border="1" width="600" align="center">
                    <tr>
                        <td>ID</td>
                        <td>Name</td>
                        <td>Money</td>
                        <td>操作</td>
                    </tr>
                    <c:forEach items="${requestScope.page.recordes }" var="account">
                        <tr>
                            <td>${account.id }</td>
                            <td>${account.name }</td>
                            <td>${account.money }</td>
                            <td><a href="">更新</a></td>
                        </tr>
                    </c:forEach>
            </table>
            
            <%@include file="/commons/page.jsp" %>
      </body>
    </html>

    js p(提取)

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <div>
                 当前${page.pageNo } 页/共${page.totalPage } 页 &nbsp; &nbsp; &nbsp;  
                 <a href="${page.url }?pageNo=${page.prep}">上一页</a>  &nbsp; &nbsp; &nbsp;   
                 
                 <!-- 滚动数字 -->
                 <c:forEach begin="${page.beginPageno }" end="${page.endPageno }" var="no">
                     <a href="${page.url }?pageNo=${no}">${no }</a> &nbsp; &nbsp;
                 </c:forEach> &nbsp; &nbsp; &nbsp;
                 
                <a href="${page.url }?pageNo=${page.nextp}">下一页</a>&nbsp; &nbsp; &nbsp;  
                选择<select onchange="changePage(this)">
                        <c:forEach begin="1" end="${page.totalPage }" var="i">
                                <option value="${i }" ${page.pageNo==i?"selected='selected'":"" }>${i }</option>
                        </c:forEach>
                </select><script>
                        function changePage(objOpt){
                            window.location.href="${page.url}?pageNo="+objOpt.value;
                        }
                </script>
            </div>
  • 相关阅读:
    表:t_category
    使用 Javascript 代码,增加 HTML 新元素(节点)
    Kind Editor 笔记
    [ 摘 ] 对 js 运算符 “||” 和 “&&” 的总结
    Oracle 的分页查询,myBatis 配置文件
    ExtJS 的工具条及菜单
    发一个 ExtJS 4.2.0 的页面源码(规则比对公式 的设置页面)
    MyBatis 中的 set 元素用法(MyBatis 3.1.1)
    MySQL 存储过程,一个 4 层的游标,用于规则比对,不知道性能如何。
    jmeter从文件中读取参数
  • 原文地址:https://www.cnblogs.com/baijin05/p/5081844.html
Copyright © 2020-2023  润新知