在SSM框架搭建之maven方式(二)基礎上進一步作如下修改html
pom.xml添加以下代碼java
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency>
spring-mybatis.xml的id標籤爲sqlSessionFactory節點中添加以下內容mysql
<property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> <!-- 你使用的數據庫類型 --> helperDialect=mysql reasonable=true autoRuntimeDialect=true </value> </property> </bean> </array> </property>
將UserController.java中的內容改寫爲以下代碼git
package com.lymn.it.controller; import java.util.List; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.lymn.it.model.User; import com.lymn.it.service.UserService; @Controller public class UserController { @Autowired UserService userService; Logger logger=Logger.getLogger(UserController.class); @RequestMapping(value="/user") public String user(@RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo,Model model) { logger.info("查詢全部用戶數據"); PageHelper.startPage(pageNo, 5); List<User> userList = userService.getAllUsers(); PageInfo<User> pageInfo=new PageInfo<User>(userList); model.addAttribute("userList", userList); model.addAttribute("pageInfo", pageInfo); logger.info("查詢完畢,返回頁面"); return "user"; } }
將user.jsp中的內容改寫爲以下代碼github
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>User</title> </head> <body> <center> <table width="200" border="1"> <tr> <th scope="col">userid</th> <th scope="col">username</th> <th scope="col">password</th> <th scope="col">email</th> </tr> <c:forEach items="${userList}" var="user"> <tr> <td>${user.userid}</td> <td>${user.username}</td> <td>${user.password}</td> <td>${user.email}</td> </tr> </c:forEach> </table> <p>當前 ${pageInfo.pageNum }頁,總${pageInfo.pages } 頁,總 ${pageInfo.total } 條記錄</div></p> <a href="user?pageNo=${pageInfo.firstPage}">第一頁</a> <c:if test="${pageInfo.hasPreviousPage }"> <a href="user?pageNo=${pageInfo.pageNum-1}">上一頁</a> </c:if> <c:if test="${pageInfo.hasNextPage }"> <a href="user?pageNo=${pageInfo.pageNum+1}">下一頁</a> </c:if> <a href="user?pageNo=${pageInfo.lastPage}">最後頁</a> </center> </body>
訪問以下圖所示表示成功web