ssm学习(五)--加入分页插件

之前我们的查询列表是将所有的数据查询出来,并没有做分页,当数据很少的时候,是不需要分页,但是如果数据很多的时候,所有数据显示在一个页面显然是不合适的。

之前用hibernate的时候,可以直接通过查询来指定页码和条数,在mybatis我没有找到类似功能,所以上网查找别人是如何做的,找到了pagehelper这个插件,这也是在mybatis的项目中主流用的插件。自己试了一下也发现很好用。

废话不多说,直接说怎么用吧。

1.首先在maven的配置文件中加入以下配置。

    <!-- 分页插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.0.3-beta</version>
        </dependency>

2.接着在spring配置文件中加入如下配置。

    <!-- 配置mybatis -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!-- mapper扫描 -->
        <property name="mapperLocations" value="classpath:com/m_gecko/entity/*.xml"></property>
        <!-- 分页插件 -->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            helperDialect=mysql
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

注意分页插件那段配置。

3.其实有了上面两个配置就大功告成了,接下来就是用这个插件了。我们来修改geckoList这个方法,将数据分页。如下。

    @RequestMapping("geckoList")
    public ModelAndView geckoList(@RequestParam(required=true,defaultValue="1")Integer page,@RequestParam(required=false,defaultValue="5")Integer pageSize) throws Exception {
        PageHelper.startPage(page, pageSize);
        ModelAndView mv = new ModelAndView();
        List<TGecko> geckoList = geckoService.getGeckoList();
        mv.getModel().put("geckoList", geckoList);
        PageInfo<TGecko>p=new PageInfo<TGecko>(geckoList);
        mv.getModel().put("page", p);
        mv.setViewName("gecko/GeckoList");
        return mv;
    }

注意这个方法中传入了两个参数,page和pagesize这两个参数,然后我们就可以把list放入pageInfo中重新包装,得到当前页面的list(而不是全部),然后把page对象传递到页面去,在页面上我们可以用${page.xxx}来使用page里面的东西。

修改页面,如下。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>geckoList</title>
<!-- css文件 -->
<link rel="stylesheet" href="./static/bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet"
    href="./static/bootstrap/css/bootstrap-theme.min.css">
<!-- js文件 -->
<script type="text/javascript" src="./static/js/jquery-1.11.0.js"></script>
<script type="text/javascript">
    function MemAdd() {
        h = "geckoAdd";
        location.href = h;
    }
    function deleteGecko(geckoId){
        if (confirm('确定删除?')){
            $.ajax({
                type : 'post',
                url : 'geckoDelete?geckoId='+geckoId,
                async : false,
                dataType : 'html',
                success : function(data) {
                    if (data > 0) {
                        alert("成功");
                    } else {
                        alert("失败")
                    }
                    location.href = "geckoList";
                }
            })
        }
    }
</script>
</head>
<body>
    <form>
        <div class="row" style="text-align: center">
            <div class="col-lg-5" style="font-size: 18px">
                <strong>hello,welcome to gecko's world</strong>
            </div>
            <div class="col-lg-2 col-xs-offset-1">
                <button type="button" class="btn  btn-sm" onclick="MemAdd()">新增</button>
            </div>

        </div>
        <div class="row">
            <div class="col-lg-8 col-xs-offset-1">
                <table class="table">
                    <tr>
                        <th>编号</th>
                        <th>名称</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                    <c:choose>
                        <c:when test="${not empty geckoList }">
                            <c:forEach items="${geckoList }" var="gecko" varStatus="vs">
                                <tr>
                                    <td>${gecko.geckoId}</td>
                                    <td>${gecko.geckoName}</td>
                                    <td><fmt:formatDate type="both" dateStyle="medium"
                                            timeStyle="medium" value="${gecko.createTime}" /></td>
                                    <td><a href="geckoEdit?geckoId=${gecko.geckoId}">编辑</a>&nbsp;&nbsp;<a
                                        href='javascript:void(0)'
                                        onclick="deleteGecko(${gecko.geckoId})">删除</a></td>
                                </tr>
                            </c:forEach>
                        </c:when>
                        <c:otherwise>
            没有相关数据
            </c:otherwise>
                    </c:choose>
                </table>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-8 col-xs-offset-7">
                <c:forEach var="i" begin="1" end="${page.pages }" step="1">
                    <a href="geckoList?page=${i}">${i}</a>
                </c:forEach>
                <a href="geckoList?page=${page.prePage}">上一页</a> <a
                    href="geckoList?page=${page.nextPage}">下一页</a>
            </div>
        </div>
    </form>
</body>
</html>

主要加入了相关的分页代码。

访问geckoList,如下。

点击第二页,发现浏览器地址栏如下。

点击上一页,下一页均可以实现页面跳转。至于更优美的分页插件(指的是页面端),以后有机会会写。

 

原文地址:https://www.cnblogs.com/roy-blog/p/7019104.html