国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php框架 > 框架设计 > MyBatis分页插件的使用——PageHelper

MyBatis分页插件的使用——PageHelper

来源:程序员人生   发布时间:2016-06-23 15:10:36 阅读次数:6955次


1,配置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版本增加)。


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

      

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

3,分页步骤


            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); }




生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生