手動SQL方式分頁 java
一.首先來看看最重要的Page類。git
首先咱們讓start默認爲0,count=5。count爲一頁的容量,而令start爲0,是若是瀏覽器訪問不輸入start參數就默認爲從頭開始瀏覽頁信息github
而public void caculateLast是用來獲取最後一頁的開始 ,這樣就能夠一鍵點到最後一頁web
public class Page { int start=0; int count = 5; 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 caculateLast(int total) { // 假設總數是50,是可以被5整除的,那麼最後一頁的開始就是45 if (0 == total % count) last = total - count; // 假設總數是51,不可以被5整除的,那麼最後一頁的開始就是50 else last = total - total % count; } }
2、而後再看控制器spring
控制器先攔截路徑/listCategory的訪問,而後Spring將自動注入一個Page,如有start參數則是start參數,若沒有則start=0瀏覽器
package com.how2java.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.how2java.pojo.Category; import com.how2java.service.CategoryService; import com.how2java.util.Page; // 告訴spring mvc這是一個控制器類 @Controller @RequestMapping("") public class CategoryController { @Autowired CategoryService categoryService; @RequestMapping("listCategory") public ModelAndView listCategory(Page page){ ModelAndView mav = new ModelAndView(); List<Category> cs= categoryService.list(page); int total = categoryService.total(); page.caculateLast(total); // 放入轉發參數 mav.addObject("cs", cs); // 放入jsp路徑 mav.setViewName("listCategory"); return mav; } }
其中經過.list.(page)來獲取查詢也得Category內容,這裏跳過中間步驟直接看其SQL語句mvc
這裏經過start 和count則可獲取Page裏的內容,並List來捕捉獲取集合。app
<select id="list" resultType="Category"> select * from category_ <if test="start!=null and count!=null"> limit #{start},#{count} </if> </select>
而int total = categoryService.total();則是將最後一頁的第一個內容注入Page類jsp
3、最後則跳轉到頁面 這裏代碼就不解釋了this
<div style="width:500px;margin:0px auto;text-align:center">
<table align='center' 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="text-align:center">
<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>
</div>
而後還能夠經過PageHelper來簡易完成分頁
applicationContext添加
<property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <!--使用下面的方式配置參數,一行配置一個 --> <value> </value> </property> </bean> </array> </property>
控制器能夠簡化爲 直接經過四、五、6行完成分頁以及獲取總條數
1 public ModelAndView listCategory(Page page){ 2 3 ModelAndView mav = new ModelAndView(); 4 PageHelper.offsetPage(page.getStart(),5); 5 List<Category> cs= categoryService.list(); 6 int total = (int) new PageInfo<>(cs).getTotal(); 7 8 page.caculateLast(total); 9 10 // 放入轉發參數 11 mav.addObject("cs", cs); 12 // 放入jsp路徑 13 mav.setViewName("listCategory"); 14 return mav; 15 }