zoukankan      html  css  js  c++  java
  • Mybatis的PageHelper插件实现分页

      实现Mybatis的分页以下几种方式都可以实现。

      数据量不大或者数据提供者不支持分页,例如数据从设备上读取,就没有分页接口。可以读取所有的数据,放到List中,使用subList(start,end)方式来获取指定的数据。

      sql语句实现分页,例如MySQL的select a,b,c from test limit #{start}, #{pageSize}来获取指定分页的数据。

      使用sql语句实现分页,一般要写2个语句,一个获取数据的总量,一个用于获取指定分页的数据。

      或者可以使用PageHelper来实现分页。

      添加maven依赖:

    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.7</version>
    </dependency>

      spring-boot项目的yml增加配置

    #pagehelper分页插件配置
    pagehelper:
        helper-dialect: mysql
        reasonable: true
        support-methods-arguments: true 
      reasonable:分页合理化参数,默认值为false。当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询。

            

       

      mapper文件,不需要count(*)或者limit

    <resultMap id="BaseResultMap" type="com.xxx.CfmMd" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="score" property="score" jdbcType="INTEGER" />
      </resultMap>

      <sql id="Base_Column_List" >
        id, name, score
      </sql>

     <select id="selectBySocre3" resultMap="BaseResultMap">
       select
       <include refid="Base_Column_List" />
       from test_integer_0 where id > 0
     </select>

       service代码

    PageHelper.startPage(1,2);
    List<CfmMd> userList= mapper.selectBySocre3();
    PageInfo<CfmMd> pageInfo = new PageInfo<CfmMd>(userList);

       userList的数据如下

       pageInfo的数据如下

  • 相关阅读:
    3dsmax不同版本 pyside qt UI 设置max窗口为父窗口的方法
    oracle中的数据库和实例
    oracle中的表空间(tablespace)、方案(schema)、段(segment)、区(extent)、块(block)
    什么是WSE
    Server.Transfer,Response.Redirect的区别
    Oracle 中的几个数据类型介绍
    oracle中的连接字符串
    Oracle中的 单引号 和 双引号
    接口是否可以继承接口?抽象类是否可以实现接口?抽象类是否可以继承实体类?
    聚簇索引
  • 原文地址:https://www.cnblogs.com/lnlvinso/p/14787935.html
Copyright © 2011-2022 走看看