Mybaits 基于pagehelper分页插件
来源:互联网 发布:电力线路输送网络信号 编辑:程序博客网 时间:2024/05/16 23:36
一、实现原理
二、实现步骤
1.导入相应的jar包
pagehelper-3.4.2.jar
2.在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>
3.在代码中使用
a、设置分页信息:
//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
//紧跟着的第一个select方法会被分页
List<Country> list = countryMapper.selectIf(1);
b、取分页信息
//分页后,实际返回的结果list类型是Page<E>,如果想取出分页信息,需要强制转换为 Page<E>,
Page<Country> listCountry = (Page<Country>)list;
listCountry.getTotal();
c、取分页信息的第二种方法
//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectAll();
//用PageInfo对结果进行包装
PageInfo page = new PageInfo(list);
//测试PageInfo全部属性
//PageInfo包含了非常全面的分页属性
assertEquals(1, page.getPageNum());
assertEquals(10, page.getPageSize());
assertEquals(1, page.getStartRow());
assertEquals(10, page.getEndRow());
assertEquals(183, page.getTotal());
assertEquals(19, page.getPages());
assertEquals(1, page.getFirstPage());
assertEquals(8, page.getLastPage());
assertEquals(true, page.isFirstPage());
assertEquals(false, page.isLastPage());
assertEquals(false, page.isHasPreviousPage());
assertEquals(true, page.isHasNextPage());
四、java测试类的例子
//创建spring 容器对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
//从 spring 容器中取出 Mapper 对象
TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);
//执行查询,并分页
TbItemExample example = new TbItemExample();
//分页
PageHelper.startPage(1,10);
List<TbItem> list = itemMapper.selectByExample(example);
for(TbItem tbItem : list){
System.out.println(tbItem.getTitle());
}
PageInfo<TbItem> pageInfo = new PageInfo<>(list);
long total = pageInfo.getTotal();
System.out.println("总共有商品:"+total);
注:如果用Mybaits 逆向工程生成的 Mapper时,使用此分页插件时,不能使用带查询,否则会起冲突,
解决方法
1、自己写Mapper 文件
2、修改 pagehelper jar包(这边上传的pagehelper jar是传智播客视频中提供的已修改的pagehelper jar包,可以兼容逆向工程的Mapper与待条件查询)
- Mybaits 基于pagehelper分页插件
- 基于Mybatis分页插件PageHelper实现分页功能
- MyBatis分页插件PageHelper
- mybatis分页插件pagehelper
- Mybaties PageHelper分页插件
- Mybatis分页插件-pagehelper
- Mybatis分页插件 - PageHelper
- Mybatis分页插件PageHelper
- PageHelper分页插件使用
- 分页插件PageHelper
- Mybatis分页插件PageHelper
- PageHelper分页插件使用
- 分页插件PageHelper
- MyBatis分页插件PageHelper
- mybatis插件分页PageHelper
- Mybatis分页插件PageHelper
- 分页插件PageHelper使用方法
- 分页插件pageHelper
- Moving Segments
- 2017第八届蓝桥杯决赛_希尔伯特曲线
- LeetCode:53. Maximum Subarray
- React生命周期
- 动画
- Mybaits 基于pagehelper分页插件
- Mysql数据库笔记
- web.dom介绍及其应用.网址
- 2017第八届蓝桥杯决赛_发现环
- 开通博客说明
- [bzoj4195][NOI2015]程序自动分析 离散化 并查集
- Xiangtan Invitation Contest 2017(2017湘潭邀请赛)(Done)
- 解决Python词云库wordcloud不显示中文的问题
- zeroc-ice静态编译(c++ win vs2015)