2018-03-04 web用戶項(xiàng)目(三):實(shí)現(xiàn)用戶信息分頁查詢

筆記如下

  • 思路:
    1.每一頁里有十條記錄,每頁就是一個(gè)PageBean,封裝了總記錄數(shù),當(dāng)前是第幾頁,每頁條數(shù),當(dāng)前是第幾頁的數(shù)據(jù)
2018-03-01_214242.png

2.兩個(gè)公式


2018-03-01_214242.png
  • *jsp
<body>
    <c:if test="${empty pageBean.customers}">
        當(dāng)前頁沒有數(shù)據(jù)
    </c:if>
    <c:if test="${not empty pageBean.customers}">
        <h3 style="text-align: center; ">當(dāng)前是${pageBean.pageNum}頁數(shù)據(jù)</h3>
        <table border="1" width="100%"> 
            <tr>
                <th>客戶姓名</th>
                <th>客戶性別</th>
                <th>客戶生日</th>
                <th>客戶郵箱</th>
                <th>客戶手機(jī)</th>
                <th>客戶愛好</th>
                <th>客戶類型</th>
                <th>客戶描述</th>
            </tr>
            <c:forEach items="${pageBean.customers}" var="customer">
                <tr>
                    
                    <td>${customer.name}</td>
                    <td>${customer.gender}</td>
                    <td>${customer.birthday}</td>
                    <td>${customer.email}</td>
                    <td>${customer.cellphone}</td>
                    <td>${customer.preference}</td>
                    <td>${customer.type}</td>
                    <td>${customer.description}</td>
                </tr>
            
            </c:forEach>
        </table>
        <br/>
        <div style="text-align: center; ">
            <c:if test="${pageBean.pageNum != 1 }">
                <a href="${pageContext.request.contextPath}/pagequery?pagenum=1">首頁</a> 
                <a  href="${pageContext.request.contextPath}/pagequery?pagenum=${pageBean.pageNum-1}">上一頁</a> 
            </c:if>
            <!-- 參考百度左五右四 -->
            <c:forEach begin="${pageBean.pageNum-5>0?pageBean.pageNum-5:1}" end="${pageBean.pageNum+4<pageBean.totalPageNum?pageBean.pageNum+4:pageBean.totalPageNum}" var="i">
                <c:if test="${pageBean.pageNum == i}">
                    <font color="red">${i}</font>
                </c:if><c:if test="${pageBean.pageNum != i}">
                    
                    <a href="${pageContext.request.contextPath}/pagequery?pagenum=${i}">${i}</a>
                </c:if>
            
            </c:forEach>
            
            <c:if test="${pageBean.pageNum != pageBean.totalPageNum }">
                <a  href="${pageContext.request.contextPath}/pagequery?pagenum=${pageBean.pageNum+1}">下一頁</a> 
                <a href="${pageContext.request.contextPath}/pagequery?pagenum=${pageBean.totalPageNum}">尾頁</a> 
            </c:if>
        </div>
        
    </c:if>
    
</body>
  • web層(PageQueryServlet.java)
//拿到需要看到頁是那一頁
        String pagenum = request.getParameter("pagenum");
        
        //調(diào)用業(yè)務(wù)層去查詢目標(biāo)頁的數(shù)據(jù)
        CustomerService cs = new CustomerService();
        
        //返回當(dāng)前頁的目標(biāo)數(shù)據(jù)是不夠的,還要做導(dǎo)航條
        //為了做分頁,需要引入一個(gè)新的javaBean
        PageBean pageBean =  cs.pageQuery(pagenum);
        
        //將pagebean存到request域中
        request.setAttribute("pageBean", pageBean);
        request.getRequestDispatcher("/bean.jsp").forward(request, response);
  • 業(yè)務(wù)層(CustomerService.java)
