1.项目配置文件pom.xml中进行引入:

<dependency>
        <groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper</artifactId>
	<version>4.1.1</version>
</dependency>

2.mybatis-config.xml中配置:

<plugins>
    <plugin interceptor="com.github.pagehelper.PageHelper">
        <!--数据库方言-->
        <property name="dialect" value="mysql" />
        <property name="offsetAsPageNum" value="true" />
        <!--设置为true时,使用RowBounds分页会进行count查询 会去查询出总数-->
        <property name="rowBoundsWithCount" value="true" />
        <property name="pageSizeZero" value="true" />
        <property name="reasonable" value="true" />
    </plugin>
</plugins>

3.service层:

public PageInfo<Product> getAllSold(Integer pageNum,
                                    Integer pageSize) {
    PageHelper.startPage(pageNum,pageSize);//在查询语句前加入该句,查询会自动物理分页
    List soldList = productMapper.selectAllSold(Dict.productSold);
    PageInfo<Product> pageInfo = new PageInfo<Product>(soldList);
    return pageInfo;
}

4.controller:

@RequestMapping(value = "/")
    public String getIndexPage(Model model,
                               @RequestParam(value = "pageNum", required = false) Integer pageNum,
                               @RequestParam(value = "pageSize", required = false) Integer pageSize,
                               @RequestParam(value = "userId", required = false) Integer userId){
        if (pageNum == null || pageNum.equals("")) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize.equals("")) {
            pageSize = Dict.pageSize;//每页显示的数目
        }

        //利用url传递参数,进行分页
        if (httpServletRequest.getQueryString() == null) {
            model.addAttribute("urlForFenYe", httpServletRequest.getRequestURI() + "?");
        } else {
            String url = httpServletRequest.getRequestURI() + "?" + httpServletRequest.getQueryString();
            String[] urls = url.split("pageNum");
            model.addAttribute("urlForFenYe", urls[0]);
        }
        PageInfo pageInfo = productServiceImp.getAllProductInformByUserId(pageNum,pageSize,userId);

        model.addAttribute("infoCount", pageInfo.getTotal());
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("pageSize",Dict.pageSize);
        model.addAttribute("ProductSold",Dict.productSold);
        model.addAttribute("allProductList",pageInfo);

        return "index";
}

5.前端

<div id="page"></div>

<script src="/js/laydate/laydate.js"></script>
<script type="text/javascript" src="/js/layer/layer.js"></script>
<script type="text/javascript" src="/js/layui/layui.js"></script>
<script type="text/javascript">
    layui.use('laypage', function(){
        var laypage = layui.laypage;
        var url = "${urlForFenYe!}";
        //执行一个laypage实例
        laypage.render({
            elem: 'page' //注意,这里的 page 是 ID,不用加 # 号
            ,count: ${infoCount!} //数据总数,从服务端得到
            ,curr:${pageNum!}
            ,limit:${pageSize!}
            ,jump: function(obj, first) {
                //obj包含了当前分页的所有参数,比如:
                console.log(obj.curr); //得到当前页,以便向服务端请求对应页的数据。
                console.log(obj.limit); //得到每页显示的条数
                //首次不执行
                if (!first) {
                    var page = obj.curr;
                    var eveyLimit = obj.limit;
                    //alert(url);
                    url=url+"pageNum="+page+"&pageSize="+eveyLimit;
                    console.log(url); //得到每页显示的条数
                    window.location.href=url;
                }
            }
        });
    });
</script>
01-14 02:08