据本人了解,目前较常用的分页实现办法有两种:
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>
四、实现效果: