<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] # 分頁 ## 步驟 1 : 先運行,看到效果,再學習 先將完整的項目(向老師要相關資料),配置運行起來,確認可用之后,再學習做了哪些步驟以達到這樣的效果。 ## 步驟 2 : 模仿和排錯 在確保可運行項目能夠正確無誤地運行之后,再嚴格照著教程的步驟,對代碼模仿一遍。 模仿過程難免代碼有出入,導致無法得到期望的運行結果,此時此刻通過比較**正確答案** ( 可運行項目 ) 和自己的代碼,來定位問題所在。 采用這種方式,**學習有效果,排錯有效率**,可以較為明顯地提升學習速度,跨過學習路上的各個檻。 ## 步驟 3 : 基于前面的知識點 本知識點基于SSM整合進行 ## 步驟 4 : 本知識點效果 訪問頁面看到如圖所示效果 `http://127.0.0.1:8080/ssm/listCategory` ![](https://box.kancloud.cn/c1291ffeba648ffa5cf55216204aac2b_620x316.png) ## 步驟 5 : 分頁類:Page Page類用于存放分頁信息: start: 開始位置 count: 每頁的個數 last: 最后一頁的位置 caculateLast()方法: 通過總數total和每頁的個數計算出最后一頁的位置 ~~~ package com.dodoke.util; public class Page { private int start = 0; private int count = 5; private int last = 0; public int getStart() { return start; } public void setStart(int start) { this.start = start; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public int getLast() { return last; } public void setLast(int last) { this.last = last; } public void calculateLast(int total) { // 假設總數是50,是能夠被5整除的,那么最后一頁的開始就是45 if (0 == total % count) { last = total - count; } // 假設總數是51,不能夠被5整除的,那么最后一頁的開始就是50 else { last = total - total % count; } } } ~~~ ## 步驟 6 : Category.xml 修改list,根據當有分頁信息的時候,進行分頁查詢 增加total sql語句 ~~~ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.dodoke.mapper.CategoryMapper"> <insert id="add" parameterType="Category"> insert into category(name) values(#{name}) </insert> <delete id="delete" parameterType="Category"> delete from category where id=#{id} </delete> <update id="update" parameterType="Category"> update category set name=#{name} where id=#{id} </update> <select id="get" parameterType="int" resultType="Category"> select * from category where id=#{id} </select> <select id="list" resultType="Category"> select * from category <if test="start!=null and count!=null"> limit #{start},#{count} </if> </select> <select id="total" resultType="int"> select count(*) from category </select> </mapper> ~~~ ## 步驟 7 : CategoryMapper 增加total方法用于調用Category.xml 中total對應的sql語句 增加 list(Page page),根據分頁來查詢數據 ~~~ package com.dodoke.mapper; import java.util.List; import com.dodoke.pojo.Category; import com.dodoke.util.Page; public interface CategoryMapper { public int add(Category category); public void delete(int id); public int update(Category category); public Category get(int id); public List<Category> list(); public List<Category> list(Page page); public int total(); } ~~~ ## 步驟 8 : CategoryService 增加total用于獲取所有 增加 list(Page page),根據分頁來查詢數據 ~~~ package com.dodoke.service; import java.util.List; import com.dodoke.pojo.Category; import com.dodoke.util.Page; public interface CategoryService { List<Category> list(); int total(); List<Category> list(Page page); } ~~~ ## 步驟 9 : CategoryServiceImpl 實現total()和list(Page page) 方法 ~~~ package com.dodoke.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.dodoke.mapper.CategoryMapper; import com.dodoke.pojo.Category; import com.dodoke.service.CategoryService; import com.dodoke.util.Page; @Service public class CategoryServiceImpl implements CategoryService { @Autowired CategoryMapper categoryMapper; @Override public List<Category> list() { // TODO Auto-generated method stub return categoryMapper.list(); } @Override public int total() { return categoryMapper.total(); } @Override public List<Category> list(Page page) { return categoryMapper.list(page); } } ~~~ ## 步驟 10 : CategoryController 1. 修改listCategory,接受分頁信息的注入 ` listCategory(Page page)` 2. 根據分頁對象,進行查詢獲取對象集合cs `List<Category> cs= categoryService.list(page);` 3. 根據總數,計算出最后一頁的信息 `int total = categoryService.total();` ~~~ package com.dodoke.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.dodoke.pojo.Category; import com.dodoke.service.CategoryService; import com.dodoke.util.Page; //告訴spring mvc這是一個控制器類 @Controller @RequestMapping("") public class CategoryController { @Autowired CategoryService categoryService; @RequestMapping("listCategory") public ModelAndView listCategory(Page page) { int total = categoryService.total(); page.calculateLast(total); // 邊界條件判斷 // 首頁,點擊上一頁,頁面沒有負數 int start = page.getStart(); if (start < 0) { page.setStart(0); } // 末頁,點擊下一頁,開始頁面不能超過最后一頁 if (start > page.getLast()) { page.setStart(page.getLast()); } ModelAndView mav = new ModelAndView(); List<Category> cs = categoryService.list(page); // 放入轉發參數 mav.addObject("cs",cs); // 放入jsp路徑 mav.setViewName("listCategory"); return mav; } } ~~~ ## 步驟 11 : listCategory.jsp 修改listCategory.jsp,分別提供首頁,上一頁,下一頁,末頁等連接 ~~~ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <table border='1' cellspacing='0'> <tr> <td>id</td> <td>name</td> </tr> <c:forEach items="${cs}" var="c" varStatus="st"> <tr> <td>${c.id}</td> <td>${c.name}</td> </tr> </c:forEach> </table> <div style=""> <a href="?start=0">首 頁</a> <a href="?start=${page.start - page.count }">上一頁</a> <a href="?start=${page.start + page.count }">下一頁</a> <a href="?start=${page.last }">末 頁</a> </div> </body> </html> ~~~ ## 步驟 12 : 增加100個對象,用于測試 修改MybatisTest 類,新增100個對象,用于分頁測試 ~~~ package com.dodoke.test; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.dodoke.mapper.CategoryMapper; import com.dodoke.pojo.Category; import com.dodoke.util.Page; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class MybatisTest { @Autowired private CategoryMapper categoryMapper; @Test public void testAdd() { for (int i = 0; i < 100; i++) { Category category = new Category(); category.setName("new Category"); categoryMapper.add(category); } } @Test public void testTotal() { int total = categoryMapper.total(); System.out.println(total); } @Test public void testList() { Page p = new Page(); p.setStart(2); p.setLast(3); List<Category> cs = categoryMapper.list(p); for (Category c : cs) { System.out.println(c.getName()); } } } ~~~ ## 步驟 13 : 測試 訪問頁面看到如圖所示效果 `http://127.0.0.1:8080/ssm/listCategory` ![](https://box.kancloud.cn/c1291ffeba648ffa5cf55216204aac2b_620x316.png) ## 常見問題 1. 為什么在Controller類里沒有把page放入mav里,jsp里也能用到呢? > 這句mav.addObject("page", page);框架已經省略了,可寫可不寫的,作為參數會被自動放進去的. 2. 我看到URL在點擊鏈接之后變了,這里是提交了start么,提交了之后又是怎么把它賦給page對象的呢(像表單提交一樣) ~~~ <a href="?start=${page.start-page.count}">上一頁</a> ~~~ >是的,就是因為提交了start參數。 springmvc框架會自動把start參數注入到Page對象的start屬性上。 ## 補充說明 > 分頁界限判斷,還可以使用JSTL中的if標簽進行判斷,提供個思路 > ~~~ <div style=""> <a href="?start=0">首 頁</a> <c:if test="${page.start-page.count>=0}"> <a href="?start=${page.start-page.count}">上一頁</a> </c:if> <c:if test="${page.start-page.count<0}"> <a href="javascript:void(0)">上一頁</a> </c:if> <c:if test="${page.start+page.count<=page.last}"> <a href="?start=${page.start+page.count}">下一頁</a> </c:if> <c:if test="${page.start+page.count>page.last}"> <a href="javascript:void(0)">下一頁</a> </c:if> <a href="?start=${page.last}">末頁</a> </div> ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看