mybatis分页(PageHelper)

来源:互联网 发布:linux zip打包 TXT文件 编辑:程序博客网 时间:2024/05/22 00:47

需求:实现分页(xml+接口)
page页面(公共页面,在别的jsp使用include引用)

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>        <c:if test="${!pageinfo.isFirstPage}">            <input type="button" value="上一页" name="${pageinfo.prePage}"/>             </c:if>            <c:if test="${!pageinfo.isLastPage}">            <input type="button" value="下一页" name="${pageinfo.nextPage}"/>             </c:if>            <input type="button" value="首页" name="${pageinfo.firstPage}"/>             <input type="button" value="尾页" name="${pageinfo.lastPage}"/>    <br />    <!-- 页码 -->                <c:forEach begin="1" end="${pageinfo.pages}" varStatus="st">                  <input type="button" value="第${st.count}页" name="${st.count}"/>                </c:forEach>    <br></body></html>

useradmin.jsp(显示全部数据的页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Insert title here</title><script type="text/javascript" src="js/jquery-1.8.3.min.js"></script><script type="text/javascript">    $(function() {        $("#id1").change(                function() {                    // 当前页数           pageNum="+$("#id1").find("option:selected").text()                     // 每页显示的数据条数    pageSize="+$("#id1").val()                    window.location.href = "userlist?pageNum="                            + $("#id1").find("option:selected").text()                            + "&pageSize=" + $("#id1").val();                })        $("#id3").click(function() {            window.location.href = "userlist?pageNum=" + $("#id2").val();        })        $("input:button").click(function(){            window.location.href = "userlist?pageNum=" + $(this).attr("name");    })     })</script><link type="text/css" rel="stylesheet" href="css/style.css"><script type="text/javascript">    function doit(flag, id) {        if (flag == "del") {            if (confirm("确认删除吗?") != true)                return;        }    }</script></head><body>    <div class="menu">        <table>            <tbody>                <tr>                    <td><form method="post" action="user.do">                            <input name="flag" value="search" class="input-text"                                type="hidden"> 用户名称:<input name="userName"                                class="input-text" type="text">&nbsp;&nbsp;&nbsp;&nbsp;                            <input value="查 询" type="submit">                        </form></td>                </tr>            </tbody>        </table>    </div>    <div class="main">        <div class="optitle clearfix">            <em><input value="添加数据" class="input-button"                onclick="window.location='userAdd.html'" type="button"></em>            <div class="title">用户管理&gt;&gt;</div>        </div>        <div class="content">            <table class="list">                <tbody>                    <tr>                        <td width="70" height="29"><div class="STYLE1" align="center">编号</div></td>                        <td width="80"><div class="STYLE1" align="center">用户名称</div></td>                        <td width="100"><div class="STYLE1" align="center">性别</div></td>                        <td width="100"><div class="STYLE1" align="center">年龄</div></td>                        <td width="150"><div class="STYLE1" align="center">电话</div></td>                        <td width="150"><div class="STYLE1" align="center">地址</div></td>                        <td width="150"><div class="STYLE1" align="center">权限</div></td>                    </tr>                    <c:forEach items="${ users}" var="user">                        <tr>                            <td height="23"><span class="STYLE1"> <c:out                                        value="${user.uid}"></c:out>                            </span></td>                            <td><span class="STYLE1"><a                                    href="userdetail.jsp?uid=${user.uid}&name=${user.name}&password=${user.password}&usex=${user.usex}&uage=${user.uage}&uaddress=${user.uaddress}&utel=${user.utel}&uissupper=${user.uissupper}">                                        <c:out value="${user.name}"></c:out>                                </a></span></td>                            <td><span class="STYLE1"> <c:out                                        value="${user.usex==1?'男':'女'}"></c:out>                            </span></td>                            <td><span class="STYLE1"> <c:out value="${user.uage}"></c:out>                            </span></td>                            <td><span class="STYLE1"> <c:out value="${user.utel}"></c:out>                            </span></td>                            <td><span class="STYLE1"> <c:out                                        value="${user.uaddress}"></c:out>                            </span></td>                            <td><span class="STYLE1"> <c:out                                        value="${user.uissupper==1?'经理':'员工'}"></c:out>                            </span></td>                        </tr>                    </c:forEach>                </tbody>            </table>        </div>    </div>    <select id="id1">        <c:forEach begin="1" end="${pageinfo.pages}" varStatus="st">            <option value="${pageinfo.pageSize}">                  ${st.count}               </option>        </c:forEach>    </select></body>    <%@ include file="page.jsp"%></html>

注意在最后一行的引用(include标签)

contoller类中:

@RequestMapping("/userlist")    public String selectAllUsers1(Map<String, Object> map,            @RequestParam(name = "pageNum", required = true, defaultValue = "0") Integer pageNum) {        System.out.println("获取用户列表");        // 访问第一页,pageNum,pageSize的值为0,查询第一页数据,还需要pageinfo信息一并传递到前端页面        // 第二页。。。        List<User> users = null;        PageInfo<User> pageinfo = null;        if (pageNum == 0 || size == 0) {            PageHelper.startPage(1, size);            users = userService.selectAll();        } else {            PageHelper.startPage(pageNum, size);            users = userService.selectAll();        }        System.out.println(users);        pageinfo = new PageInfo<User>(users);        map.put("users", users);        map.put("pageinfo", pageinfo);        System.out.println("获取成功");        return "userAdmin";    }

解析:PageHelper.startPage(pageNum, size); 第一个参数是指查询第几页,size是一页显示多少条数据.(这里不需要使用(页数-1)*每页条数来计算第几条开始查询)

注意:: users = userService.selectAll();这里最底层的dao层的对应的mapper.xml的select是查询全部数据,也就是select * from 表名

同时,如果别的模块的数据也想使用分页,直接饮用page.jsp即可,但是在对应的controller类的方法中,是map.put(“pageinfo”, pageinfo);第一个参数是pageinfo,因为page.jsp的pageinfo这个名字已经固定.

原创粉丝点击