MyBatis分页插件的使用——PageHelper


一,配置plugin


   在myBatis的配置文件中,加入如下配置:


<configuration>
		<!-- 配置分页插件 -->
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageHelper">
			<!-- 指定使用的数据库是什么 -->
			<property name="dialect" value="mysql"/>
		</plugin>
	</plugins>

</configuration>

  PS:

  该插件目前支持以下数据库的物理分页:

  1. Oracle
  2. Mysql
  3. MariaDB
  4. SQLite
  5. Hsqldb
  6. PostgreSQL
  7. DB2
  8. SqlServer(2005,2008)
  9. Informix
  10. H2
  11. SqlServer2012

    配置dialect属性时,可以使用小写形式:

  oracle,mysql,mariadb,sqlite,hsqldb,postgresql,db2,sqlserver,informix,h2,sqlserver2012

  在4.0.0版本以后,dialect参数可以不配置,系统能自动识别这里提到的所有数据库。

  对于不支持的数据库,可以实现com.github.pagehelper.parser.Parser接口,然后配置到dialect参数中(4.0.2版本增加)。


二,引入jar包或通过其他方式配置依赖

      

	<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
		</dependency>

三,分页步骤


            1,通过PageHelper.startPage(page,rows)开始分页;

            2,通过PageInfo获取分页结果;


@Test
	public void testPageHelper() throws Exception{
		//1,获得mapper代理对象
		ApplicationContext application=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
		TbItemMapper itemMapper=application.getBean(TbItemMapper.class);
		//2,设置分页
		PageHelper.startPage(1, 30);
		//3,执行查询
		TbItemExample example=new TbItemExample();
		List<TbItem>list=itemMapper.selectByExample(example);
		//4,取得分页结果
		PageInfo<TbItem> pageInfo=new PageInfo<TbItem>(list);
		long total=pageInfo.getTotal();
		System.out.println(total);
		int pages=pageInfo.getPages();
		System.out.println(pages);
		int pageSize=pageInfo.getPageSize();
		System.out.println(pageSize);
		
	}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值