一、分页效果:
(1)查询所有的数据
(2) 单个条件查询
(3) 2个条件查询
(4) 3个条件查询
(5) 3个条件查询
二、具体步骤:
1)在 pom.xml文件中添加依赖包
<!--mybatis处理分页的包-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>2.0</version>
</dependency>
2)spring-mybatis.xml添加mybatis分页插件
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties" value=""/>
</bean>
</array>
</property>
3)根据条件查询数据
<select id="findName" resultType="Userinfo">
-- 引用这个sql块 refid定义的名称id
<include refid="table"/>
<where>
<if test="loginName != null">
loginName like concat('%',#{loginName},'%')
</if>
<if test="phone != null">
and phone like concat('%',#{phone},'%')
</if>
<if test="admin != null">
and admin=#{admin}
</if>
</where>
</select>
4)封装参数类Parames
use resturant;
select * from userinfo;
-- 第一页 (1-1)*6=0
select * from userinfo limit 0,6;
-- 第二页 (2-1)*6=6
select * from userinfo limit 6,6;
5)介绍分页类的说明:
public class PageInfo<T> extends PageSerializable<T> {
private int pageNum; //当前页
private int pageSize; //每页的个数
private int pages; //总页数
private int prePage; //上一页
private int nextPage; //下一页
private boolean isFirstPage; //是否是第一页
private boolean isLastPage; //是否是最后一页
private boolean hasPreviousPage;//是否有上一页
private boolean hasNextPage; //是否有下一页
................................
protected long total; //总个数
protected List<T> list; //每一页的数据
。。。
}
6)业务分页接口
public PageInfo<Userinfo> getPageInfo(Parames parames);
7)实现这个业务分页接口
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public PageInfo<Userinfo> findName(Userinfo userinfo, Parames parames) {
//1>开始进行分页
PageHelper.startPage(parames.getPageCurrent(),parames.getPageSize());
//2>调用底层方法
List<Userinfo> list = userinfoDao.findName(userinfo);
//3>分页包装
PageInfo<Userinfo> pageInfo =new PageInfo<>(list);
return pageInfo;
}
8)控制层调用
@RequestMapping("/list")
public String list(ModelMap map,Parames parame,Userinfo userinfo){
//业务方法
PageInfo<Userinfo> pageInfo= userinfoService.findName(userinfo,parame);
//保存数据
map.addAttribute("pageInfo",pageInfo);
//用户数据
map.addAttribute("userinfo",userinfo);
//转发
return "list"; //list.jsp
}
三、页面显示
<%--
Created by IntelliJ IDEA.
User: 44401
Date: 2020/4/15
Time: 11:16
To change this template use File | Settings | File Templates.
<%--
Created by IntelliJ IDEA.
User: 44401
Date: 2020/4/11
Time: 16:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
<link href="css/find.css" rel="stylesheet"/>
<script type="text/javascript">
function funDel(id){
//判断
if(window.confirm("您确定要删除吗?")){
//跳转处理数据
window.location="del?userId="+id;
}
}
</script>
</head>
<body>
<div class="dv">
<a href="save.jsp">添加</a><p/>
<p class="page">
<form action ="list" method="post" >
用户名:<input type="text" name="loginName" value="${userinfo.loginName}"/>
电话:<input type="text" name="phone" value="${userinfo.phone}"/>
权限:<select name="admin" onchange="" id="admin">
<%--下拉列表选中的数据不能没有--%>
<option value="0" <c:if test="${userinfo.admin==0}">selected</c:if> >0</option>
<option value="1" <c:if test="${userinfo.admin==1}">selected</c:if> >1</option>
</select>
<input type="submit" value="查询">
</form>
</p>
<table border="1">
<caption><h2>用户基本信息</h2></caption>
<tr>
<td>编号</td>
<td>用户名</td>
<td>密码</td>
<td>电话</td>
<td>姓名</td>
<td>余额</td>
<td>权限</td>
<td>操作</td>
</tr>
<c:if test="${pageInfo.list.size()==0}">
<tr><td colspan="8"> 没有您要查找的数据</td></tr>
</c:if>
<c:if test="${pageInfo.list.size()!=0}">
<c:forEach var="user" items="${pageInfo.list}">
<tr>
<td>${user.userId}</td>
<td>${user.loginName}</td>
<td>${user.loginPass}</td>
<td>${user.phone}</td>
<td>${user.realname}</td>
<td>${user.amount}</td>
<td>${user.admin}</td>
<td>
<a href="${pageContext.request.contextPath}/find?userId=${user.userId}">详情</a>
<a href="${pageContext.request.contextPath}/edit?userId=${user.userId}">修改</a>
<a href="javascript:funDel(${user.userId})">删除</a></td>
</tr>
</c:forEach>
</table>
<p class="page">
第${pageInfo.pageNum}/ ${pageInfo.pages}页 共${pageInfo.total} 条
<a href="list?pageCurrent=1&loginName=${userinfo.loginName}&phone=${userinfo.phone}&admin=${userinfo.admin}">首页</a>
<c:if test="${!pageInfo.isFirstPage}">
<a href="list?pageCurrent=${pageInfo.prePage}&loginName=${userinfo.loginName}&phone=${userinfo.phone}&admin=${userinfo.admin}">上一页</a>
</c:if>
<a href="list?pageCurrent=${pageInfo.nextPage}&loginName=${userinfo.loginName}&phone=${userinfo.phone}&admin=${userinfo.admin}">下一页</a>
<a href="list?pageCurrent=${pageInfo.pages}&loginName=${userinfo.loginName}&phone=${userinfo.phone}&admin=${userinfo.admin}">尾页</a>
<c:forEach var="i" begin="1" end="${pageInfo.pages}">
<a href="list?pageCurrent=${i}">${i}</a>
</c:forEach>
<p>
</c:if>
</div>
</body>
</html>
源码:链接:https://pan.baidu.com/s/1xoDW-6Bi_sb8uTpVQY5_lg
提取码:b66n