先增加依赖
compile group: 'com.github.pagehelper', name: 'pagehelper', version: '5.0.2'编写MyBatis配置文件
<?xml version="1.0" encoding="UTF-8"?>详细参数请看:http://git.oschina.net/free/Mybatis_PageHelper/blob/master/wikis/zh/HowToUse.md
<!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>
使用:
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}