MyBatis分页插件的使用——PageHelper
2016-05-29 21:05
447 查看
一,配置plugin
在myBatis的配置文件中,加入如下配置:<configuration> <!-- 配置分页插件 --> <plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 指定使用的数据库是什么 --> <property name="dialect" value="mysql"/> </plugin> </plugins> </configuration>
PS:
该插件目前支持以下数据库的物理分页:
Oracle
Mysql
MariaDB
SQLite
Hsqldb
PostgreSQL
DB2
SqlServer(2005,2008)
Informix
H2
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); }
相关文章推荐
- 【ES6】变量的解构赋值
- 描述Android系统在视频播放时来电话的解决方案
- Android1.6 启动init.c分析
- svn使用与配置
- Go学习笔记二: 函数,文件操作及网络通信
- iOS-OC为什么需要重写setter或getter方法?
- onActivityResult——Activity给之前的Activiy回传值
- 236. Lowest Common Ancestor of a Binary Tree
- macbook air完美使用wtg外接硬盘运行win8系统
- c#中的异步编程
- Linux驱动开发、20-SPI子系统分析
- 快餐店之间插入仓库,路最短,DP,POJ(1485)
- ZZULIOJ 1432: 背包again <类似进制问题>
- url 重写,设置伪静态页面
- 23种设计模式01--简单工厂
- 多重背包问题
- Tinyhttp服务器编译运行
- eerror: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCo
- C51学习笔记
- C++走向远洋——57(项目二2、动物这样叫、抽象类)