Mybatis分页插件 - PageHelper

首先把PageHelper依赖的jar包添加到工程中

在Mybatis的全局文件中配置SqlMapConfig.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>

注意:紧跟着分页信息PageHelper.startPage(1, 5),的第一个select方法才会被分页

代码如下:

@Test
	public void testPageHelper(){
		
		//初始化spring容器
		ApplicationContext  context = new ClassPathXmlApplicationContext("spring/applicationContext-dao.xml");
		
		//获取mapper的代理对象
		TbItemMapper mapper = context.getBean(TbItemMapper.class);
		
		//设置分页信息
		PageHelper.startPage(1, 5);//从第1条记录开始,5行
		
		TbItemExample e = new TbItemExample();
		
		//注:紧跟着分页信息的第一个查询才会被分页
		List<TbItem> list1 = mapper.selectByExample(e);
		List<TbItem> list2 = mapper.selectByExample(e);
		
		System.out.println("第一个分页的list的集合长度"+list1.size());
		System.out.println("第二个分页的list的集合长度"+list2.size());
		
		System.out.println("============================");
		//PageInfo 获取分页信息
		PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(list1);
		
		System.out.println("list1的总记录数为:"+pageInfo.getTotal());
		System.out.println("list1的所有页数为:"+pageInfo.getPages());
		System.out.println("list1当前页数为:"+pageInfo.getPageNum());
		System.out.println("list1每页显示的记录数为:"+pageInfo.getPageSize());

		System.out.println("============================");
		
		PageInfo<TbItem> pageInfo2 = new PageInfo<TbItem>(list2);
		
		System.out.println("list2的总记录数为:"+pageInfo2.getTotal());
		System.out.println("list2的所有页数为:"+pageInfo2.getPages());
		System.out.println("list2当前页数为:"+pageInfo2.getPageNum());
		System.out.println("list2每页显示的记录数为:"+pageInfo2.getPageSize());
		
	}

如果设置一个分页信息PageHelper.startPage(1, 5);,紧跟着分页信息的第一条select语句才会被分页, 

 

如果再次设置分页信息PageHelper.startPage(2, 10);,则紧跟的select语句也会被分页

 

@Test
	public void testPageHelper(){
		
		//初始化spring容器
		ApplicationContext  context = new ClassPathXmlApplicationContext("spring/applicationContext-dao.xml");
		
		//获取mapper的代理对象
		TbItemMapper mapper = context.getBean(TbItemMapper.class);
		
		
		
		//设置分页信息
		PageHelper.startPage(1, 5);//从第1条记录开始,5行
		
		TbItemExample e = new TbItemExample();
		
		//注:紧跟着分页信息的第一个查询才会被分页
		List<TbItem> list1 = mapper.selectByExample(e);
	
		
		
		//再次设置分页信息,紧跟的第一个select也会被分页
		PageHelper.startPage(2, 10);//从第1条记录开始,5行
		TbItemExample e2 = new TbItemExample();
		List<TbItem> list2 = mapper.selectByExample(e2);
		
		System.out.println("第一个分页的list的集合长度"+list1.size());
		System.out.println("第二个分页的list的集合长度"+list2.size());
		
		System.out.println("============================");
		//PageInfo 获取分页信息
		PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(list1);
		
		System.out.println("list1的总记录数为:"+pageInfo.getTotal());
		System.out.println("list1的所有页数为:"+pageInfo.getPages());
		System.out.println("list1当前页数为:"+pageInfo.getPageNum());
		System.out.println("list1每页显示的记录数为:"+pageInfo.getPageSize());

		System.out.println("============================");
		
		PageInfo<TbItem> pageInfo2 = new PageInfo<TbItem>(list2);
		
		System.out.println("list2的总记录数为:"+pageInfo2.getTotal());
		System.out.println("list2的所有页数为:"+pageInfo2.getPages());
		System.out.println("list2当前页数为:"+pageInfo2.getPageNum());
		System.out.println("list2每页显示的记录数为:"+pageInfo2.getPageSize());
		
	}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值