• 分页插件PageHelper


    http://blog.csdn.net/eson_15/article/details/52270046

    1.  需要引入PageHelperjar

    如果没有使用maven,那直接把jar包导入到lib文件夹下即可,这个PageHelper插件在github上有开源,地址为:https://github.com/pagehelper/Mybatis-PageHelper/tree/master/src/main/java/com/github/pagehelper。 
    如果使用了maven,那么只要在pom.xml中引入该插件即可,引入如下:

    <dependency>

        <groupId>com.github.pagehelper</groupId>

        <artifactId>pagehelper</artifactId>

        <version>4.1.4</version>

    </dependency>

    2. mybatis的全局配置文件SqlMapConfig.xml中配置该插件

    <configuration>

     

       <settings>

          <setting name="logImpl" value="STDOUT_LOGGING" />

       </settings>

       <plugins>

          <!-- 配置分页助手 -->

          <plugin interceptor="com.github.pagehelper.PageHelper">

            <property name="dialect" value="mysql" />

            <!-- 该参数默认为false -->

            <!-- 设置为true时,使用RowBounds分页会进行count查询 -->

            <property name="rowBoundsWithCount" value="true" />

          </plugin>

         

       </plugins>

    </configuration>

    3定义需要用到的实体,里面包括页数,数据总数等等

    package com.edu.mooc.common.bean;

     

    import java.util.List;

     

    import com.fasterxml.jackson.databind.JsonNode;

    import com.fasterxml.jackson.databind.ObjectMapper;

     

    public class PageInfoResult {

     

       // 定义jackson对象

       private static final ObjectMapper MAPPER = new ObjectMapper();

     

       private Integer total;// 总记录数

     

       private List<?> rows;// 分页查询集合数

     

       private int page;// 页码

     

       public static int page_rows = 10;// 每页条数

     

       public PageInfoResult() {

       }

     

       public PageInfoResult(Integer total, List<?> rows) {

          this.total = total;

          this.rows = rows;

       }

     

       public PageInfoResult(Long total, List<?> rows) {

          this.total = total.intValue();

          this.rows = rows;

       }

     

     

       public Integer getTotal() {

          return total;

       }

     

       public void setTotal(Integer total) {

          this.total = total;

       }

     

       public List<?> getRows() {

          return rows;

       }

     

       public void setRows(List<?> rows) {

          this.rows = rows;

       }

     

       public int getPage() {

          return page;

       }

     

       public void setPage(int page) {

          this.page = page;

       }

     

       public int getPage_rows() {

          return page_rows;

       }

     

       /*

        * 计算页码总数

        */

       public int total_page() {

          // 判断页码是否能和每页条数整除,能—>商为总页码,否->商+1为总页码

          return total % page_rows == 0 ? total / page_rows : total / page_rows + 1;

       }

     

       /**

        * Object是集合转化

        *

        * @param jsonData

        *            json数据

        * @param clazz

        *            集合中的类型

        * @return

        */

       public static PageInfoResult formatToList(String jsonData, Class<?> clazz) {

          try {

            JsonNode jsonNode = MAPPER.readTree(jsonData);

            JsonNode data = jsonNode.get("rows");

            List<?> list = null;

            if (data.isArray() && data.size() > 0) {

               list = MAPPER.readValue(data.traverse(),

                     MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));

            }

            return new PageInfoResult(jsonNode.get("total").intValue(), list);

          } catch (Exception e) {

            return null;

          }

       }

     

    }

    4. 分页插件进行分页处理

       public PageInfoResult queryAdminList(PageInfoResult infoResult){

          //判断该页面是否为第一页。是第一页。手动赋页码

               if(infoResult.getPage()==0){

                  infoResult.setPage(1);

               }

               //使用分页插件进行分页

               PageHelper.startPage(infoResult.getPage(), infoResult.getPage_rows());

               List<SysUser> admin_list = adminLoginMapper.queryAdminList();

          //对Page结果进行包装   

    PageInfo<SysUser> pageInfo=new PageInfo<>(admin_list);

               PageInfoResult result=new PageInfoResult(pageInfo.getTotal(), admin_list);

               result.setPage(infoResult.getPage());

              

               return result;

       }

  • 相关阅读:
    JAVA日报
    JAVA日报
    论文爬取(七)
    论文爬取(六)
    论文爬取(五)
    JAVA日报
    JAVA日报
    剑指 Offer 68
    剑指 Offer 68
    剑指 Offer 67. 把字符串转换成整数
  • 原文地址:https://www.cnblogs.com/CookiesBear/p/6197826.html
Copyright © 2020-2023  润新知