mysql pageinfo_【防坑指南】使用Mybatis分页插件PageHelper为什么PageInfo对象出现null的原因...

在mybatis中,先导入pagehelper.jar所需的jar包,然后在sqlMapConfig,xml中配置插件

配置好插件后就进行测试public class TestPagehelper {

@Test

public void test(){

//读取applicationContext配置文件

ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");

//获取mapper代理对象

TbItemMapper mapper = context.getBean(TbItemMapper.class);

//执行查询语句

TbItemExample example = new TbItemExample();

List list = mapper.selectByExample(example);

//设置PageHelper分页信息,1表示当前第1页,10表示当前页的条数为10

PageHelper.startPage(1, 10);

//获取分页信息

PageInfo pageInfo=new PageInfo<>(list);

System.out.println(pageInfo);

}

}

输出结果为:PageInfo{pageNum=0, pageSize=0, size=0, startRow=0, endRow=0, total=0, pages=0,

list=null, firstPage=0, prePage=0, nextPage=0, lastPage=0, isFirstPage=false,

isLastPage=false, hasPreviousPage=false, hasNextPage=false, navigatePages=0, navigatepageNums=null}

为什么PageInfo对象为null呢?

原因就是:必须得先设置PageHelper,然后执行查询语句

7534633c7dffd7a326864668750f54d1.png

public class TestPagehelper {

@Test

public void test(){

//读取applicationContext配置文件

ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");

//获取mapper代理对象

TbItemMapper mapper = context.getBean(TbItemMapper.class);//设置PageHelper分页信息,1表示当前第1页,10表示当前页的条数为10

PageHelper.startPage(1, 10);        //执行查询语句      TbItemExample example = new TbItemExample();List list = mapper.selectByExample(example);        //获取分页信息        PageInfo pageInfo=new PageInfo<>(list);System.out.println(pageInfo);}}查询成功

PageInfo{pageNum=1, pageSize=10, size=10, startRow=1, endRow=10, total=934, pages=94, list=Page{pageNum=1, pageSize=10, startRow=0, endRow=10, total=934, pages=94}, firstPage=1, prePage=0, nextPage=2, lastPage=8, isFirstPage=true, isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=8, navigatepageNums=[1, 2, 3, 4, 5, 6, 7, 8]}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值