• Mybatis分页插件-PageHelper的使用


    1.首先在mybatis-config.xml中添加如下配置:

        <plugins>
            <!-- com.github.pagehelper为PageHelper类所在包名 -->
            <plugin interceptor="com.github.pagehelper.PageInterceptor">
                <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
                <property name="helperDialect" value="mysql"/>
            </plugin>
        </plugins>

    配置后为

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <!-- 配置全局属性 -->
        <settings>
            <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
            <setting name="useGeneratedKeys" value="true" />
            <!-- 使用列别名替换列名 默认:true -->
            <setting name="useColumnLabel" value="true" />
            <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
            <setting name="mapUnderscoreToCamelCase" value="true" />
        </settings>
        <plugins>
            <!-- com.github.pagehelper为PageHelper类所在包名 -->
            <plugin interceptor="com.github.pagehelper.PageInterceptor">
                <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
                <property name="helperDialect" value="mysql"/>
            </plugin>
        </plugins>
    </configuration>

    2.

    需要分页,自然就还要一个查询了。用了PageHelper之后,查询语句就可以很简单了。

    <select id="queryAll" resultMap="BaseResultMap"  >
        select *
        from article
        ORDER BY id DESC
      </select>
     

    3.

    接口、实现类我就不再贴代码了,该怎么写还是怎么写,不需要传分页参数。然后是Controller:

     @RequestMapping("/")
        public ModelAndView index(@RequestParam(required=true,defaultValue="1") Integer page, @RequestParam(required=false,defaultValue="5") Integer pageSize){
            ModelAndView modelAndView =new ModelAndView("index");
            PageHelper.startPage(page, pageSize);
            List<Article> articles=articleService.queryAll();
            PageInfo<Article> pageInfo=new PageInfo<Article>(articles);
            modelAndView.addObject("articles",articles);
            modelAndView.addObject("pageInfo",pageInfo);
            return modelAndView;
        }
    PageHelper.startPage(page, pageSize);

    这段代码表示,程序开始分页了,page默认值是1,pageSize默认是10,意思是从第1页开始,每页显示10条记录。

    使用PageInfo这个类,你需要将查询出来的list放进去:

    PageInfo<Article> pageInfo=new PageInfo<Article>(articles);

    然后 

    modelAndView.addObject("pageInfo",pageInfo);

    4.前端分页代码(Bootstrap)

       <ul class="pagination" >
                    <li <c:if test="${pageInfo.pageNum==1}">class="disabled"</c:if>><a href="/?page=1">&laquo;</a></li>
                    <c:forEach begin="1" end="${pageInfo.pages}" step="1" var="pageNo">
                        <li <c:if test="${pageInfo.pageNum==pageNo}">class="active"</c:if>><a href="/?page=${pageNo}">${pageNo}</a></li>
                    </c:forEach>
                    <li <c:if test="${pageInfo.pageNum==pageInfo.pages}">class="disabled"</c:if>><a href="/?page=${pageInfo.pages}">&raquo;</a></li>
                </ul>
    5.PageInfo类
     //当前页
        private int pageNum;
        //每页的数量
        private int pageSize;
        //当前页的数量
        private int size;
        //排序
        private String orderBy;
    
        //由于startRow和endRow不常用,这里说个具体的用法
        //可以在页面中"显示startRow到endRow 共size条数据"
    
        //当前页面第一个元素在数据库中的行号
        private int startRow;
        //当前页面最后一个元素在数据库中的行号
        private int endRow;
        //总记录数
        private long total;
        //总页数
        private int pages;
        //结果集
        private List<T> list;
    
        //第一页
        private int firstPage;
        //前一页
        private int prePage;
        //下一页
        private int nextPage;
        //最后一页
        private int lastPage;
    
        //是否为第一页
        private boolean isFirstPage = false;
        //是否为最后一页
        private boolean isLastPage = false;
        //是否有前一页
        private boolean hasPreviousPage = false;
        //是否有下一页
        private boolean hasNextPage = false;
        //导航页码数
        private int navigatePages;
        //所有导航页号
        private int[] navigatepageNums;
     
  • 相关阅读:
    前端面试题
    Less 中的嵌套规则与 & 符号的使用
    组件的生命周期
    222
    221
    twemproxy代理memcache(3)
    memcache数据迁移与解决方案(2)
    mencache的原理与应用(1)
    yield推荐,理解协程的最佳入口
    上下文管理器的实现
  • 原文地址:https://www.cnblogs.com/youth-dream/p/7630227.html
Copyright © 2020-2023  润新知