Spring MVC中目前較常用的分頁實現辦法有兩種:
1.每次翻頁都修改SQL,向SQL傳入相關參數去數據庫實時查出該頁的數據並顯示。
2.查出數據庫某張表的全部數據,再通過在業務邏輯裡面進行處理去取得某些數據並顯示。
對於數據量並不大的簡單的管理系統而言,第一種實現方法相對來說容易使用較少的代碼實現分頁這一功能,本文也正是為大家介紹這種方法:
一、MyBatis數據表配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="ec.help.dao.UserDao"> <resultMap type="ec.help.bean.User" id="userResult" > <id column="id" javaType="string" /> <result column="username" javaType="string" /> <result column="password" javaType="string" /> </resultMap> <sql id="userColumn"> id, username, password</sql> <select id="getUser" parameterType="map" resultType="ec.help.bean.User" > select * from User where username=#{0} and password=#{1} </select> <select id="getAllUser" parameterType="map" resultType="ec.help.bean.User" > select * from User </select> <!-- 分頁使用SQL --> <select id="getUserByPage" resultType="ec.help.bean.User" > select * from user limit #{0},#{1} </select> <insert id="addUser" parameterType="ec.help.bean.User"> insert into User(id,username,password) values(#{id},#{username},#{password}) </insert> <delete id="deleteUser" parameterType="String"> delete from User where id=#{id} </delete> <select id="showUser" parameterType="String" resultType="ec.help.bean.User" > select * from User where id=#{id} </select> <update id="updateUser" parameterType="map"> update User set username=#{0},password=#{1} where id=#{2} </update> </mapper>
SQL中傳入的第一個參數為開始的行數,第二個參數為數據條數。
二、Controller中邏輯實現:
@Value("#{configProperties['userPageSize']}") private String userPageSize; @RequestMapping("/listUser.do") public ModelAndView listUser(String page,Model model){ //每頁顯示的條數 int pageSize = Integer.parseInt(userPageSize); List<User> users = new ArrayList<User>(); users = this.userService.getAllUser(); //查到的總用戶數 model.addAttribute("userNum", users.size()); //總頁數 int pageTimes; if(users.size()%pageSize == 0) { pageTimes = users.size()/pageSize; }else { pageTimes = users.size()/pageSize + 1; } model.addAttribute("pageTimes", pageTimes); //頁面初始的時候page沒有值 if(null == page) { page = "1"; } //每頁開始的第幾條記錄 int startRow = (Integer.parseInt(page)-1) * pageSize; users = this.userService.getUserByPage(startRow, pageSize); model.addAttribute("currentPage", Integer.parseInt(page)); model.addAttribute("users", users); return new ModelAndView("user/listUser"); }
三、分頁頁面文件:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <div class="pagging"> <div class="left">共${userNum}條記錄</div> <div class="right"> <c:if test="${currentPage == 1}"> <span class="disabled"><< 前一頁</span> </c:if> <c:if test="${currentPage != 1}"> <a href="listUser.do?page=${currentPage-1}"><< 前一頁</a> </c:if> <c:if test="${currentPage == 1}"> <span class="current">1</span> </c:if> <c:if test="${currentPage != 1}"> <a href="listUser.do?page=1">1</a> </c:if> <% int pageTimes = (Integer)session.getAttribute("pageTimes"); for(int i=1;i<pageTimes;i++) { request.setAttribute("page", i+1); %> <c:if test="${currentPage == page}"> <span class="current"><%=i+1%></span> </c:if> <c:if test="${currentPage != page}"> <a href="listUser.do?page=<%=i+1%>"><%=i+1%></a> </c:if> <%} %> <c:if test="${currentPage == pageTimes}"> <span class="disabled">後一頁 >></span> </c:if> <c:if test="${currentPage != pageTimes}"> <a href="listUser.do?page=${currentPage+1}">後一頁 >></a> </c:if> </div> </div>
四、實現效果: