pom.xml::
<!-- mybatis-pageHelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.0</version> </dependency>
mybatis-config.xml:
<!-- 分頁插件 --> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="reasonable" value="true"/> </plugin> </plugins>cintroller:
@RequestMapping("/emps") public String getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){ //分頁查詢 int pageSize=5; //傳入頁碼,以及每頁的大小 PageHelper.startPage(pn, pageSize); List<Employee> emps=employeeService.getAll(); //PageInfo封裝查詢後結果,傳入連續顯示的頁數 PageInfo page=new PageInfo(emps,5); model.addAttribute("pageInfo",page); return "list"; }
testMVC:
/* * spring4的測試需要servlet-api 3.0以上版本 */ @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations={"classpath:applicationContext.xml", "file:D:/cmbc/codeAtNight/crud/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml"}) public class MVCTest { //傳入springMVC的ioc @Autowired WebApplicationContext context; MockMvc mockMvc; //虛擬springmvc請求,獲取到處理結果 @Before public void initMockMvc(){ mockMvc=MockMvcBuilders.webAppContextSetup(context).build(); } @Test public void testPage() throws Exception{ //模擬請求拿到返回值 MvcResult result=mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "1")).andReturn(); //請求成功以後,請求域中會有PageInfo,我們可以取出PageInfo進行驗證 MockHttpServletRequest request=result.getRequest(); PageInfo pi=(PageInfo) request.getAttribute("pageInfo"); //當前頁碼 System.out.println(pi.getPageNum()); //總頁碼 System.out.println(pi.getPages()); //每頁的個數 System.out.println(pi.getSize()); //總數量 System.out.println(pi.getTotal()); //連續顯示的頁碼 int[] nums=pi.getNavigatepageNums(); for(int i:nums){System.out.print(i+",");} List<Employee> list=pi.getList(); for(Employee employee:list){ System.out.println("ID:"+employee.getEmpId()+"==>Name:"+employee.getEmpName()); } } }
jsp:
<c:forEach items="${pageInfo.list}" var="item">
<tr>
<th>${item.id}</th>
<th>${item.title}</th>
<th>${item.h5url}</th>
<th>${item.need_login}</th>
<th>${item.code}</th>
<th>${item.start_time}</th>
<th>${item.end_time}</th>
<th>
<button class="btn btn-primary btn-sm" onclick="updateHints('${item.id}','${item.title}','${item.h5url}','${item.need_login}','${item.code}','${item.start_time}','${item.end_time}')">
<span class="glyphicon glyphicon-pencil" aria-hidden="true" ></span>
編輯
</button>
<button class="btn btn-danger btn-sm" onclick="deleteHints('${item.id}')">
<span class="glyphicon glyphicon-trash" aria-hidden="true" ></span>
刪除
</button>
</th>
</tr>
</c:forEach>
<%--顯示分頁信息--%>
<div class="row">
<%--分頁文字信息--%>
<div class="col-md-6">
當前${pageInfo.pageNum}頁,總${pageInfo.pages}頁,總${pageInfo.total}條記錄
</div>
<%--分頁條信息--%>
<div class="col-md-6">
<nav aria-label="Page navigation">
<ul class="pagination">
<li><a href="${APP_PATH}fenyeHints?pn=1">首頁</a></li>
<c:if test="${pageInfo.hasPreviousPage}">
<li>
<a href="${APP_PATH}/fenyeHints?pn=${pageInfo.pageNum-1}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<c:forEach items="${pageInfo.navigatepageNums}" var="page_Num">
<c:if test="${page_Num==pageInfo.pageNum}">
<li class="active"><a href="#">${page_Num}</a></li>
</c:if>
<c:if test="${page_Num!=pageInfo.pageNum}">
<li ><a href="${APP_PATH}/fenyeHints?pn=${page_Num}">${page_Num}</a></li>
</c:if>
</c:forEach>
<%-- <li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>--%>
<c:if test="${pageInfo.hasNextPage}">
<li>
<a href="${APP_PATH}/fenyeHints?pn=${pageInfo.pageNum+1}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
<li><a href="${APP_PATH}/fenyeHints?pn=${pageInfo.pages}">末頁</a></li>
</ul>
</nav>
</div>
</div>