//完成分頁查詢的業(yè)務(wù)方法:
    public PageBean pageQuery(String num) {
        
        int numberPerPage = 10;//默認(rèn)每頁十條
        
        int pageNum = Integer.parseInt(num);//當(dāng)前頁是那一頁
        
          
          //從數(shù)據(jù)庫里查找總記錄數(shù)
        int totalRecordsCount = cdao.getToltalCount();
        
        //運(yùn)用公式
        int totalPageNum = (totalRecordsCount + numberPerPage-1)/numberPerPage;//總頁數(shù)
        
        //select * from customers limit ?,?
        int startIndex = (pageNum-1)*numberPerPage;

        //從數(shù)據(jù)庫里查找10條記錄
        List<Customer> customers = cdao.getCurrectPageData(startIndex,numberPerPage);
        
        //封裝數(shù)據(jù)
        PageBean pageBean = new PageBean();
        pageBean.setNumberPerPage(numberPerPage);
        pageBean.setCustomers(customers);
        pageBean.setPageNum(pageNum);
        pageBean.setTotalPageNum(totalPageNum);
        pageBean.setTotalRecordsCount(totalRecordsCount);
        
        return pageBean;
    }

dao層(CustomerDaoImpl.java)

    //獲得總記錄條數(shù)
    @Override
    public int getToltalCount() {
        
        QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
        
        try {
         long count =  (long) runner.query("select count(*) from customers", new ScalarHandler());
         return (int)count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        
    }
    
    //查詢目標(biāo)頁的數(shù)據(jù)返回
    @Override
    public List<Customer> getCurrectPageData(int startIndex, int numberPerPage) {
        
        QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
        
        String sql = "select * from customers limit ?,?";
        try {
            return runner.query(sql, new BeanListHandler<Customer>(Customer.class),startIndex,numberPerPage);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        
        
        
    }

實(shí)現(xiàn)效果:


4.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蚤吹,一起剝皮案震驚了整個(gè)濱河市钥顽,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌疑故,老刑警劉巖轻专,帶你破解...
    沈念sama閱讀 223,207評論 6 521
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件魔眨,死亡現(xiàn)場離奇詭異滴劲,居然都是意外死亡苏章,警方通過查閱死者的電腦和手機(jī)寂嘉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,455評論 3 400
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來布近,“玉大人垫释,你說我怎么就攤上這事〕徘疲” “怎么了棵譬?”我有些...
    開封第一講書人閱讀 170,031評論 0 366
  • 文/不壞的土叔 我叫張陵,是天一觀的道長预伺。 經(jīng)常有香客問我订咸,道長曼尊,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,334評論 1 300
  • 正文 為了忘掉前任脏嚷,我火速辦了婚禮骆撇,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘父叙。我一直安慰自己神郊,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,322評論 6 398
  • 文/花漫 我一把揭開白布趾唱。 她就那樣靜靜地躺著涌乳,像睡著了一般。 火紅的嫁衣襯著肌膚如雪甜癞。 梳的紋絲不亂的頭發(fā)上夕晓,一...
    開封第一講書人閱讀 52,895評論 1 314
  • 那天,我揣著相機(jī)與錄音悠咱,去河邊找鬼蒸辆。 笑死,一個(gè)胖子當(dāng)著我的面吹牛析既,可吹牛的內(nèi)容都是我干的躬贡。 我是一名探鬼主播,決...
    沈念sama閱讀 41,300評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼渡贾,長吁一口氣:“原來是場噩夢啊……” “哼逗宜!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起空骚,我...
    開封第一講書人閱讀 40,264評論 0 277
  • 序言:老撾萬榮一對情侶失蹤纺讲,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后囤屹,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體熬甚,經(jīng)...
    沈念sama閱讀 46,784評論 1 321
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,870評論 3 343
  • 正文 我和宋清朗相戀三年肋坚,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了乡括。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,989評論 1 354
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡智厌,死狀恐怖诲泌,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情铣鹏,我是刑警寧澤敷扫,帶...
    沈念sama閱讀 36,649評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站诚卸,受9級特大地震影響葵第,放射性物質(zhì)發(fā)生泄漏绘迁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,331評論 3 336
  • 文/蒙蒙 一卒密、第九天 我趴在偏房一處隱蔽的房頂上張望缀台。 院中可真熱鬧,春花似錦哮奇、人聲如沸膛腐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,814評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽依疼。三九已至,卻和暖如春而芥,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背膀值。 一陣腳步聲響...
    開封第一講書人閱讀 33,940評論 1 275
  • 我被黑心中介騙來泰國打工棍丐, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人沧踏。 一個(gè)月前我還...
    沈念sama閱讀 49,452評論 3 379
  • 正文 我出身青樓歌逢,卻偏偏與公主長得像,于是被迫代替她去往敵國和親翘狱。 傳聞我的和親對象是個(gè)殘疾皇子秘案,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,995評論 2 361

推薦閱讀更多精彩內(nèi)容