
数据库:
该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页。
第一步:把PageHelper依赖的jar包添加到工程中
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</dependency>
第二步:在Mybatis配置xml中配置拦截器插件
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
测试使用
public class PageHelperTest {
@Test
public void testPageHelper() throws Exception{
//初始化spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
//从容器中获取Mapper代理对象
TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);
//执行sql语句之前设置分页信息使用PageHelper的startPage方法
PageHelper.startPage(1, 10);
//执行查询
TbItemExample example = new TbItemExample();
List<TbItem> list = itemMapper.selectByExample(example);
//取分页信息,PageInfo。1,总记录数2,总页数3,当前页码。
PageInfo<TbItem> pageInfo = new PageInfo<>(list);
System.out.println(pageInfo.getTotal());
System.out.println(pageInfo.getPages());
System.out.println(list.size());
}
}
测试结果:

网友评论