目录
普通分页
在目前的很多项目中,都会涉及到查询结果的分页操作,分页的出现大大减轻了用户在使用时对数据的阅读量,最为重要的一点在于分页可以大大减轻服务器的查询操作,不用一次查询所有的记录,提高查询效率。
而在上一个我学习开发的项目之中就出现了分页,当时我的写法非常的麻烦,代码如下(这是一个web的小项目,而且当时还没学怎么将java代码从jsp中消除):
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*,java.sql.*,test22.Student,test22.Student1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int totalRecord=0;
int pageSize = 6;
int currentPage = -1;
String curPage = request.getParameter("currentPage");
if(curPage==null)
currentPage=1;
else
currentPage=Integer.parseInt(curPage);
int startRec = (currentPage-1)*pageSize;
Connection conn = null;
ResultSet rs = null;
try{
Class.forName("oracle.jdbc.OracleDriver");
} catch(ClassNotFoundException e){
out.print("加载驱动错误");
}
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","123");
String sql = "select count(*) from student";
PreparedStatement ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()){
totalRecord = rs.getInt(1);
}
int pageCount = (totalRecord%pageSize==0)?(totalRecord/pageSize):(totalRecord/pageSize+1);
//调整待显示的页码
if(currentPage > pageCount) currentPage = pageCount;
ps = conn.prepareStatement("select * from (select s.*,rownum rn from (select * from student order by sno) s where rownum<=?*?) where rn>(?-1)*? ");
ps.setInt(1, currentPage);
ps.setInt(2,pageSize);
ps.setInt(3, currentPage);
ps.setInt(4,pageSize);
rs = ps.executeQuery();
List<Student1> stuList = new ArrayList<Student1>();
while(rs.next()){
Student1 stu = new Student1();
stu.setSno(rs.getInt(1));
stu.setSname(rs.getString(2));
stu.setSsex(rs.getString(3));
stu.setSbirth(rs.getDate(4).toString());
stu.setShobby(rs.getString(5));
stu.setSgid(rs.getInt(6));
stuList.add(stu);
}
%>
<table>
<tr>
<th>学生id</th>
<th>学生姓名</th>
<th>学生性别</th>
<th>学生生日</th>
<th>兴趣爱好</th>
<th>分组id</th>
</tr>
<%for(Student1 s : stuList){%>
<tr>
<td><%=s.getSno() %></td>
<td><%=s.getSname() %></td>
<td><%=s.getSsex() %></td>
<td><%=s.getSbirth() %></td>
<td><%=s.getShobby() %></td>
<td><%=s.getSgid() %></td>
</tr>
<%}%>
</table>
<%//循环显示数据%>
共<%=pageCount%>页 每页<%=pageSize%>条记录 页次 <%=currentPage%>/<%=pageCount%>页
<a href="pagination1.jsp?currentPage=1">首页</a>
<%if(currentPage==1){%>
<a href="pagination1.jsp?currentPage=<%=1%>">上一页</a>
<%}else if(currentPage>1){%>
<a href="pagination1.jsp?currentPage=<%=currentPage-1%>">上一页</a>
<%} %>
<a href="pagination1.jsp?currentPage=<%=currentPage+1%>">下一页</a>
<a href="pagination1.jsp?currentPage=<%=pageCount%>">尾页</a>
<%
//关闭结果集
rs.close();
//关闭SQL语句对象
ps.close();
//关闭数据库
conn.close();
%>
</body>
</html>
代码写的现在看来很糙,但是思想就是,在使用Oracle数据库中和在使用MySQL数据库中,用不同的分页sql查询语句来操作,MySQL的语句相对简单,使用select * from table limit (start-1)*limit,limit; 其中start是页码,limit是每页显示的数。而 Oracle就相对麻烦的多,上面的代码就是使用Oracle来进行一个分页操作,需要使用伪列的思想,代码非常的复杂。
PageHelper分页
接下来我们使用分页工具PageHelper来处理分页就将大大减低我们开发的难度
PageHelper分页的具体操作步骤:
1.安装jar包或者在maven中导入依赖,个人推荐导入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper-version}</version>
</dependency>
2.mybatis对PageHelper的配置
<plugins>
<!-- 配置分页插件 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 4.0.0以后版本可以不设置该参数 -->
<!--<property name="dialect" value="mysql"/>-->
<!-- 该参数默认为false -->
<!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
<!-- 和startPage中的pageNum效果一样 -->
<property name="offsetAsPageNum" value="true" />
<!-- 该参数默认为false -->
<!-- 设置为true时,使用RowBounds分页会进行count查询 -->
<property name="rowBoundsWithCount" value="true" />
<!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
<!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型) -->
<property name="pageSizeZero" value="true" />
<!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
<!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
<!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
<property name="reasonable" value="false" />
<!-- 支持通过Mapper接口参数来传递分页参数 -->
<property name="supportMethodsArguments" value="false" />
<!-- always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->
<property name="returnPageInfo" value="none" />
</plugin>
上面列举的是一些特别常用的配置信息的设置
3.代码测试
@Test
public void test_selectAllStudent() throws IOException {
PageHelper.startPage(1, 0,"sid");
//第五步、查询所有学生
List<Student> ls = studentDao.selectAllStudent();
for(Student student : ls) {
System.out.println(student.toString());
}
}
在Junit4中调用PageHelper.startPage(1, 0,"sid");这个静态方法其中第一参数是指分页的当前页码,如果在配置文件中设置启用分页参数合理化时,第一个参数小于1或者大于分组总页数时,会分别显示第一页的数据和最后一页的数据;第二个参数代表的是一页有多少的数据量,如果设置<property name="pageSizeZero" value="true" /> 时,参数为0,会将所有的数据都显示;第三个参数代表数据库的数据字段名称还可以在后面加上desc来表示降序排列。