MyBatis分页插件PageHelper的使用

先增加依赖

compile group: 'com.github.pagehelper', name: 'pagehelper', version: '5.0.2'
编写MyBatis配置文件

<?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>
        <!-- com.github.pagehelper 为 PageInterceptor 类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 使用 PageHelper 方式进行分页 -->
            <property name="dialect" value="com.github.pagehelper.PageHelper"/>
            <!-- 指定分页插件使用哪种方言 -->
            <property name="helperDialect" value="mysql"/>
            <property name="offsetAsPageNum" value="true"/>
            <property name="rowBoundsWithCount" value="true"/>
            <!--
                默认值为 false,当该参数设置为 true 时,如果 pageSize=0 或者 RowBounds.limit = 0
                就会查询出全部的结果(相当于没有执行分页查询,但是返回结果仍然是 Page 类型)。
            -->
            <property name="pageSizeZero" value="true"/>
            <!--
                分页合理化参数,默认值为false。
                当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。
                默认false 时,直接根据参数进行查询。
            -->
            <property name="reasonable" value="true"/>

        </plugin>
    </plugins>

</configuration>
详细参数请看:http://git.oschina.net/free/Mybatis_PageHelper/blob/master/wikis/zh/HowToUse.md

使用:

import cn.bjut.entity.Person;
import cn.bjut.mapper.PersonMapper;
import com.github.pagehelper.PageHelper;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * Created by N3verL4nd on 2017/6/6.
 */
public class T {

    @Test
    public void test() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        PersonMapper personMapper = ctx.getBean(PersonMapper.class);
        PageHelper.startPage(5, 3);
        List<Person> list = personMapper.selectAllPersons();
        list.forEach(System.out::println);
    }
}

输出:

2017-06-12 16:08:22,394 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
2017-06-12 16:08:22,405 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7e07db1f] was not registered for synchronization because synchronization is not active
2017-06-12 16:08:22,407 [main] DEBUG [SQL_CACHE] - Cache Hit Ratio [SQL_CACHE]: 0.0
2017-06-12 16:08:22,443 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
2017-06-12 16:08:22,685 [main] DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=true, UserName=root@localhost, MySQL Connector Java] will not be managed by Spring
2017-06-12 16:08:22,689 [main] DEBUG [cn.bjut.mapper.PersonMapper.selectAllPersons_COUNT] - ==>  Preparing: SELECT count(0) FROM persons 
2017-06-12 16:08:22,713 [main] DEBUG [cn.bjut.mapper.PersonMapper.selectAllPersons_COUNT] - ==> Parameters: 
2017-06-12 16:08:22,727 [main] DEBUG [cn.bjut.mapper.PersonMapper.selectAllPersons_COUNT] - <==      Total: 1
2017-06-12 16:08:22,730 [main] DEBUG [cn.bjut.mapper.PersonMapper.selectAllPersons] - ==>  Preparing: SELECT id, name, age FROM persons LIMIT 3 
2017-06-12 16:08:22,730 [main] DEBUG [cn.bjut.mapper.PersonMapper.selectAllPersons] - ==> Parameters: 
2017-06-12 16:08:22,732 [main] DEBUG [cn.bjut.mapper.PersonMapper.selectAllPersons] - <==      Total: 3
2017-06-12 16:08:22,732 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7e07db1f]
2017-06-12 16:08:22,732 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
Person{id=1, name='lgh', age=30}
Person{id=2, name='xiya', age=20}
Person{id=3, name='芊漪', age=100}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

N3verL4nd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值