zoukankan      html  css  js  c++  java
  • Pagehelper分页插件的配置和各种使用方式

    转自:https://www.jianshu.com/p/5acf5ac7a440

    mybatis + pagehelper 数据库分页
    Mybatis的一个插件,PageHelper,非常方便mybatis分页查询。

    优点: 不需要自己在每个 mapper文件中写 limit x,x 这样的代码,引入插件调用pagehelper即可自动注入分页。

    1、引入jar

    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>最新版本</version>
    </dependency>
    
    //推荐使用下面这种方式
    <!--pagehelper-->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.3</version>
    </dependency>
    

    2、配置插件拦截规则

    • 方法一、在 MyBatis 配置 xml 中配置拦截器插件
      在mybatis-config.xml中配置
    <!-- 
        plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
        properties?, settings?, 
        typeAliases?, typeHandlers?, 
        objectFactory?,objectWrapperFactory?, 
        plugins?, 
        environments?, databaseIdProvider?, mappers?
    -->
    <plugins>
        <!-- com.github.pagehelper为PageHelper类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
            <property name="param1" value="value1"/>
        </plugin>
    </plugins>
    
    • 方法二、在 Spring 配置文件中配置拦截器插件
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <!-- 注意其他配置 -->
      <property name="plugins">
        <array>
          <bean class="com.github.pagehelper.PageInterceptor">
            <property name="properties">
              <!--使用下面的方式配置参数,一行配置一个 -->
              <value>
                params=value1
              </value>
            </property>
          </bean>
        </array>
      </property>
    </bean>
    

    如图:


     
    image.png
    • 方法三、在 SpringBoot配置文件application.properties,添加如下几行配置信息,推荐使用
    #分页插件
    pagehelper.helper-dialect=mysql
    pagehelper.params=count=countSql
    pagehelper.reasonable=true
    pagehelper.support-methods-arguments=true
    

    3、调用

    //第一种、RowBounds方式的调用
    List<Country> list = sqlSession.selectList("x.y.selectIf", null, new RowBounds(0, 10));
    
    //第二种、Mapper接口方式的调用,推荐这种使用方式。
    PageHelper.startPage(1, 10);
    List<Country> list = countryMapper.selectIf(1);
    
    //第三种、Mapper接口方式的调用,推荐这种使用方式。
    PageHelper.offsetPage(1, 10);
    List<Country> list = countryMapper.selectIf(1);
    
    //第四种、参数方法调用
    //存在以下 Mapper 接口方法,你不需要在 xml 处理后两个参数
    public interface CountryMapper {
        List<Country> selectByPageNumSize(
                @Param("user") User user,
                @Param("pageNum") int pageNum, 
                @Param("pageSize") int pageSize);
    }
    //配置supportMethodsArguments=true
    //在代码中直接调用:
    List<Country> list = countryMapper.selectByPageNumSize(user, 1, 10);
    
    //第五种、参数对象
    //如果 pageNum 和 pageSize 存在于 User 对象中,只要参数有值,也会被分页
    //有如下 User 对象
    public class User {
        //其他fields
        //下面两个参数名和 params 配置的名字一致
        private Integer pageNum;
        private Integer pageSize;
    }
    //存在以下 Mapper 接口方法,你不需要在 xml 处理后两个参数
    public interface CountryMapper {
        List<Country> selectByPageNumSize(User user);
    }
    //当 user 中的 pageNum!= null && pageSize!= null 时,会自动分页
    List<Country> list = countryMapper.selectByPageNumSize(user);
    
    //第六种、ISelect 接口方式
    //jdk6,7用法,创建接口
    Page<Country> page = PageHelper.startPage(1, 10).doSelectPage(new ISelect() {
        @Override
        public void doSelect() {
            countryMapper.selectGroupBy();
        }
    });
    //jdk8 lambda用法
    Page<Country> page = PageHelper.startPage(1, 10).doSelectPage(()-> countryMapper.selectGroupBy());
    
    //也可以直接返回PageInfo,注意doSelectPageInfo方法和doSelectPage
    pageInfo = PageHelper.startPage(1, 10).doSelectPageInfo(new ISelect() {
        @Override
        public void doSelect() {
            countryMapper.selectGroupBy();
        }
    });
    //对应的lambda用法
    pageInfo = PageHelper.startPage(1, 10).doSelectPageInfo(() -> countryMapper.selectGroupBy());
    
    //count查询,返回一个查询语句的count数
    long total = PageHelper.count(new ISelect() {
        @Override
        public void doSelect() {
            countryMapper.selectLike(country);
        }
    });
    //lambda
    total = PageHelper.count(()->countryMapper.selectLike(country));
    

    参考:
    https://blog.csdn.net/kittyboy0001/article/details/79317450/

  • 相关阅读:
    ECMAScript 继承继承机制实现
    ECMAScript 函数定义类或对象
    药品查询APP开发流程(五)开发—yao_search.js
    ECMAScript 函数修改对象
    药品查询APP开发流程(六)开发—yao_category.js
    药品查询APP开发流程(四)开发—app.js
    药品查询APP开发流程(一)需求分析
    药品查询APP开发流程(三)开发—SQLite数据库
    ECMAScript 继承继承机制实例
    求最长递减子序列(转载)
  • 原文地址:https://www.cnblogs.com/sharpest/p/13709714.html
Copyright © 2011-2022 走看看