Mybatis插件PageHelper使用简单总结

mybatis-pagehelper插件能方便的让我们对一批数据进行分页操作。

maven仓库坐标:

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

简单使用:

因为是Mybatis插件,所以需要在Mybatis核心配置文件中进行插件的相关配置。

未和spring框架整合的需要在mybatis-config的xml文件中进行配置:

<?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>
 ......
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 使用下面的方式配置参数,参考github上的文档 -->
            <property name="param1" value="value1"/>
        </plugin>
    </plugins>
</configuration>

plugins标签位置在settings标签之后,environments标签之前。

spring框架整合后,因为mybatis-config.xml文件可以不需要,所以此时需要配置SqlSessionFactoryBean到spring的ApplicationContext容器中。所以plugins也相应的在spring容器中的SqlSessionFactoryBean配置:

<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>

以上配置二选一,整合spring后也只能选其中一种方式配置。

配置完成后就可以在代码中使用该该插件了。

SqlSession sqlSession = null;
try {
    SqlSessionFactory sqlSessionFactory = getSqlSession();
    sqlSession = sqlSessionFactory.openSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);

    Page<Object> startPage = PageHelper.startPage(2,3);
    List<Users> usersOfPage = mapper.getUsersOfPage();

    /**
     * 页数
     */
    int pageNum = startPage.getPageNum();
    System.err.println("页码:"+pageNum);
    /**
     * 总数
     */
    long total = startPage.getTotal();
    System.err.println("数据总数:"+total);
    /**
     * 进行count查询的列名
     */
    String countColumn = startPage.getCountColumn();
    System.err.println(countColumn);
    /**
     * 排序
     */
    String orderBy = startPage.getOrderBy();
    System.err.println(orderBy);
    /**
     * 总页数
     */
    int pages = startPage.getPages();
    System.err.println(pages);
    /**
     ************* 通过pageInfo对页码导航进行相应的处理 ***************
     */
    PageInfo<Users> pageInfo = new PageInfo(usersOfPage,2);
    int[] navigatepageNums = pageInfo.getNavigatepageNums();
    for (int i : navigatepageNums) {
        System.err.println(i);
    }

}catch(Exception e) {
    e.printStackTrace();
}finally {
    sqlSession.close();
}

重点: PageHelper.startPagePageHelper.offsetPage的区别:

startPage方法有两个参数。第一个参数是启始页,启始页从1开始计,当启始页设置为0时默认为1。第二个参数为一页显示几条数据。

offsetPage同样有两个参数。但是第一个参数则为启始数据的条数,第二个参数则为从启始数据开始取几条数据。

部分摘录自插件作者GitHub上的代码。

更多参考Github地址: Mybatis-PageHelper

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值