一 .使用原生分页器的实体类
1.1 java代码部分
方法多 不易书写
package cn.bdqn.entity;
public class Page {
private Integer pageIndex;//页码
private Integer pageSize;//页大小 显示多少行数据
private Integer totalCounts;//数据的总行数
private Integer totalPages;//总页数
private Integer startRows;//起始行
private String sname="";
private String grade="";
private String sex="";
public Page() {
}
public Page(Integer pageIndex) {
this(pageIndex, 7);
}
public Page(Integer pageIndex, Integer pageSize, Integer totalCounts, Integer totalPages, Integer startRows, String snane, String sex, String grade) {
this.pageIndex = pageIndex;
this.pageSize = pageSize;
this.totalCounts = totalCounts;
this.totalPages = totalPages;
this.startRows = startRows;
this.sname = sname;
this.sex = sex;
this.grade = grade;
}
public Page(Integer pageIndex, Integer pageSize) {
this.pageIndex = pageIndex;
this.pageSize = pageSize;
this.setStartRows((pageIndex - 1) * pageSize);
}
public Integer getPageIndex() {
return pageIndex;
}
public void setPageIndex(Integer pageIndex) {
this.pageIndex = pageIndex;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalCounts(int count) {
return totalCounts;
}
public void setTotalCounts(Integer totalCounts) {
this.totalCounts = totalCounts;
this.setTotalPages(totalCounts % pageSize == 0 ? totalCounts / pageSize : totalCounts / pageSize + 1);
}
public Integer getTotalPages() {
return totalPages;
}
public void setTotalPages(Integer totalPages) {
this.totalPages = totalPages;
}
public Integer getStartRows() {
return startRows;
}
public void setStartRows(Integer startRows) {
this.startRows = startRows;
}
// public int getPageNum() {
// return pageNum;
// }
//
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getSex() {
return sex;
}
public void setSex(String grade) {
this.sex = sex;
}
// @Override
// public String toString() {
// return "Page{" +
// "pageIndex=" + pageIndex +
// ", pageSize=" + pageSize +
// ", totalCounts=" + totalCounts +
// ", totalPages=" + totalPages +
// ", startRows=" + startRows +
// '}';
// }
@Override
public String toString() {
return "Page{" +
"pageIndex=" + pageIndex +
", pageSize=" + pageSize +
", totalCounts=" + totalCounts +
", totalPages=" + totalPages +
", startRows=" + startRows +
", sname='" + sname + '\'' +
", sex='" + sex + '\'' +
", grade='" + grade + '\'' +
'}';
}
}
1.2 在动态jsp页面中 用forEach 书写
<div class="a-run">
<a href="<c:url context='${pageContext.request.contextPath}' value='/ServletScoreShow?pageIndex=1'/>">首页</a>
<c:if test="${page.pageIndex > 1}">
<a href="<c:url context='${pageContext.request.contextPath}' value='/ServletScoreShow?pageIndex=${page.pageIndex - 1}' />">上一页</a>
</c:if>
<c:if test="${page.pageIndex == 1}">
<a>上一页</a>
</c:if>
<c:if test="${page.pageIndex < page.totalPages}">
<a href="<c:url context='${pageContext.request.contextPath}' value='/ServletScoreShow?pageIndex=${page.pageIndex + 1}'/>">下一页</a>
</c:if>
<c:if test="${page.pageIndex == page.totalPages}">
<a>下一页</a>
</c:if>
<a href="<c:url context='${pageContext.request.contextPath}' value='/ServletScoreShow?pageIndex=${page.totalPages}'/>">尾页</a>
</div>
二 . 使用mybatis 分页
2.1.1 在pom.xml文件中引入依赖
<!--引入分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.3.2</version> </dependency> </dependencies>
2.1.2 在mybatis.xml中 设定标签(注意标签书写顺序)
<!--配置mybatis分页插件--> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins>
三 . 测试类代码
package cn.sy;
import cn.sy.entity.User;
import cn.sy.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class TestPageHelper {
SqlSessionFactory factory = null;
@Before
public void init() {
try {
InputStream resourceAsStream = Resources.getResourceAsStream("config/mybatis-config.xml");
factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void test(){
SqlSession sqlSession = factory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//pageNum : 分的页数 pageSize: 每页显示的记录数
PageHelper.startPage(3,3);
List<User> users = mapper.selectList();
for (User user : users) {
System.out.println("user = " + user);
}
// 创建分页信息对象 获取更多的分页信息
PageInfo<User> userPageInfo = new PageInfo<>(users);
// 获取分页的详细信息
List<User> list = userPageInfo.getList();
System.out.println("list = " + list);
long total = userPageInfo.getTotal();
System.out.println("total = " + total);
System.out.println("*********************");
System.out.println("userPageInfo = " + userPageInfo);
}
}