PageHelper测试分页插件

步骤一,在maven的pom.xml文件中添加相应的依赖,mybatis的依赖,相应Jdbc驱动的依赖,PageHelper的依赖
PageHelper的依赖如下
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>4.2.1</version>
</dependency>

在resource下新建一个spring文件夹

测试代码:
package com.taotao.dao;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemExample;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * 测试Mybatis分页插件
 */
public class TestPageHelper {

    @Test
    public void testPageHelper() throws Exception {
        //1,创建一个Spring容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
        //2,从spring容器中获得Mapper的代理对象
        TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);
        //3,设置分页信息
        PageHelper.startPage(1, 30);
        //4,执行查询
        TbItemExample tbItemExample = new TbItemExample();
        List<TbItem> tbItemsList = itemMapper.selectByExample(tbItemExample);
        //5,取分页结果
        PageInfo<TbItem> pageInfo = new PageInfo<>(tbItemsList);

        long total = pageInfo.getTotal();
        System.out.println("total:" + total);
        int pages = pageInfo.getPages();
        System.out.println("pages:" + pages);
        int pageSize = pageInfo.getPageSize();
        System.out.println("pageSize:" + pageSize);

        List<TbItem> list = pageInfo.getList();
        System.out.println("list:" + list);

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值