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

    }
}

猜你喜欢

转载自blog.csdn.net/bighuan/article/details/78597133