这里以maven为例(若是下载jar,还须要下载pageHelper的依赖com.github.jsqlparser):java
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency>
在mybatis的全局配置文件里<configuration>标签下添加下面的配置mysql
<!-- plugins在配置文件中的位置必须符合要求,不然会报错,顺序以下: properties?, settings?, typeAliases?, typeHandlers?, objectFactory?,objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers? --> <plugins> <!-- com.github.pagehelper为PageHelper类所在包名 --> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 4.0.0之后版本能够不设置该参数 --> <property name="dialect" value="mysql"/> <!-- 该参数默认为false --> <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 --> <!-- 和startPage中的pageNum效果同样--> <property name="offsetAsPageNum" value="true"/> <!-- 该参数默认为false --> <!-- 设置为true时,使用RowBounds分页会进行count查询 --> <property name="rowBoundsWithCount" value="true"/> <!-- 设置为true时,若是pageSize=0或者RowBounds.limit = 0就会查询出所有的结果 --> <!-- (至关于没有执行分页查询,可是返回结果仍然是Page类型)--> <property name="pageSizeZero" value="true"/> <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 --> <!-- 启用合理化时,若是pageNum<1会查询第一页,若是pageNum>pages会查询最后一页 --> <!-- 禁用合理化时,若是pageNum<1或pageNum>pages会返回空数据 --> <property name="reasonable" value="false"/> <!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 --> <!-- 增长了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 --> <!-- 能够配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默认值 --> <!-- 不理解该含义的前提下,不要随便复制该配置 --> <property name="params" value="pageNum=pageHelperStart;pageSize=pageHelperRows;"/> <!-- 支持经过Mapper接口参数来传递分页参数 --> <property name="supportMethodsArguments" value="false"/> <!-- always老是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page --> <property name="returnPageInfo" value="none"/> </plugin> </plugins>
下面是Junit单元测试的简单实现分页查询的代码git
package com.taotao.PageHelper; import java.util.ArrayList; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.junit.Test; import org.junit.runner.RunWith; import org.mybatis.spring.SqlSessionFactoryBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.taotao.mapper.TbItemMapper; import com.taotao.pojo.TbItem; import com.taotao.pojo.TbItemExample; import com.taotao.pojo.TbItemExample.Criteria; //自动加载spring容器 @RunWith(SpringJUnit4ClassRunner.class) // 告诉junit spring的配置文件 @ContextConfiguration({ "classpath:spring/applicationContext-dao.xml", "classpath:spring/applicationContext-service.xml" }) public class TestPageHelper { @Autowired private TbItemMapper itemMapper; /** * 无条件分页查询测试 * * @Title:函数 TestPageHelper * @Description:Comment for non-overriding methods * @author 张颖辉 * @date 2016年9月19日下午9:54:36 */ @Test public void TestPageHelper() { // 执行查询并分页 TbItemExample itemExample = new TbItemExample(); // 分页处理 PageHelper.startPage(2, 10);// 若是没有这行,后面就会取出全部的商品list // 执行查询语句获取商品列表 List<TbItem> items = itemMapper.selectByExample(itemExample); // 打印返回的商品列表 for (int i = 0; i < items.size(); i++) { System.out.println((i + 1) + ":" + items.get(i).getTitle()); } // 获取分页信息 PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(items); long total = pageInfo.getTotal(); System.out.println("共有商品:" + total); } @Autowired SqlSessionFactoryBean sqlSessionFactory; /** * 条件分页查询测试 * * @Title:函数 TestPageHelper2 * @Description:Comment for non-overriding methods * @author 张颖辉 * @date 2016年9月19日下午9:54:55 * @throws Exception */ @Test public void TestPageHelper2() throws Exception { /** * 另一种注入的方式,经过SqlSessionFactoryBean拿到bean(比较落后,已经不经常使用) */ // SqlSessionFactory sessionFactory = sqlSessionFactory.getObject(); // SqlSession sqlSession = sessionFactory.openSession(); // TbItemMapper itemMapper = sqlSession.getMapper(TbItemMapper.class); System.out.println("测试获取数据库会话对象Session:" + sqlSessionFactory.getObject().openSession()); TbItemExample itemExample = new TbItemExample(); Criteria criteria = itemExample.createCriteria(); criteria.andIdLessThan(700000L); // criteria.andPriceEqualTo(49800L); // 获取第1页,10条内容,默认查询总数count PageHelper.startPage(1, 10); // 紧跟着的第一个select方法会被分页 List<TbItem> items = itemMapper.selectByExample(itemExample); // 打印返回的商品列表 for (int i = 0; i < items.size(); i++) { System.out.println((i + 1) + ":" + items.get(i).getTitle()); } // 获取分页信息 PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(items); long total = pageInfo.getTotal(); System.out.println("共有商品:" + total); } }
上面的分页方法支持自定义多表关联查询sql的mapper实现。github
注意:分页插件对逆向工程生成的代码支持很差(3.4.2版本),不能对有查询条件的查询分页,会抛异常。spring
可是后来使用4.1.6能够支持逆向工程。sql
官方文档:包含更多配置以及注意事数据库