select * from user limit 0, 10
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.csr.web.domain.User">
<resultMap type="com.csr.web.domain.User" id="userResult">
<result property="id" column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<result property="username" column="username" />
<result property="password" column="password" />
<result property="email" column="email" />
</resultMap>
<select id="findUserPage" resultMap="userResult">
select * from user limit #{_parameter.offset,jdbcType=INTEGER},#{_parameter.pagesize,jdbcType=INTEGER}
</select>
<select id="findUserCount" resultType="int">
select count(*) from user
</select>
</mapper>
@Repository
public class UserDaoImpl implements UserDao {
private final String FIND_USER_COUNT = "findUserCount";
private final String FIND_USER_PAGE = "findUserPage";
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
public List<User> findAllUser(Pager page) {
return sqlSessionTemplate.selectList(FIND_USER_PAGE, page);
}
public int findUserCount() {
return sqlSessionTemplate.selectOne(FIND_USER_COUNT);
}
}
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
public List<User> findAllUser(Pager page) {
return userDao.findAllUser(page);
}
public int findUserCount() {
return userDao.findUserCount();
}
}
@Controller
@Scope("prototype")
public class UserAction extends ActionSupport {
@Autowired
private UserService userService;
private User user;
private List<User> userList;
private PageModel pm ;
private Pager pager;
public Pager getPager() {
return pager;
}
public void setPager(Pager pager) {
this.pager = pager;
}
public PageModel getPm() {
return pm;
}
public void setPm(PageModel pm) {
this.pm = pm;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
public String queryAllUser() {
int pagesize=3;
int offset=0;
HttpServletRequest request = ServletActionContext.getRequest();
if(request.getParameter("pager.offset")!=null){
offset=Integer.parseInt(request.getParameter("pager.offset"));
}
Pager page = new Pager();
page.setOffset(offset);
page.setPagesize(pagesize+offset+1);
// userList = userService.findAllUser();
userList = userService.findAllUser(page);
int count = userService.findUserCount();
pm = new PageModel();
pm.setDatas(userList);
pm.setTotal(count);
return "userList";
}
}
public class Pager implements Serializable {
private static final long serialVersionUID = 1L;
private int pagesize;
private int offset;
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
}
public class PageModel {
private int offset = 0;
private int pagesize = 10;
private int total;
private List datas;
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public List getDatas() {
return datas;
}
public void setDatas(List datas) {
this.datas = datas;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="pg" uri="http://jsptags.com/tags/navigation/pager" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>无标题文档</title>
</head>
<body>
<center>
<h2>用户列表</h2>
<!--
<table width="90%" border="1">
<tr>
<th>用户id</th>
<th>用户名称</th>
<th>用户密码</th>
<th>操作</th>
</tr>
<s:iterator value="userList">
<tr>
<td><s:property value="id"/> </td>
<td><s:property value="username"/> </td>
<td><s:property value="password"/> </td>
<td><s:a action="user_updateUI.action"><s:param name="user.id">${id}</s:param>修改</s:a>
<s:a action="user_delete.action"><s:param name="user.id">${id}</s:param>删除</s:a></td>
</tr>
</s:iterator>
</table>
-->
<table width="778" border="0" cellPadding="0" cellSpacing="1" bgcolor="#6386d6">
<tr bgcolor="#EFF3F7">
<TD align="center">ID</TD>
<TD align="center">内容</TD>
<TD align="center">时间</TD>
<TD align="center">相关操作</TD>
</tr>
<c:if test="${!empty pm.datas}">
<c:forEach items="${pm.datas}" var="user">
<tr bgcolor="#EFF3F7">
<td align="center">${user.id }</td>
<td align="center">${user.username }</td>
<td align="center">${user.password}</td>
<td align="center">
修改
删除</td>
</tr>
</c:forEach>
</c:if>
<c:if test="${empty pm.datas}">
<tr>
<td colspan="5" align="center" bgcolor="#EFF3F7">
没有找到相应的记录
</td>
</tr>
</c:if>
</table>
<pg:pager url="user_queryAllUser.action" items="${pm.total}" export="currentPageNumber=pageNumber" maxPageItems="3">
<pg:first>
<a href="${pageUrl}">首页</a>
</pg:first>
<pg:prev>
<a href="${pageUrl }">上一页</a>
</pg:prev>
<pg:pages>
<c:choose>
<c:when test="${currentPageNumber eq pageNumber}">
<font color="red">${pageNumber }</font>
</c:when>
<c:otherwise>
<a href="${pageUrl }">${pageNumber }</a>
</c:otherwise>
</c:choose>
</pg:pages>
<pg:next>
<a href="${pageUrl }">下一页</a>
</pg:next>
<pg:last>
<a href="${pageUrl }">尾页</a>
</pg:last>
</pg:pager>
</center>
</body>
</html>
pg:pager【这个标签用来设置分页的总体参数】重要参数说明:
url:分页的链接根地址,pager标签会在这个链接的基础上附加分页参数
items:总记录数,pager标签正是根据这个值来计算分页参数的
maxPageItems:每页显示的行数,默认为10
maxIndexPages:在循环输出页码的时候,最大输出多少个页码,默认是10
pg:first【第一页的标签】重要参数说明:
export变量的意义:
pageUrl - 分页链接URL地址(最重要的export参数)
pageNumber - 页码
firstItem - 首页第一行的索引值
lastItem - 首页最后一行的索引值
pg:pre【上一页标签】重要参数说明:
export变量的意义:
pageUrl - 分页链接URL地址(最重要的export参数)
pageNumber - 页码
firstItem - 前页第一行的索引值
lastItem - 前页最后一行的索引值
pg:next【下一页标签】重要参数说明:
export变量的意义:
pageUrl - 分页链接URL地址(最重要的export参数)
pageNumber - 页码
firstItem - 下页第一行的索引值
lastItem - 下页最后一行的索引值
pg:last【最后一页的标签】重要参数说明:
export变量的意义:
pageUrl - 分页链接URL地址(最重要的export参数)
pageNumber - 页码
firstItem - 尾页第一行的索引值
lastItem - 尾页最后一行的索引值
pg:pages【这个标签用来循环输出页码信息】重要参数说明:
export变量的意义:
pageUrl - 分页链接URL地址(最重要的export参数)
pageNumber - 页码
firstItem - pageNumber这个页码指定的那一页的第一行的索引值
lastItem - pageNumber这个页码指定的那一页的最后一行的索引值
pager-taglib插件进行普通分页,布布扣,bubuko.com
原文:http://www.cnblogs.com/wang3680/p/3b5b598a09ef2ec4315da829c87c7976.html